-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRSA.py
More file actions
74 lines (53 loc) · 1.41 KB
/
RSA.py
File metadata and controls
74 lines (53 loc) · 1.41 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
import math
import random
def prime(n):
for i in range(2, math.floor(math.sqrt(n))):
if n % i == 0: return False
return True
def gcd(a, b):
while (b > 0): a, b = b, a % b
return a
def modulo_inverse(e, max):
og, track, key = max, 0, 1
if (max == 1): return 0
while (e > 1):
div, temp = e // max, max
max = e % max
e = temp
temp = track
track = key - div * track
key = temp
if (key <= -1): key += og
return key
def modulo(txt, key, n):
ed, p = 1, txt % n
for i in range(32):
if (0x00000001 & (key >> i) == 1): ed = (ed * p) % n
p = (p ** 2) % n
return ed
def gen():
primes = [i for i in range(2, 1000) if prime(i)]
p, q = 0, 0
while (p == q):
p = random.choice(primes)
q = random.choice(primes)
n = p * q
phi = (p - 1) * (q - 1)
es = []
for i in range (1, phi):
if (gcd(i, phi) == 1): es.append(i)
e = random.choice(es)
d = modulo_inverse(e, phi)
return p, q, e, d, n
def generateKeys():
p, q, e, d, n = 0, 0, 0, 0, 0
while(e == d): p, q, e, d, n = gen()
return e, n, d, n
def encrypt(msg, e, n):
cipher = []
for i in range(len(msg)): cipher.append(modulo(ord(msg[i]), e, n))
return cipher
def decrypt(cipher, d, n):
true_t = ''
for i in cipher: true_t += chr(modulo(i, d, n))
return true_t