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 lensysargv:
sysexitNeeds two commandline arguments, one for the message file and the other for the encrypted output file'
def kasiskiexaminationciphertext:
distances
for i in rangelenciphertext:
substring ciphertexti:i
occurrences pos for pos, char in enumerateciphertext if ciphertextpos:pos substring
if lenoccurrences:
distances.extendoccurrencesjoccurrencesj for j in range lenoccurrences
return distances
def findkeylengthdistances:
distancecounter Counterdistances
possiblelengths length for length, count in distancecounter.items if count
if not possiblelengths:
return None
keylength gcdpossiblelengths
return keylength
def guesskeypartblock mostfrequentchar:
xorvalue intmostfrequentchar, int
tempkeypart binxorvalue:zfilllenmostfrequentchar
return tempkeypart
def findmostfrequentvigenererow:
frequencydict
for char in vigenererow:
frequencydictchar frequencydict.getchar
return maxfrequencydict, keyfrequencydict.get
PassPhrase "I want to learn cryptograph and network security"
BLOCKSIZE
numbytes BLOCKSIZE
bviv BitVectorbitlist BLOCKSIZE
for i in range lenPassPhrase numbytes:
textstr PassPhrasei numbytes:i numbytes
bviv BitVectortextstringtextstr
with opensysargvr as inFile:
cipherbin BitVectorhexstringinFile.readlinerstrip
# Determine key length using Kasiski examination
distances kasiskiexaminationcipherbingetbitvectorinhex
keylength findkeylengthdistances
# Print the correct key length
printDetermined Key Length:", keylength
# Frequency analysis to find key
cipherBlockList
for i in range lencipherbin BLOCKSIZE:
block BitVectorbitstringcipherbini: i BLOCKSIZE
cipherBlockList.appendblock
cipherBlockList.insert bviv
plainXorKey
for i in range lencipherBlockList:
xorresult cipherBlockListi cipherBlockListi
plainXorKey.appendxorresult
plainXorKey listreversedplainXorKey
NUMROWS BLOCKSIZE
table for in rangeNUMROWS
for xorvalue in reversedplainXorKey:
xorstr strxorvalue
for k in range lenxorstr numbytes:
tablek appendxorstrk: k numbytes
decryptionkey
for row in table:
mostfrequentchar findmostfrequentrow
tempkeypart guesskeypartrow mostfrequentchar
decryptionkey tempkeypart
# Convert binary key to ASCII string
asciikey joinchrintdecryptionkeyi:i for i in range lendecryptionkey
# Write the key to the "key.txt file
with openkeytxtw as keyfile:
keyfile.writeasciikey
# Decrypt using the found key
keybv BitVectorbitstringdecryptionkey
msgdecryptedbv BitVectorsize
previousdecryptedblock bviv
for i in range lencipherbin BLOCKSIZE:
bv cipherbini BLOCKSIZE:i BLOCKSIZE
temp bvdeepcopy
bv previousdecryptedblock
previousdecryptedblock temp
bv keybv
msgdecryptedbv bv
outputtext msgdecryptedbvgettextfrombitvector
with opensysargvw encoding'latin errors'replace' as FILEOUT:
FILEOUT.writeoutputtext
printKey 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
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
