aboutsummaryrefslogtreecommitdiff
path: root/final/RSA.py
blob: 82a32113e188003d4793173f0b57e2de6ce93989 (plain)
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
import random
from math import gcd as bltin_gcd
def isPrime(x):
    pr = True
    for j in range(2, x):
        if x % j == 0:
            pr = False
    return pr
def remove_char(input_string, index):
    first_part = input_string[:index]
    second_part - input_string[index+1:]
    return first_part + second_part
def coprime(a, b):
    return bltin_gcd(a, b) == 1

primes = [i for i in range(2,1000) if isPrime(i)]
p = random.choice(primes)
q = random.choice(primes)
n = p * q
coprimes = [j for j in range(2,n) if coprime(j, ((p-1)*(q-1)))]
e = random.choice(coprimes)
string = input()
sepChars = list(string)
numChars = 0

for c in sepChars:
    numChars += ((128**sepChars.index(c))*ord(c))
print("numChars:",numChars)
c = 0
blocks = []

while numChars > 0:
    blocks.append(numChars%n)
    numChars = (numChars-numChars%n)/n

print("Blocks:",blocks)
encryptedBlocks = []
for b in blocks:
    encryptedBlocks.append((b**e)%n)
print("Encrypted:",encryptedBlocks)