-
Notifications
You must be signed in to change notification settings - Fork 0
/
RSA.py
68 lines (58 loc) · 1.62 KB
/
RSA.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
from math import gcd
N = 329159
e = 7 #public
d = 46543 #secret
p = 101
q = 3259
'''
N = p * q (coprime)
L = lcm(p-1, q-1)
e which gcd(E, L) = 1
d which (E, D) mod L = 1
encryption : Crypto = Plain^e mod N
pow(P, E, N) => C
decryption : Plain = Crypto^d mod N
pow(C, D, N) => P
'''
plain = 'Please encrypt me!'
dec_text = [253630, 393, 27573, 318532, 209682, 27573, 146994, 27573, 181510, 94307, 287693, 141332, 193545, 179374, 146994, 25541, 27573, 252293]
def encrypt_before(plaintext, public):
enc_text = [ord(char) for char in plaintext]
binary_e = format(public, 'b')
return(enc_text, binary_e)
def r_enc(enc_text, binary_e):
T = [1] * len(enc_text)
flage = 0
for it in enc_text:
for jt in reversed(binary_e):
T[flage] = (T[flage] * T[flage]) % N
if jt == '1':
T[flage] = (it * T[flage]) % N
flage += 1
print(T)
return T
def decrypt_before(d):
binary_d = format(d, 'b')
return(binary_d)
def r_dec(dec_text, binary_d):
S = [1] * len(dec_text)
flagd = 0
for i in dec_text:
for j in (binary_d):
S[flagd] = (S[flagd] * S[flagd]) % N
if j == '1':
S[flagd] = (i * S[flagd]) % N
b = S[flagd]
for a in range(10000000):
( i * b )% N
print("Y")
else:
print("N")
flagd += 1
return S
#(enc_text, binary_e) = encrypt_before(plain, e)
#dec_text = r_enc(enc_text, binary_e)
#print(dec_text)
binary_d = decrypt_before(d)
decrypted_text = r_dec(dec_text, binary_d)
print(decrypted_text)