A --> C
E --> G
Here we can see that if all 'A' is 'C' and all 'E' is 'G' then we can count the shift and tell this is a shift 2 cipher. This isn't too hard to calculate by hand however when the text becomes longer and the shifts are much larger this process can become very tedious. Here I used a Python script as it is quick and easy to perform what we are looking for.
#!/usr/bin/python
from string import maketrans
plaintext = raw_input("Enter your text: ")
shift_num = int(raw_input("Enter number to shift by: "))
alphabet = 'abcdefghijklmnopqrstuvwxyz'
code_string = alphabet[shift_num:] + alphabet[:shift_num]
encode = maketrans(alphabet + alphabet.upper(), code_string + code_string.upper())
print '\n'
print "Encoded text is: " + plaintext.translate(encode) The code_string line will rearrange the alphabet to look like the cipher alphabet making translation easier. The maketrans function will then replace each character in the string or text provided with its equal in the shift alphabet. the alphabet.upper() is to make sure that upper and lower case characters don't break the script. An additional challenge for this script might be to add a decode function. This can be done by checking the frequency of occurring letters in the cipher text and then making a guess at the number used for the shift in the cipher.
0 comments:
Post a Comment