-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path凱薩.py
131 lines (85 loc) · 2.39 KB
/
凱薩.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
import numpy as np
import random
import nltk
from nltk.corpus import wordnet
def StoI(s,key):
if ord(s)>=ord("a") and ord(s)<=ord("z") :
b=ord(s)-ord("a")
b=(b+key) % 26
c=b+ord("a")
return chr(c)
else :
return s
def ItoS(s,key):
if ord(s)>=ord("a") and ord(s)<=ord("z") :
b=ord(s)-ord("a")
b=(b-key) % 26
c=b+ord("a")
return chr(c)
else :
return s
#def BOOM(s1,s2):
def BOOM(s2):
b=s2
c=""
for key in range(26):
c=""
for i in range(len(s2)):
c+=ItoS(b[i],key)
print("key:"+str(key)+"\t猜測明文:"+c)
#if c==s1 :
# return "明文:"+c+"\nkey:"+str(key)
def bigBOOM(s2):
nltk.download('wordnet')
nltk.download('punkt')
b=s2
c=""
for key in range(26):
c=""
for i in range(len(s2)):
c+=ItoS(b[i],key)
dd=nltk.word_tokenize(c)
#print(type(dd))
cs=0
for i in dd:
if wordnet.synsets(i):
cs=cs+0
else :
cs=cs+1
if cs>0:
print("key:"+str(key)+"\t猜測明文:"+c)
else:
print("key:"+str(key)+"\t較肯定猜測明文:"+c)
#if not wordnet.synsets(c):
# print("key:"+str(key)+"\t猜測明文:"+c)
#else:
# print("key:"+str(key)+"\t較肯定猜測明文:"+c)
#if c==s1 :
# return "明文:"+c+"\nkey:"+str(key)
mod=int(input("模式(1>加密,2>解密,3>破密,4>破密(nltk)):"))
if mod==1:
q=input("明文:")
#key=int(input("key:"))
key=random.randint(0,25)
print("key:"+str(key))
out=""
for ans in q:
out+=StoI(ans,key)
print(out)
elif mod==2:
q=input("密文:")
key=int(input("key:"))
out=""
for ans in q:
out+=ItoS(ans,key)
print(out)
elif mod==3 :
#q1=input("明文:")
q2=input("密文:")
#print(BOOM(q1,q2))
print(BOOM(q2))
else :
#q1=input("明文:")
q2=input("密文:")
#print(BOOM(q1,q2))
print(bigBOOM(q2))