-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrsaCrypto.py
86 lines (64 loc) · 2.54 KB
/
rsaCrypto.py
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
from keysModels import RSAPublicKey, RSAPrivateKey
import math
import sys
def getMaxBitsDataSize(key):
return len(str(key.n)) - 3
def asciiToText(msg):
length, size = len(msg), 3
symbols = [ msg[i:i+size] for i in range(0, length, size)]
return ''.join(chr(int(s[0].strip('0') + s[1:])) for s in symbols).rstrip('\0')
def encrypt(msg, publicKey):
# hex = toHex(msg)
# for c in msg:
# print(str(ord(c)).zfill(3))
m = int(''.join(str(ord(c)).zfill(3) for c in msg))
# m = int(''.join(str(ord(c, 'x')) for c in msg))
# m = 3
print('message to ascii', m)
value = pow(m, publicKey.e, publicKey.n)
print('ENCRYPTED VALUE', value)
print('BIT LENGTH OF ENCRYPTED VALUE', int.bit_length(value))
return value
def decrypt(cipher, privateKey):
decryptedVal = str(pow(cipher, privateKey.d, privateKey.n))
if (len(decryptedVal) % 3 != 0):
decryptedVal = '0' + decryptedVal
print('DECRYPTED VALUE', decryptedVal)
return decryptedVal
def msgToAsciiValue(msg):
return ''.join(str(ord(c)).zfill(3) for c in msg)
def splitToBlocks(asciiMsg, size):
length = len(asciiMsg)
blocks = [ asciiMsg[i:i+size].ljust(size, '0') for i in range(0, length, size)]
return blocks
def encryptBlock(block, publicKey):
val = pow(block, publicKey.e, publicKey.n)
return val
def encryptBlockMessage(blocks, publicKey):
return ''.join(str(encryptBlock(int(block), publicKey)) + '\n' for block in blocks).rstrip('\n')
def encryptLargeFile(msg, publicKey):
bits = getMaxBitsDataSize(publicKey)
asciiMsg = msgToAsciiValue(msg)
blocks = splitToBlocks(asciiMsg, bits)
encryptedMsg = encryptBlockMessage(blocks, publicKey)
return encryptedMsg
def decryptBlock(block, privateKey):
bits = getMaxBitsDataSize(privateKey)
decryptedVal = str(pow(block, privateKey.d, privateKey.n))
return decryptedVal.zfill(bits)
def decryptBlockMessage(blocks, privateKey):
decrypted, blocksAmount = '', len(blocks)
print('\r0%', end='')
for i, block in enumerate(blocks):
decrypted = decrypted + str(decryptBlock(int(block), privateKey))
progress = str(math.floor((i/blocksAmount)*100))
print('\r' + progress + '%', end='')
print('\r100%', end='')
return decrypted
# return ''.join(str(decryptBlock(int(block), privateKey)) for block in blocks)
def decryptLargeFile(msg, privateKey):
length = len(msg)
blocks = msg.split('\n')
decryptedMsg = decryptBlockMessage(blocks, privateKey)
text = asciiToText(decryptedMsg)
return text