-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbe_judge.py
126 lines (122 loc) · 2.51 KB
/
be_judge.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
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 lbb
lb=list(strr)
lb=lb[:3]
lbb=[]
for i in lb:
lbb.append(ord(i))
lbb=np.array(lbb)
print(lbb)
lbb=he.encryptBatch(lbb)
#全局化后三位密文
global ebb,l
eb=list(strr)
l=len(strr)
eb=eb[l-4:l-1]
print(eb)
ebb=[]
for i in eb:
ebb.append(ord(i))
ebb=np.array(ebb)
ebb=he.encryptBatch(ebb)
#转数字+密文化+存储
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)
PPctxt('1.txt')
#字符串开头判断
def startswitch(name,judge):
while name==panduan:
arr=list(judge)
#print(arr)
brr=[]
for i in arr:
brr.append(ord(i))
brr=np.array(brr)
brr=he.encryptBatch(brr)
result=he.sub(lbb,brr)
#以下两行测试用
global bb
bb=result
break
return result
#startswitch('1.txt','sjt')
#字符串结尾判断
def endswitch(name,judge):
while name==panduan:
arr=list(judge)
brr=[]
for i in arr:
brr.append(ord(i))
brr=he.encryptBatch(brr)
result=he.sub(ebb,brr)
#print(he.decryptBatch(result))
#测试
global ee
ee=result
break
return result
#endswitch('1.txt','sjt')
#eq比较函数
def eq(name,eq):
a.load(name,encoding='array')
eq=list(eq)
arr=[]
for i in eq:
arr.append(ord(i))
arr=np.array(arr)
arr=he.encryptBatch(arr)
result=he.sub(a,arr)
#ceshi
global jg
jg=result
return jg
eq('1.txt','sjt')
#字符串开头、结尾判断+比较 解密函数
def DecryptJudge():
a=he.decryptBatch(jg)
cc=a
a=a[:3]
b=a[:l]
#print(b)
c=0
d=0
for i in cc:
if i==0:
d=1
for i in b:
if(i==0 or d==1):
c=1
#print(c)
#print(c)
if(a==[0,0,0] or c==1):
print('True')
else:
print('Flase')
DecryptJudge()