Question: import sys import numpy as np from collections import Counter from math import gcd from BitVector import * if len ( sys . argv )

import sys
import numpy as np
from collections import Counter
from math import gcd
from BitVector import *
if len(sys.argv)!=3:
sys.exit('''Needs two command-line arguments, one for the message file and the other for the encrypted output file''')
def kasiski_examination(ciphertext):
distances =[]
for i in range(len(ciphertext)-3):
substring = ciphertext[i:i+3]
occurrences =[pos for pos, char in enumerate(ciphertext) if ciphertext[pos:pos+3]== substring]
if len(occurrences)>1:
distances.extend([occurrences[j]-occurrences[j-1] for j in range(1, len(occurrences))])
return distances
def find_key_length(distances):
distance_counter = Counter(distances)
possible_lengths =[length for length, count in distance_counter.items() if count >1]
if not possible_lengths:
return None
key_length = gcd(*possible_lengths)
return key_length
def guess_key_part(block, most_frequent_char):
xor_value = int(most_frequent_char, 2)^ int('00100000',2)
temp_key_part = bin(xor_value)[2:].zfill(len(most_frequent_char))
return temp_key_part
def find_most_frequent(vigenere_row):
frequency_dict ={}
for char in vigenere_row:
frequency_dict[char]= frequency_dict.get(char,0)+1
return max(frequency_dict, key=frequency_dict.get)
PassPhrase = "I want to learn cryptograph and network security"
BLOCKSIZE =64
numbytes = BLOCKSIZE //8
bv_iv = BitVector(bitlist=[0]* BLOCKSIZE)
for i in range(0, len(PassPhrase)// numbytes):
textstr = PassPhrase[i * numbytes:(i +1)* numbytes]
bv_iv ^= BitVector(textstring=textstr)
with open(sys.argv[1],'r') as inFile:
cipherbin = BitVector(hexstring=inFile.readline().rstrip('
'))
# Determine key length using Kasiski examination
distances = kasiski_examination(cipherbin.get_bitvector_in_hex())
key_length = find_key_length(distances)
# Print the correct key length
print("Determined Key Length:", key_length)
# Frequency analysis to find key
cipherBlockList =[]
for i in range(0, len(cipherbin), BLOCKSIZE):
block = BitVector(bitstring=cipherbin[i: i + BLOCKSIZE])
cipherBlockList.append(block)
cipherBlockList.insert(0, bv_iv)
plainXorKey =[]
for i in range(1, len(cipherBlockList)):
xor_result = cipherBlockList[i]^ cipherBlockList[i -1]
plainXorKey.append(xor_result)
plainXorKey = list(reversed(plainXorKey))
NUM_ROWS = BLOCKSIZE //8
table =[[] for _ in range(NUM_ROWS)]
for xor_value in reversed(plainXorKey):
xor_str = str(xor_value)
for k in range(0, len(xor_str), numbytes):
table[k //8].append(xor_str[k: k + numbytes])
decryption_key =""
for row in table:
most_frequent_char = find_most_frequent(row)
temp_key_part = guess_key_part(row, most_frequent_char)
decryption_key += temp_key_part
# Convert binary key to ASCII string
ascii_key =''.join([chr(int(decryption_key[i:i+8],2)) for i in range(0, len(decryption_key),8)])
# Write the key to the "key.txt" file
with open('key.txt','w') as key_file:
key_file.write(ascii_key)
# Decrypt using the found key
key_bv = BitVector(bitstring=decryption_key)
msg_decrypted_bv = BitVector(size=0)
previous_decrypted_block = bv_iv
for i in range(0, len(cipherbin)// BLOCKSIZE):
bv = cipherbin[i * BLOCKSIZE:(i +1)* BLOCKSIZE]
temp = bv.deep_copy()
bv ^= previous_decrypted_block
previous_decrypted_block = temp
bv ^= key_bv
msg_decrypted_bv += bv
outputtext = msg_decrypted_bv.get_text_from_bitvector()
with open(sys.argv[2],'w', encoding='latin-1', errors='replace') as FILEOUT:
FILEOUT.write(outputtext)
print("Key has been written to 'key.txt' file.") explain this code with proper example each line

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Finance Questions!