Answered step by step
Verified Expert Solution
Question
1 Approved Answer
ALPHABET = 0 1 2 3 4 5 6 7 8 9 ABCDEFGHIJKLMNOPQRSTUVWXYZ _ # message = ' CV 4 RS 7 KDAPWZCT
ALPHABET ABCDEFGHIJKLMNOPQRSTUVWXYZ#
message CVRSKDAPWZCTY#GDWUGYCDHWUBRYSSPJKGAIDUXSGSDYYEUWSGJAGUCYOFGWZGPMKDDWWYKZKNIZCCJWGBFWXUPK#VKGKWBGCZFSO#SJNSCZHWGSV#SDMSHUQYKWVKSO#WYDRZSYWNNZKGANIFGOUAIYVYPUQWBEKEYK#SIFGKR#EGLAXGFGXVWWSCYSPGXRGOACFRSKGAIFCTUSVKAWUBGQWIFGWGSWSWHCGODWYOGNSGWDWAPVYXKMCSGPZK#UGSCYJGSGWGSUYYHU#VKBGWAAKKAGOCJCYPUCCLWURTGMBSEIMUYOKGNIFGZSWAYOGXAGSP#WKRSYWKAWUBGSYSP#WKUKVWCOLKICGXRGDJIYHKOZGCCJNGGUJKXCSNSBOCEFKQYGRQMOZFSCWQUXZEKSO#PVNWDFYGGHAKFMCPGVSDOLDYGNGUZZWKJAIGMKVWBSEISUZOCKNIFGOOOIWTGXZFWBZCNSOJWWYKYPADOWLQZY#EGSZXKXCHSKZGRJKWCW#GQGRJKULAGNCXC#OLDYPZSWKGNICTKAXGAOBWLEKOZOLDYGQEGXEKDGNBMPYTG#FAXKRWUOJASGPYKHAGQQE#DAAGRJKWCLWBZCWKNAEYGOCFWBYIKCSKZGGVYVGDWXWQVOXAGSIUYHGDUISPGKCPIYMQJBHSKZGNGUZZWKWGMEIKBHEYHU#UWDOEUAWJHK#VKHGYGNCXCOLDYGGPTYQWHSLWU
def getOrdx:
Returns the order of the character in the new alphabet"""
if ordx ordA and ordx ordZ:
return ordx ordA
elif ordx ord and ordx ord:
return intx
elif x :
return lenALPHABET
else:
return
def addCharmarginals index, x:
Appends the character in the index's dictionary
if x in marginalsindex:
marginalsindexx
else:
marginalsindexx
def marginalFreql message:
Returns the marginal probability distributions
marginals for in rangel
for index, x in enumeratemessage:
addCharmarginals indexl x
return marginals
def distancemarginal:
Distance of a probability distribution"""
size
dist
for val in marginal.values:
size val
dist val
return distsize
def totalDistancemarginals:
Computes the global distance by summing
the distances of marginal distributions
globDist
for marginal in marginals:
globDist distancemarginal
return globDist
MAXLENGTH
def indexMaxlista:
'Returns the index of the maximum'
indexMax
maximum floatinf
if not lista:
return None
for index, val in enumeratelista:
if val maximum:
indexMax index
maximum val
return indexMax
def guessLengthmessage:
dist
marginals
for length in range MAXLENGTH :
marg marginalFreqlength message
marginals.appendmarg
dist.appendtotalDistancemarg
period indexMaxdist
return indexMaxdist marginalsperiod
def computeOffsetmarginal:
Computes the offset of a marginal distribution"""
sortedM sortedmarginalitems key lambda x: x
return getOrdsortedM lenALPHABET
def computeOffsetsmarginals:
Computes the offsets for the different marginals"""
offsets
for marginal in marginals:
offsets.appendcomputeOffsetmarginal
return offsets
def vigenereDecrptmessage:
Decrypts the message by:
Identifying the period and applying
ceasar decryption for each subset"""
# We need the period and the corresponding marginals
period, marginals guessLengthmessage
printperiod
offsets computeOffsetsmarginals
decrypted
for index, x in enumeratemessage:
decrypted ALPHABETgetOrdx offsetsindexperiodlenALPHABET
return decrypted
fp openplainTexttxtw
fpwritevigenereDecrptmessage
i am trying to decrypt a vigenere cipher. what am i doing wrong, because i am supposed to get an english plaintext. I haven't provided the entire ciphertext due to character limitation but the alphabet used are:
ABCDEFGHIJKLMNOPQRSTUVWXYZ#
where an underscore encodes a space and a hash character encodes a newline. Please fix the error and also provide the decoded plaintext, which should be english not gibbrish
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started