@tojira/

RSA (python)

Python

No description

fork
loading
Files
  • main.py
  • decrypt.py
  • encrypted.txt
  • requirements.txt
main.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
from random import randint
from sympy import mod_inverse, gcd, nextprime, lcm

text = input(':')
values = [ord(i) for i in list(text)]

print('Generating first prime number...')
P = nextprime(randint(10**99, 10**100 - 1000))
print('Generating second prime number...')
Q = nextprime(randint(10**99, 10**100 - 1000))
print('Multiplying...')
n = P * Q
phi = int(lcm(P - 1, Q - 1))

print('Generating public key...')
while 'e' not in locals() or gcd(e, phi) != 1:
    e = randint(2, phi - 1)

print('Generating private key...')
d = mod_inverse(e, phi)

encrypted = list()
k = 1
for i in values:
    print('Encrypting {}/{}...'.format(k, len(values)))
    encrypted.append(str(pow(i, d, n)))
    k += 1

print('Making file...')
with open('encrypted.txt', 'w') as file:
    file.write(','.join(encrypted))
    file.write('\ne={}\nn={}'.format(e, n))
print('Success')