-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpinjie_jieduan.py
111 lines (106 loc) · 2.04 KB
/
pinjie_jieduan.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
from Pyfhel import Pyfhel,PyCtxt,PyPtxt
import numpy as np
import time
he=Pyfhel()
he.contextGen(p=1179649,m=32768,flagBatching=True,fracDigits=20)
he.keyGen()
a=PyCtxt()
b=PyCtxt()
q=0
cc=0
#加密文件
def PPctxt(name):
fil=open(name)
lines=fil.readlines()
strr=''.join(lines)
global l
l=len(strr)
l=l-1
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 nrr,num_zifu
num_zifu=0
nrr=[]
for line in lines:
num_zifu=num_zifu+len(line)
nrr.append(len(line))
#行数
global num_h
num_h=len(lines)
PPctxt('1.txt')
#拼接/追加
def concat(name,seq):
global l
a.load(name,encoding='array')
#0数组用于追加
b=np.zeros((l,),dtype=int)
b=list(b)
c=np.array(list(seq))
#追加
for i in c:
b.append(ord(i))
b=np.array(b)
b=he.encryptBatch(b)
#拼接
d=he.add(a,b)
#存储
d.save(name)
l=l+len(c)
#print(he.decryptBatch(d))
#concat('1.txt','sjt')
#截断(按字符)
def cut_off(name,n):
global l
a.load(name,encoding='array')
b=np.zeros((l,),dtype=int)
for i in range(n):
b[i]=1
b=he.encryptBatch(b)
c=he.multiply(a,b)
c.save(name)
l=n
#cut_off('1.txt',2)
#截断(按行)
def h_cut_off(name,n):
num=0
global l
if(n<num_h):
for i in range(n):
num=num+nrr[i]
a.load(name,encoding='array')
b=np.zeros((l,),dtype=int)
for i in range(num):
b[i]=1
b=he.encryptBatch(b)
c=he.multiply(a,b)
c.save(name)
l=num
h_cut_off('1.txt',2)
#解密
def Decrypt(name):
#global q
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')