-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRSAEncode.py
More file actions
38 lines (31 loc) · 1023 Bytes
/
RSAEncode.py
File metadata and controls
38 lines (31 loc) · 1023 Bytes
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
# RSA for encrypting text messages
# note that in order to make the message resistant to frequency analysis, it
# is best to use a lot of spaces, numbers and special characters!
# Large numbers will take a long time to calculate!
# function to encrypt an ascii value - we must carry out
# the exponentiation manually or else the values will
# lose precision!
def encrypt(m):
E = m
count = 1
while (count<e):
E = (m*e)%(n)
count = count + 1
return E
# enter the public key:
# enter the modulus
n = input('Please enter the modulus')
# enter the number e
e = input('Please enter the other component of the public key - the number e')
# enter the message
M = raw_input('Please enter the text you wish to encrypt')
E = ""
# read in the message character by charater, converting to ascii values and
# then encrypting
for i in range(len(M)):
ascii = ord(M[i:i+1])
print(ord(M[i:i+1]))
E = E + str(encrypt(ascii)) + " "
# print encrypted text
print('Your message is:')
print(E)