-
Notifications
You must be signed in to change notification settings - Fork 0
/
rsa.py
73 lines (55 loc) · 1.82 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
69
70
71
72
73
#!/usr/bin/python2
# -*- coding: utf-8 -*-
import functions as func
import time
import os
## INPUT:
# rsa (text, [enc/dec - 1/0],[enc/dec settings 1 - manual, 0 - auto], p, q)
# 1: text - input text (letters)
# 2: method - number '1' if encode
# - number '0' if decode
# 3 4 5:- parametres p and q, e.
## OUTPUT:
# 1: array of int numbers
def rsa(text,method,p,q,e):
n = p*q
key = (p-1)*(q-1)
tmp = func.egcd(e,key)
d = int(tmp[1])
# encode = 1
if method == 1:
start_time = time.time()
size = len(text)
print "[ RSA ] text lenght:", size
text_int = []
i = 0
while i < size:
text_int.append(ord(text[i]))
i = i+1
text_crypt = []
i = 0
while i < size:
text_crypt.append(int(text_int[i] ** e % n))
i = i+1
print "[ RSA ] Encrypted:", text_crypt
finish_time = time.time()-start_time
text_crypt = ' '.join([str(i) for i in text_crypt])
print "[ RSA ] Time elapsed:",finish_time, "ms"
return [finish_time,text_crypt]
# decode = 0
if method == 0:
print text
text = text.split(' ')
size = len(text)
print "[ RSA ] text length:",size
text_decrypt = []
start_time = time.time()
i = 0
while i < size:
text_decrypt.append(chr(int(int(text[i]) ** d % n)))
i = i+1
print "[ RSA ] Decrypted:",text_decrypt
finish_time = time.time()-start_time
print "[ RSA ] Time elapsed:",finish_time, "ms"
text_decrypt = ''.join([str(i) for i in text_decrypt])
return [finish_time,text_decrypt]