Last active
October 2, 2015 13:39
-
-
Save patleeman/9265e61c5a08c22ce5fa to your computer and use it in GitHub Desktop.
Basic vigenere's cipher encoder and decoder.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def main(): | |
action = 0 | |
cipherizer = """ | |
_____ _ _ _ ______ | |
/ __ (_) | | (_)|___ / | |
| / \/_ _ __ | |__ ___ _ __ _ / / ___ _ __ | |
| | | | '_ \| '_ \ / _ \ '__| | / / / _ \ '__| | |
| \__/\ | |_) | | | | __/ | | |./ /__| __/ | | |
\____/_| .__/|_| |_|\___|_| |_|\_____/\___|_| | |
| | | |
|_| """ | |
print(cipherizer) | |
print(' Welcome to CipheriZer v. 1.0') | |
print(' Please choose an option:') | |
print(' 1: Encode a message') | |
print(' 2: Decode a message') | |
print(' 3: Help') | |
print(' 4: Exit') | |
print('') | |
action = input('Choose an action: ') | |
while True: | |
if action == '1': | |
encode_get_message() | |
return False | |
#encode message fn | |
elif action == '2': | |
decode_get_message() | |
return False | |
#decode message fn | |
elif action == '3': | |
help() | |
return False | |
#print help | |
elif action == '4': | |
print('') | |
print('') | |
print('bye bye') | |
return False | |
else: | |
print('') | |
print('') | |
print('Please choose a valid option. ') | |
print('') | |
print('') | |
main() | |
#NUMBER CRUNCHER | |
#numbercruncher converts letters into a list of numbers | |
def numbercruncher(text): | |
dict = { | |
' ':0, | |
'a':1, | |
'b':2, | |
'c':3, | |
'd':4, | |
'e':5, | |
'f':6, | |
'g':7, | |
'h':8, | |
'i':9, | |
'j':10, | |
'k':11, | |
'l':12, | |
'm':13, | |
'n':14, | |
'o':15, | |
'p':16, | |
'q':17, | |
'r':18, | |
's':19, | |
't':20, | |
'u':21, | |
'v':22, | |
'w':23, | |
'x':24, | |
'y':25, | |
'z':26, | |
'A':27, | |
'B':28, | |
'C':29, | |
'D':30, | |
'E':31, | |
'F':32, | |
'G':33, | |
'H':34, | |
'I':35, | |
'J':36, | |
'K':37, | |
'L':38, | |
'M':39, | |
'N':40, | |
'O':41, | |
'P':42, | |
'Q':43, | |
'R':44, | |
'S':45, | |
'T':46, | |
'U':47, | |
'V':48, | |
'W':49, | |
'X':50, | |
'Y':51, | |
'Z':52, | |
'.':53, | |
'!':54, | |
'?':55, | |
'1':56, | |
'2':57, | |
'3':58, | |
'4':59, | |
'5':60, | |
'6':61, | |
'7':62, | |
'8':63, | |
'9':64, | |
'0':65} | |
textstring = list(text) | |
listlength = len(textstring) | |
output = [] | |
letter = '' | |
dictnumber = 0 | |
i = 0 | |
while i != listlength: | |
letter = textstring[i] | |
dictnumber = dict[letter] | |
output.append(dictnumber) | |
i += 1 | |
return output | |
#LETTER CRUNCHER | |
#lettercruncher converts numbers into a list of letters | |
def lettercruncher(numlist): | |
dict = { | |
0:' ', | |
1:'a', | |
2:'b', | |
3:'c', | |
4:'d', | |
5:'e', | |
6:'f', | |
7:'g', | |
8:'h', | |
9:'i', | |
10:'j', | |
11:'k', | |
12:'l', | |
13:'m', | |
14:'n', | |
15:'o', | |
16:'p', | |
17:'q', | |
18:'r', | |
19:'s', | |
20:'t', | |
21:'u', | |
22:'v', | |
23:'w', | |
24:'x', | |
25:'y', | |
26:'z', | |
27:'A', | |
28:'B', | |
29:'C', | |
30:'D', | |
31:'E', | |
32:'F', | |
33:'G', | |
34:'H', | |
35:'I', | |
36:'J', | |
37:'K', | |
38:'L', | |
39:'M', | |
40:'N', | |
41:'O', | |
42:'P', | |
43:'Q', | |
44:'R', | |
45:'S', | |
46:'T', | |
47:'U', | |
48:'V', | |
49:'W', | |
50:'X', | |
51:'Y', | |
52:'Z', | |
53:'.', | |
54:'!', | |
55:'?', | |
56:'1', | |
57:'2', | |
58:'3', | |
59:'4', | |
60:'5', | |
61:'6', | |
62:'7', | |
63:'8', | |
64:'9', | |
65:'0'} | |
numstring = list(numlist) | |
listlength = len(numstring) | |
output = [] | |
number = 0 | |
dictletter = '' | |
i = 0 | |
while i != listlength: | |
number = numstring[i] | |
dictnumber = dict[number] | |
output.append(dictnumber) | |
i += 1 | |
return output | |
#ENCODE | |
#encode_get_message will convert your message to a list of numbers, then pass it to the encode_msg fn. | |
def encode_get_message(): | |
try: | |
print('') | |
print('') | |
print(' The key and message are case sensitive!') | |
print(' Allowed symbols: .!?') | |
print('') | |
print('') | |
key = input('Please enter your private key: ') | |
text = input('Please enter your message: ') | |
keylength = len(key) | |
textlength = len(text) | |
i = 0 | |
j = 0 | |
longkey = '' | |
if keylength > textlength: | |
print('') | |
print(' The key must be shorter than the text') | |
print('') | |
encode() | |
else: | |
while i != textlength: | |
longkey = longkey + key[j] | |
i += 1 | |
j += 1 | |
j = j % keylength | |
encode_msg(longkey, text) | |
return longkey | |
except: | |
print('') | |
print('') | |
print(' !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!') | |
print(' There was a problem!') | |
print(' The only symbols allowed are !?.') | |
print(' Please try again') | |
print(' !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!') | |
print('') | |
encode_get_message() | |
#This does the encryption by adding the message and key. | |
def encode_msg(lk, txt): | |
keynum = numbercruncher(lk) | |
textnum = numbercruncher(txt) | |
cipher = [] | |
end = len(txt) | |
i = 0 | |
while i != end: | |
cipheredtext =(keynum[i]+textnum[i]) % 65 | |
cipher.append(cipheredtext) | |
i+=1 | |
output = ''.join(lettercruncher(cipher)) | |
print('') | |
print(' /////////////////////////////////') | |
print('') | |
print(' Here is your encrypted message:') | |
print(' %s' % output) | |
print('') | |
print(' /////////////////////////////////') | |
print('') | |
print('') | |
input('Press enter to continue') | |
main() | |
#DECODE | |
#This function asks for the encrypted text and key and converts it to a list of numbers. | |
def decode_get_message(): | |
try: | |
print('') | |
print('') | |
print(' The key and encrypted message are case sensitive!') | |
print(' Allowed symbols: .!?') | |
print('') | |
print('') | |
key = input('Please enter your private key: ') | |
text = input('Please enter your encrypted message: ') | |
keylength = len(key) | |
textlength = len(text) | |
i = 0 | |
j = 0 | |
longkey = '' | |
if keylength > textlength: | |
print('') | |
print(' The key must be shorter than the text') | |
print('') | |
encode() | |
else: | |
while i != textlength: | |
longkey = longkey + key[j] | |
i += 1 | |
j += 1 | |
j = j % keylength | |
decode_msg(longkey, text) | |
return longkey | |
except: | |
print('') | |
print('') | |
print(' !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!') | |
print(' There was a problem!') | |
print(' The only symbols allowed are !?.') | |
print(' Please try again') | |
print(' !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!') | |
decode_get_message() | |
#This function outputs the decoded message. | |
def decode_msg(lk, txt): | |
keynum = numbercruncher(lk) | |
textnum = numbercruncher(txt) | |
cipher = [] | |
end = len(txt) | |
i = 0 | |
while i != end: | |
cipheredtext = (textnum[i] - keynum[i]) % 65 | |
cipher.append(cipheredtext) | |
i+=1 | |
output = ''.join(lettercruncher(cipher)) | |
print('') | |
print(' /////////////////////////////////') | |
print('') | |
print(' Here is your decrypted message:') | |
print(' %s' % output) | |
print('') | |
print(' /////////////////////////////////') | |
print('') | |
input('Press enter to continue') | |
main() | |
def help(): | |
print('') | |
print('') | |
print(' How to use CipheriZer') | |
print('') | |
print(' To use this simple Vigeneres cipher:') | |
print(' 1: You and your friend agree on a private key. Preferably in person and in a safe location.') | |
print(' 2: To encrypt a message to send, type in the private key then your message') | |
print(' 3: Send the encrypted message to your friend') | |
print(' 4: Your friend can decrypt the message using the agreed upon private key') | |
print('') | |
print('') | |
print(' This program was created by Patrick Lee') | |
print(' http://www.patricklee.nyc') | |
print('') | |
print('') | |
print('') | |
input('Press enter to continue') | |
main() | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment