-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRSA Program
More file actions
143 lines (120 loc) · 3.16 KB
/
RSA Program
File metadata and controls
143 lines (120 loc) · 3.16 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
"""
RSA Program for MAT-230 Discrete Math final discussion.
"""
import math
# Encryption function
def encryption():
print("encryption")
word = input('Phrase to be encrypted: ')
word = word.lower()
wl = []
space = 32
period = 27
comma = 28
question = 29
exclamation = 30
for letter in word:
if letter == ' ':
wl.append(space)
elif letter == '.':
wl.append(period)
elif letter == ',':
wl.append(comma)
elif letter == '?':
wl.append(question)
elif letter == '!':
wl.append(exclamation)
else:
wl.append(ord(letter) - 96)
print('Phrase input to numbers: ', *wl)
encrypt_list_short = []
encrypt_list_long = []
p = 3
q = 11
e = 3
n = p * q
def encrypt(me):
global encrypt_list_long
en = math.pow(me, e)
c = en % n
c = int(c)
encrypt_list_short.append("%02d" % c)
encrypt_list_long = [' '.join(encrypt_list_short)]
return c
# run function on int list
for item in wl:
c = encrypt(item)
print('Encrypted phrase: ', *encrypt_list_long)
main ()
# Dncryption Function
def decryption():
print("decryption")
# Takes input and makes list of integers
d_list = [int(item) for item in input("Enter the message to be decrypted: ").split(' ')]
decrypted_list_short = []
decrypted_list_long = []
message = []
space = '32'
period = '27'
comma = '28'
question = '29'
exclamation = '30'
p = 3
q = 11
e = 3
d = 7
n = p * q
# Outputs a string
def decrypt(c):
global decrypted_list_long
en = math.pow(c, d)
m = en % n
m = int(m)
decrypted_list_short.append("%02d" % m)
decrypted_list_long = [' '.join(decrypted_list_short)]
return m
for item in d_list:
m = decrypt(item)
for item in decrypted_list_short:
if item == space:
message.append(' ')
elif item == period:
message.append('.')
elif item == comma:
message.append(',')
elif item == question:
message.append('?')
elif item == exclamation:
message.append('!')
else:
letter = chr(int(item) + 96)
letter = letter.upper()
message.append(letter)
print("Original Message is: ", *d_list)
print('Decrypted Message numbers: ', *decrypted_list_long)
print('Secret message: ', ''.join(message))
main ()
# Menu function
def menu():
print('{:*^20}'.format('*'))
print('{:-^20}'.format('Menu'))
print("What would you like to do?")
print("[1] Encryption")
print("[2] Decryption")
print("[3] Exit")
print('{:*^20}'.format('*'))
# Program runs here
def main():
menu()
user_choice = int(input(">"))
while user_choice != 3:
if user_choice == 1:
encryption()
elif user_choice == 2:
decryption()
else:
print("Invalid choice, try again.")
user_choice = int(input(">"))
print("Exiting now, goodbye!")
quit()
main()