-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfenp.py
83 lines (80 loc) · 1.63 KB
/
fenp.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
from Pyfhel import Pyfhel,PyCtxt,PyPtxt
import numpy as np
import time
import operator
he=Pyfhel()
he.contextGen(p=1179649,m=32768,flagBatching=True,fracDigits=20)
he.keyGen()
a=PyCtxt()
b=PyCtxt()
q=0
#加密文件
def PPctxt(name):
global panduan
panduan=name
fil=open(name,mode='r',encoding='UTF-8')
lines=fil.readlines()
strr=''.join(lines)
global l
l=len(strr)
arr=[]
for line in strr:
for ind in line:
arr.append(ind)
#arr.remove('\n')
brr=[]
for i in arr:
brr.append(ord(i))
crr=np.array(brr)
#列表数据转数组
err=he.encryptBatch(crr)
err.save(name)
global e,f,g
# :'#'记录
ii=0
b=c=d=np.zeros((l,),dtype=int)
for i in arr:
if(i=='#'):
b[ii]=9
ii=ii+1
#print('b:::',b)
bb=np.array(b)
e=he.encryptBatch(bb)
#' '记录
iii=0
for i in arr:
if(i==' '):
c[iii]=12
iii=iii+1
cc=np.array(c)
f=he.encryptBatch(cc)
PPctxt('1.txt')
def slicing(name,cc):
#cc-选择从‘#’/‘ ’处分片
a.load(name,encoding='array')
#print('a:',type(a))
#print('e:',type(e))
if(cc=='#'):
bb=he.add(a,e)
bb.save(name)
elif(cc==' '):
b=he.add(a,f)
b.save(name)
else:
print('Error: \n mistake')
slicing('1.txt','#')
def Decrypt(name):
a.load(name,encoding='array')
b=he.decryptBatch(a)
b=b[:l]
#print(b)
brr=[]
for i in b:
brr.append(chr(i))
print(brr)
brr=''.join(brr)
#解密后明文存入文件
f=open(name,'w')
f.writelines(brr)
f.close()
Decrypt('1.txt')