1.2 part a (with key) done

This commit is contained in:
Kaushik Narayan R 2024-01-31 21:06:59 -07:00
parent 640aa3e9af
commit 6ff8e3d880
16 changed files with 99 additions and 8 deletions

View File

@ -1,3 +1,5 @@
import sys
with open("3/forstudents/a/ciphertext.bin", "rb") as cipher_file, open( with open("3/forstudents/a/ciphertext.bin", "rb") as cipher_file, open(
"3/forstudents/a/key.bin", "rb" "3/forstudents/a/key.bin", "rb"
) as key_file: ) as key_file:
@ -5,6 +7,6 @@ with open("3/forstudents/a/ciphertext.bin", "rb") as cipher_file, open(
key_content = key_file.read() key_content = key_file.read()
print( print(
(( ((
int.from_bytes(cipher_content, "big") ^ int.from_bytes(key_content, "big") int.from_bytes(cipher_content, sys.byteorder) ^ int.from_bytes(key_content, sys.byteorder)
).to_bytes(max(len(cipher_content), len(key_content)), "big")).decode() ).to_bytes(max(len(cipher_content), len(key_content)), sys.byteorder)).decode()
) )

View File

@ -1,3 +1,5 @@
import sys
with open("3/forstudents/b/ciphertext1.bin", "rb") as cipher1_file, open( with open("3/forstudents/b/ciphertext1.bin", "rb") as cipher1_file, open(
"3/forstudents/b/ciphertext2.bin", "rb" "3/forstudents/b/ciphertext2.bin", "rb"
) as cipher2_file, open("3/forstudents/b/plaintext1.txt", "r") as plain_unknown_file: ) as cipher2_file, open("3/forstudents/b/plaintext1.txt", "r") as plain_unknown_file:
@ -6,14 +8,14 @@ with open("3/forstudents/b/ciphertext1.bin", "rb") as cipher1_file, open(
plain_unknown_content = plain_unknown_file.read() plain_unknown_content = plain_unknown_file.read()
possible_key = ( possible_key = (
int.from_bytes(cipher1_content, "big") int.from_bytes(cipher1_content, sys.byteorder)
^ int.from_bytes(bytes(plain_unknown_content, "utf-8"), "big") ^ int.from_bytes(bytes(plain_unknown_content, "utf-8"), sys.byteorder)
).to_bytes(max(len(cipher1_content), len(plain_unknown_content)), "big") ).to_bytes(max(len(cipher1_content), len(plain_unknown_content)), sys.byteorder)
print( print(
( (
( (
int.from_bytes(cipher2_content, "big") int.from_bytes(cipher2_content, sys.byteorder)
^ int.from_bytes(possible_key, "big") ^ int.from_bytes(possible_key, sys.byteorder)
).to_bytes(max(len(cipher2_content), len(possible_key)), "big") ).to_bytes(max(len(cipher2_content), len(possible_key)), sys.byteorder)
).decode("latin-1") ).decode("latin-1")
) )

5
hw1.1/answer.txt Normal file
View File

@ -0,0 +1,5 @@
1a
e--for-example--instead-of-always-being-replaced-by--say--16--will-be-replaced-by-any-one-of-the-figures-16--74--35--21--These-alternates-are-called-homophones--Sometimes-a-cipher-alphabet-will-include-symbols-that-mean-nothing-and-are-intended-to-confuse-interceptors--these-are-called-nulls--As-long-as-only-one-cipher-alphabet-is-in-use--as-above--the-system-is-called-monoalpbabetic--When--howeve
1b
-of-Magic---II--IN-October-the-cabinet-of-Prince-Konoye-fell--and-the-Emperor-summoned-General-Hideki-Tojo-to-form-a-new-government--One-of-the-first-acts-of-the-new-Foreign-Minister--Shigenori-Togo--was-to-call-in-the-chief-of-the-cable-section--Togo--remembering-a-book-that-Herbert-O--Yardley-had-written-disclosing-his-1920-solution-of-Japanese-diplomatic-codes--asked-the-cable-chief--Kazuji-Kam

Binary file not shown.

View File

@ -0,0 +1 @@
13922541651cb1b459187120a6b3621bdc2bfe65ebc18b7d88c9a9fadddd5dbb7073885f7fcde061fd5a3c10f7ef1b731a0c694643d18c06992e5b707c32bd0f plaintext.txt

View File

@ -0,0 +1 @@
Key is Y i.e. rotating by 24

View File

@ -0,0 +1 @@
PMNMPRGMLYRCJWOSGBQKCBYJJGMLKGAFCJMZQGACZMVUCYIDGQFQASQRMKQGLBMARPGLYRCGLOSGPCPQYLARGDGCBAFYDDQCGQKMJMEWQNCPAYJCQAFGQFMJKQZPYEECPQQRSKZJCBRUCPIGLESPMJMEGQRQNPMQNCARSQCQZSLQCLNPGQQGCQRGLOSGPGLEZPYLAFCQ

View File

@ -0,0 +1 @@
Key is shifting by 1 and XORing with YfiSgCbepeey

View File

@ -0,0 +1,2 @@
gqyC7=ðÛpc±“<C2B1>´«³¿<C2B3>°¹˜¯•Ž™»g‰EÓgMYR™ý0yÿ¢²ZÌ÷ÛÊKyDÈ<44>P9©HáÓý7ý´µâð¸Ïˆ¸ð1ÜÏ5i&Fl/ÏZ?­!çý[
r#:Îx¬ñÀ0“xíÊ}Õ³!LçÐÎÙËä6é^ÖxJÂø€TEŸ+ô»ÁK3!eRåüL¹<4C>çápÎ{èR÷ËyfÁD²¦Öâ7CqðQÀT£iDš¨ñdFÌLb…r”ˆJƒø.ÉÇH<ö YKÌÊÉRÐ}{N§)Ñú΢¦ÙÝÂŒ,ˆç2„ŠA7΄÷AšÃ"ê|Çk‰/<2F>Ö©"Ýg|òzù#«»×!õdgÐHL×Ù²íŒ'x7^ËóÖSºü&yPTåcmþ<6D>D^Cf1†°ØDáFª<Œ®ÈdSð-bÎ\ž,¤½¢VWS—<53>âkôÖõ€ÐÜÏ´©<>ÅD°ÜOºßkgó=ÔÜÀƒ‹/ëp©2¤rÝÈô?rýNöb¦ÓAÆ\éy÷®ŸüÙmî\G¦L²„]°Æh¯ I1~,•&“ï¼òþ.4-°Ï®573Ša—=”rv0Auˆ&Ož%ŒÛ_à9V¡žŒúpìUÉÇ!ØnšÚˆ,\„eÞ·è¤<C3A8>i¯4;ìYßsQû»D¿[,¬ù¼p^<5E>ÝQˆ+ ­ÇmÈUeÛU¤ùœ¸^¦åÆkÜI ­”EI´³Úí3ãïAC¯È::b׫aCb

View File

@ -0,0 +1,2 @@
7e6386bb605d1709efed5b375b5d5df56d0bf782aba7c9753da71e21670d96baae89e1316be31b7bb3f9fe9a9bb1e9e04fccfb109e4d100aaf1e1a2065b08f24 juliaplaintext.txt
9505698ce39bd1eb8265d3cb0c4152f364a57cce9af4ea5b9aa4ad3a36f074a22210c58635a1cc198c436bb866bcfcb193f1a78f5be62b680ddc35c6c0072c05 plaintext.txt

View File

@ -0,0 +1 @@
HPDVZDOGVULQJVKLJKODQGVVQRRWLHVWDVFHQGVHWWHUVIODUHVWKLQNLQJVFDUSLQJVHGHUVVZHOOHGPHUFLOHVVRYHUVHQVLWLYHDGYHQWXUHUGLYDQVBSVLODQWLVMRFDVWDKHDGUHVWVGLVVHUWDWLRQVMRLVWVEXVVVHUDSXSSHWUBVEXQWHGDPSKLWKHDWUHVVWHUHRVXQMXVWLILHGCHSKBUXVVDEGLFDWLRQVKXVVHUOSDOPLVWIHUDOPDQQHUOHRQLGVGLOORQVZKHUHIRUHVIHUWLOLCHGGXFNELOOGXQQHVVFRUHVKDLUFXWERWWLFHOOLUHSHOJOLGHVIHOLFHVEUDLQVWRUPLQJVFOHPHQFBUHGRLQJPHGLFDLGVEOXQGHUEXVV

Binary file not shown.

View File

@ -0,0 +1,2 @@
da1918d414a12e8453bf938218242cf348c3538ecb71da2a3857c431693516c6e23fb20cdfdf54a676bdf0a78bf0eb7fe928c389b97818ff79b7ec921742d4d4 plaintext.txt
356b8c38a00a53f1530aeeb4f30ac0bb64f7f66c9522ecfe10249c707766215af0695193a7673c35901e4d6e73397d04291fc5e47903f732b248dc241ed54729 secretfile.txt

10
hw1.2/answer.txt Normal file
View File

@ -0,0 +1,10 @@
a - caesar
left shift:
ROPORTIONATELYQUIDSMEDALLIONMICHELOBSICEBOXWEAKFISHSCUSTOMSINDOCTRINATEINQUIRERSANCTIFIEDCHAFFSEISMOLOGYSPERCALESCHISHOLMSBRAGGERSSTUMBLEDTWERKINGUROLOGISTSPROSPECTUSESBUNSENPRISSIESTINQUIRINGBRANCHES
(right shift gave:)
NKLKNPEKJWPAHUMQEZOIAZWHHEKJIEYDAHKXOEYAXKTSAWGBEODOYQOPKIOEJZKYPNEJWPAEJMQENANOWJYPEBEAZYDWBBOAEOIKHKCUOLANYWHAOYDEODKHIOXNWCCANOOPQIXHAZPSANGEJCQNKHKCEOPOLNKOLAYPQOAOXQJOAJLNEOOEAOPEJMQENEJCXNWJYDAO
which is not english text, also sha512 sum doesn't match
a - julia
mile Vinay and Joseph Gaussinthough not with the speed and ease of a typewriter keyboard. Rather it was the assimilation of encipherment into the overall communication process. Vernam created what came to be called "on-line encipherment" (because it was done directly on the open telegraph circuit) to distinguish it from the old, separate, off-line encipherment. He freed a fundamental process in cryptography from the shackles of time and error. He eliminated a human beingthe cipher clerkfrom the chain of communication. His great contribution was to bring to cryptography the automation that had benefited mankind so much in so many fields of endeavor. These values were immediately recognized, and Ver-nam's idea quickly kicked up a flurry of activity. He put it down on paper in a sketch dated December 17. A.T. & T. notified the Navy, with which it had worked closely in a communications demonstration the previous year, and on February 18, 1918, Vernam, Parker, Lyman F. Morehouse, equipment

24
hw1.2/caesar_with_key.py Normal file
View File

@ -0,0 +1,24 @@
with open("3/withkey/caesarkey.txt", "r") as caesar_key_file, open(
"3/withkey/ciphertext.txt", "r"
) as caesar_cipher_file:
caesar_key_content = caesar_key_file.read()
caesar_cipher_content = caesar_cipher_file.read()
key_shift = int(caesar_key_content.split()[-1])
print("key shift", key_shift)
caesar_plaintext_content_left_shift = ""
caesar_plaintext_content_right_shift = ""
for char in caesar_cipher_content:
caesar_plaintext_content_left_shift += chr(
(ord(char) - ord("A") - key_shift) % 26 + ord("A")
)
caesar_plaintext_content_right_shift += chr(
(ord(char) - ord("A") + key_shift) % 26 + ord("A")
)
print("left shift")
print(caesar_plaintext_content_left_shift)
print()
print("right shift")
print(caesar_plaintext_content_right_shift)

37
hw1.2/julia_with_key.py Normal file
View File

@ -0,0 +1,37 @@
import sys
import os
from itertools import cycle
import gzip
def ror(n, rotations, width):
return (2**width - 1) & (n >> rotations | n << (width - rotations))
def rol(n, rotations, width):
return (2**width - 1) & (n << rotations | n >> (width - rotations))
with open("3/withkey/juliakey.txt", "r") as julia_key_file, open(
"3/withkey/juliaplaintext.txt.gz.enc", "rb"
) as julia_cipher_file:
julia_key_content = julia_key_file.read()
key_shift = int(julia_key_content.split()[4])
key_xor = julia_key_content.split()[-1]
print("key shift", key_shift)
print("key xor", key_xor)
julia_cipher_content = julia_cipher_file.read()
result = bytes()
for c_byte, k_byte in zip(julia_cipher_content, cycle(key_xor)):
tmp = c_byte ^ ord(k_byte)
# kinda misleading, question mentions that encryption rotates bits but didn't mention which direction
# imagine spending 3 hours thinking it's gzip when it turns out you rotate RIGHT
result += ror(c_byte ^ ord(k_byte), 1, 8).to_bytes(1, "big")
# gzip - write as bytes, read with decompression
with open("test.gz", "wb") as f:
f.write(result)
with gzip.open("test.gz", "r") as gf:
print(gf.read().decode())
os.remove("test.gz")