-
Notifications
You must be signed in to change notification settings - Fork 0
/
OTP Super Secure Secret Messaging
94 lines (75 loc) · 2.62 KB
/
OTP Super Secure Secret Messaging
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
from random import randint
ALPHABET = 'abcdefghijklmnopqrstuvwxyz'
def generate_otp(sheets, length):
for sheet in range(sheets):
with open("otp" + str(sheet) + ".txt", "w") as f:
for i in range(length):
f.write(str(randint(0, 26)) + "\n")
def load_sheet(filename):
with open(filename, "r") as f:
contents = f.read().splitlines()
return contents
def get_plaintext():
plaintext = input('Please type your message ')
return plaintext.lower()
def load_file(filename):
with open(filename, "r") as f:
contents = f.read()
return contents
def save_file(filename, data):
with open(filename, 'w') as f:
f.write(data)
def encrypt(plaintext, sheet):
ciphertext = ''
for position, character in enumerate(plaintext):
if character not in ALPHABET:
ciphertext += character
else:
encrypted = (ALPHABET.index(character) + int(sheet[position])) % 26
ciphertext += ALPHABET[encrypted]
return ciphertext
def decrypt(ciphertext, sheet):
plaintext = ''
for position, character in enumerate(ciphertext):
if character not in ALPHABET:
plaintext += character
else:
decrypted = (ALPHABET.index(character) - int(sheet[position])) % 26
plaintext += ALPHABET[decrypted]
return plaintext
def menu():
choices = ['1', '2', '3', '4']
choice = '0'
while True:
while choice not in choices:
print('What would you like to do?')
print('1. Generate one-time pads')
print('2. Encrypt a message')
print('3. Decrypt a message')
print('4. Quit the program')
choice = input('Please type 1, 2, 3 or 4 and press Enter⇴ ')
if choice == '1':
sheets = int(
input('How many one-time pads would you like to generate?⇒ '))
length = int(input('What will be your maximum message length?⇝ '))
generate_otp(sheets, length)
elif choice == '2':
filename = input('Type in the filename of the OTP you want to use➠ ')
sheet = load_sheet(filename)
plaintext = get_plaintext()
ciphertext = encrypt(plaintext, sheet)
filename = input('What will be the name of the encrypted file?➾ ')
save_file(filename, ciphertext)
elif choice == '3':
filename = input('Type in the filename of the OTP you want to use➳ ')
sheet = load_sheet(filename)
filename = input('Type in the name of the file to be decrypted➻ ')
ciphertext = load_file(filename)
plaintext = decrypt(ciphertext, sheet)
print('The message reads:')
print('')
print(plaintext)
elif choice == '4':
exit()
choice = '0'
menu()