-
Notifications
You must be signed in to change notification settings - Fork 1
/
code.py
89 lines (61 loc) · 1.96 KB
/
code.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
74
75
76
77
78
79
80
81
82
83
84
85
from speedyfunctions import fast, egcd, modinv
from math import gcd
def pospair(c): #Return the line,position pair assosciated with char c
for i in range(0, lenKey): #loop through the lines of the key
if c in linesKey[i]: #if c appears in the line
linesKey[i].index(c)
return [i,linesKey[i].index(c)]
return 0
def encrypt(i,e,n): #i^e mod n encryption
return pow(i,e,n)
def decrypt(j,d,n): #use modular inverse of e to decrypt
return pow(j,d,n)
#Read Input File
file = open("code.param","r")
lines = file.readlines()
#Read first line containing 3 ints: p,q,e
numbers = [int(n) for n in lines[0].split()]
if len(numbers) != 3:
raise Exception("Input File Error - First line")
p,q,e = numbers
#read file named in second line of Input File: string to be encoded
fileinput1 = open(lines[1].rstrip('\n'),"r")
code = fileinput1.read()
fileinput1.close()
#read file named in third line of Input file: string to be used as encoding key
fileinput2 = open(lines[2].rstrip('\n'),"r")
linesKey = fileinput2.readlines()
lenKey = len(linesKey)
fileinput2.close()
file.close()
######################################
## RSA ALGORITHM ##
######################################
#Large Primes
#p = 1300051
#q = 17
#Product of Large Primes
n = p * q
#Euler totient
phi = n - p - q + 1 #=(p1-1)(p2-1)
#print(phi)
d = modinv(e,phi)
########################################
#Turn codetext into pairs of positions in keytext
positions = []
for c in code: #loop through characters and put their position pairs in a list
positions.append(pospair(c))
#print(positions)
encryptedpos = []
for i in positions: #loop through positions and encrypt them
temp = []
for j in i:
temp.append(encrypt(j,e,n))
encryptedpos.append(temp)
#print(encryptedpos)
fileoutput = open("code.out","w")
#Write the encrypted positions to the file
for i in encryptedpos:
fileoutput.write(str(i) + "\n")
print("Done")
fileoutput.close()