-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRSA.py
More file actions
48 lines (39 loc) · 889 Bytes
/
RSA.py
File metadata and controls
48 lines (39 loc) · 889 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
39
40
41
42
43
44
45
46
47
48
from Crypto.Util import number
import gmpy2
def int_hex(n):
temp=hex(n)[2:].strip('L')
return '0'*(len(temp)%2) + temp
class RSAKey(object):
def __init__(self,bits=1024,e=3):
while True:
try:
p=number.getPrime(bits/2)
q=p
while p==q:
q=number.getPrime(bits/2)
self.n=p*q
self.e=e
self.bits=bits
et=(p-1)*(q-1)
self.d=int(gmpy2.invert(self.e,et))
break
except:
pass
def encrypt(self,m):
m=int(m.encode('hex'),16)
return pow(m,self.e,self.n)
def decrypt(self,c):
m = pow(c,self.d,self.n)
return int_hex(m).decode('hex')
def send_public(self):
return self.n,self.e
def send_private(self):
return self.n,self.d
def parity_oracle(self,c):
return pow(c,self.d,self.n)%2
def PKCS_oracle(self,c):
temp = self.decrypt(c)
if temp[0]=='\x02' and len(temp)==self.bits/8-1:
return True
else:
return False