-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathencode.py
56 lines (49 loc) · 988 Bytes
/
encode.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
import MyMath
import MyBase
def getPublicKey (file) :
fi = open(file,"r")
n = int(fi.readline())
e = int(fi.readline())
fi.close()
return n, e
def getPlaintext (file):
fi = open(file,"r")
P = fi.read()
fi.close()
return P
def convertStringToInt(P, base):
R = []
for i in P:
c = str(ord(i))
while len(c) != base:
c = '0' + c #0000
R.append(c)
return R
def createBigInt(R, size_n):
A = []
x = ""
for i in R:
if len(x) + len(i) >= size_n: # Tối ưu mã hóa nhiều kí tự nhất có thể
A.append(int(x))
x = ""
x+= i
A.append(int(x))
return A
def encode(n, e, P, file):
fo = open(file,"w")
C = ""
R = convertStringToInt(P, 4)
A = createBigInt(R, len(str(n)))
for i in A:
M = MyMath.powMod(i,e,n)
M = MyBase.toBase(M,64)
C+= M + ' '
fo.write(M+' ')
fo.close()
return C
def main():
n, e = getPublicKey("Data/PublicKey.txt")
P = getPlaintext("Data/Plaintext.txt")
C = encode(n, e, P, "Data/Ciphertext.txt")
#print (C)
main()