Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

ALPHABET = 0 1 2 3 4 5 6 7 8 9 ABCDEFGHIJKLMNOPQRSTUVWXYZ _ # message = ' CV 4 RS 7 KDAPWZCT

ALPHABET ="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_#"
message ='CV4RS7KDAPWZCT4Y#GD56W34UGY1C8D2HWU3B0RY00_1S694SPJ854KG6AI_D0UXSG83SDY7YEUW1S76GJAGUCY8O59FG6WZGPM854KDD6WWYKZ803K56NIZCCJ8WGB_FWX4UP4K#VK9GK2WB0G1CZ_FSO#SJN4SCZ_9HWG4S0V#S9DMSHU8Q0YKWVK6SO#WYDR24Z_1S9YWNN280ZKGANI8FG4O9U5_AIYVYPU8QWB2EK5EYK4#S55669I8FG4KR#EGLAXGFGX8VWW1SCYSP0GXRG2O9A35CF4RS7KG9AI9FCTUSVKA6WU3B0G0QW7169I9FG405W9GSW4SW07HCG2O96DWYOGNSGW9DWA4PVY80XK_4MCSGPZK#U_GS7C9YJG3SGWGSUY9YHU2#VKBGWA4AKK1AG2OCJ6CYPU1CC1_LWU9RTGM5_8BSEI5MUYO48K3GNI9FG4ZS4996WA4YOGXAG83SP#WK0RSYWKA6WU3B0G15SY5SP#WKUKV7WCOLKI2C0GXRGD56JI_YHKOZGCCJNGGUJKXC912SN6SB00OC5EFKQYGRQMO5Z_FSC6WQ0UXZEK_SO#4PV4NW8D_F8YGGH460AK6FMC_PG44VSDOLDYGNGUZZWK_JAI1GMK8VWB2SEI2SUZ8O3CK6NI9FG40O2_O2OIWTGX68Z_F6WBZC0N2_S7OJ6WWYKY8PADO2W44LQZY#4EGS6ZX8KX8_4CHSKZGRJKWC1W#GQ9GRJK8U7__LA9GNCX1C4#OLDY_P0ZS_WK3GNI2CTK84AXGAOBWLEK8O3ZOLDYGQEGX5EKDGNB_MP4YTG#F6AX4K00RWU1OJA4SGPY854KH9A4GQQ4134E#DAAGRJKWC91_LWBZC_424WK2NA9EYG1O392CFWB4YIK1C721SKZGGV4YVGD56WXWQVOXAG83SIU3Y0H25GD56UISPG4KC7249PIYMQJ844BHSKZGNGUZZWK67W0GMEIK4_8B2H3EYHU#UWDOEUAWJH4K#VKH2GYGNCX1C_7OLDYGGPTYQW7HSL3W8U_'
def getOrd(x):
""" Returns the order of the character in the new alphabet"""
if ord(x)>= ord("A") and ord(x)<= ord("Z"):
return ord(x)- ord("A")+10
elif ord(x)<= ord("9") and ord(x)>= ord("0"):
return int(x)
elif x =="_":
return len(ALPHABET)-1
else:
return -1
def addChar(marginals, index, x):
"""
Appends the character in the index's dictionary
"""
if x in marginals[index]:
marginals[index][x]+=1
else:
marginals[index][x]=1
def marginalFreq(l, message):
"""
Returns the marginal probability distributions
"""
marginals =[{} for _ in range(l)]
for index, x in enumerate(message):
addChar(marginals, index%l, x)
return marginals
def distance(marginal):
""" Distance of a probability distribution"""
size =0
dist =0
for val in marginal.values():
size += val
dist += val**2
return dist/(size**2)
def totalDistance(marginals):
""" Computes the global distance by summing
the distances of marginal distributions """
globDist =0
for marginal in marginals:
globDist += distance(marginal)
return globDist
MAX_LENGTH =25
def indexMax(lista):
'Returns the index of the maximum'
indexMax =0
maximum =-float("inf")
if not lista:
return None
for index, val in enumerate(lista):
if val > maximum:
indexMax = index
maximum = val
return indexMax
def guessLength(message):
dist =[]
marginals =[]
for length in range(1, MAX_LENGTH +1):
marg = marginalFreq(length, message)
marginals.append(marg)
dist.append(totalDistance(marg))
period = indexMax(dist)
return indexMax(dist), marginals[period]
def computeOffset(marginal):
""" Computes the offset of a marginal distribution"""
sortedM = sorted(marginal.items(), key= lambda x: -x[1])
return getOrd(sortedM[0][0])- len(ALPHABET)+1
def computeOffsets(marginals):
""" Computes the offsets for the different marginals"""
offsets =[]
for marginal in marginals:
offsets.append(computeOffset(marginal))
return offsets
def vigenereDecrpt(message):
""" Decrypts the message by:
Identifying the period and applying
ceasar decryption for each subset"""
# We need the period and the corresponding marginals
period, marginals = guessLength(message)
print(period)
offsets = computeOffsets(marginals)
decrypted =''
for index, x in enumerate(message):
decrypted += ALPHABET[(getOrd(x)- offsets[index%(period+1)])%len(ALPHABET)]
return decrypted
fp = open("plainText.txt","w")
fp.write(vigenereDecrpt(message))
- 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:
0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_#
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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

OCA Oracle Database SQL Exam Guide Exam 1Z0-071

Authors: Steve O'Hearn

1st Edition

1259585492, 978-1259585494

More Books

Students also viewed these Databases questions

Question

Explain the function and purpose of the Job Level Table.

Answered: 1 week ago