python program to decrypt the ciphertext into plain text. This scheme of cipher uses a text string (say, a word) as a key, which is then used for doing a number of shifts on the plaintext. For example, let’s assume the key is ‘point’. Each alphabet of the key is converted to its respective numeric value: In this case
p→16,o→15,i→9, n→14, and t→20.
Thus, the key is: 16 15 9 14 20
The sender and the receiver decide on a key. Say ‘point’ is the key. Numeric representation of this key is [16, 15, 9, 14, 20]
The sender wants to decrypt the message, say ‘pHBNVI’.arrange cipher test and numeric key to get the plain text as "Attack" (Toggle Case of each letter)
p H B N V I
p-16 o-14 i-9 n-14 t-20 p-16
a t t a c k
now left shifts each ciphertext by alphabet by the number written below it to create plain text as shown below
Input format
pHBNVZ
point
Output Format
[16, 15, 9, 14, 20, 16]
Attack
from string import ascii_lowercase as ALPHABET
def shift(message, offset):
trans = str.maketrans(ALPHABET, ALPHABET[offset:] + ALPHABET[:offset])
return message.lower().translate(trans)
def decrypt(cipher):
key = "point"
numbers = []
for letter in key:
number = ord(letter) - 96
numbers.append(number)
print(numbers)
x = shift(cipher, -1)
return x
cipher = "pHBNVI"
print(decrypt(cipher))
Comments
Leave a comment