-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzm.py
155 lines (144 loc) · 2.99 KB
/
zm.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
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
qq=[]
pp=[]
ppp=0
#加密文件
def PPctxt(name):
fil=open(name)
lines=fil.readlines()
strr=''.join(lines)
arr=[]
for line in strr:
for ind in line:
arr.append(ind)
arr.remove('\n')
global lenth
lenth=len(arr)
brr=[]
global qq
global pp
for i in arr:
brr.append(ord(i))
q=0
p=0
l=len(brr)
pp=np.zeros((l,),dtype=int)
qq=np.zeros((l,),dtype=int)
#小写记录
for i in brr:
if(96<i<123):
qq[q]=32
q=q+1
#大写记录
for i in brr:
if(64<i<91):
pp[p]=32
p=p+1
global ppp
if(pp[0]==32):
ppp=1
print('qq',qq)
pp=np.array(pp)
pp=he.encryptBatch(pp)
qq=np.array(qq)
qq=he.encryptBatch(qq)
crr=np.array(brr)
err=he.encryptBatch(crr)
err.save(name)
pp.save('dxjl.txt')
qq.save('xxjl.txt')
PPctxt('1.txt')
#print(qq)
#大写转换
def upper(name):
a.load(name,encoding='array')
b.load('xxjl.txt',encoding='array')
c=he.sub(a,b)
c.save(name)
#print(he.decryptBatch(c))
a.load('dxjl.txt',encoding='array')
e=he.decryptBatch(a)
#print('e1',e[1])
for i in range(len(e)):
e[i]=32
#print('e',e)
e=he.encryptBatch(e)
e.save('dxjl.txt')
#upper('1.txt')
#小写转换
def lower(name):
a.load(name,encoding='array')
b.load('dxjl.txt',encoding='array')
#print(pp)
#b=he.encryptBatch(pp)
c=he.add(a,b)
#d=he.decryptBatch(b)
#print('b',b[:5])
c.save(name)
a.load('xxjl.txt',encoding='array')
e=he.decryptBatch(a)
for i in range(len(e)):
e[i]=32
e=he.encryptBatch(e)
e.save('xxjl.txt')
#lower('1.txt')
#首字母大写(整体)其他小写
def capitalize(name):
a.load(name,encoding='array')
b.load('dxjl.txt',encoding='array')
#首字母已经大写ppp=1
if(ppp==1):
c=he.add(a,b)
else:
d=[32,0]
d=he.encryptBatch(d)
c=he.add(a,b)
c=he.sub(c,d)
c.save(name)
#修改大写记录
e=he.decryptBatch(b)
for i in range(len(e)):
e[i]=0
e[0]=32
e=he.encryptBatch(e)
e.save('dxjl.txt')
#修改小写记录
a.load('xxjl.txt',encoding='array')
f=he.decryptBatch(a)
for i in range(len(f)):
f[i]=32
f[0]=0
f=he.encryptBatch(f)
f.save('xxjl.txt')
#capitalize('1.txt')
#大小写反转
def swapcase(name):
a.load(name,encoding='array')
b.load('dxjl.txt',encoding='array')
c=he.add(a,b)#原大写变小写
b.save('xxjl.txt')
b.load('xxjl.txt',encoding='array')
b.save('dxjl.txt')
c=he.sub(c,b)#原小写变大写
c.save(name)
swapcase('1.txt')
#解密
def Decrypt(name):
global q
a.load(name,encoding='array')
b=he.decryptBatch(a)
b=b[:lenth]
#print(b)
brr=[]
for i in b:
brr.append(chr(i))
print(brr)
Decrypt('1.txt')