-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRSA.py
More file actions
27 lines (20 loc) · 857 Bytes
/
RSA.py
File metadata and controls
27 lines (20 loc) · 857 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
def encrypt(message,n,e, block_size=2):
encrypted_blocks = []
ciphertext = -1
if (len(message) > 0):
ciphertext = ord(message[0])
for i in range(1, len(message)):
if (i % block_size == 0):
encrypted_blocks.append(ciphertext)
ciphertext = 0
# multiply by 1000 to shift the digits over to the left by 3 places
# because ASCII codes are a max of 3 digits in decimal
ciphertext = ciphertext * 1000 + ord(message[i])
# add the last block to the list
encrypted_blocks.append(ciphertext)
# encrypt all of the numbers by taking it to the power of e
# and modding it by n
for i in range(len(encrypted_blocks)):
encrypted_blocks[i] = str((encrypted_blocks[i]**e) % n)
encrypted_message = "".join(encrypted_blocks)
return str(encrypted_message)