凯撒加密【加密+暴力破解+文本单词匹配】 要求: 1 实现指定秘钥的凯撒密码加密 2 已知秘钥解密 3 未知秘钥,采用暴力破解 4 对破解出的密码,与单词词典比对,找出明文的那一条,得到加密的秘钥 采用python实现 报告+代码链接 链接:https://pan.baidu.com/s/1aOIhnLFIy36Y05AnrCz8pw 提取码:zulz
1 实现指定秘钥的凯撒密码加密
2 已知秘钥解密
3 未知秘钥,采用暴力破解
4 对破解出的密码,与单词词典比对,找出明文的那一条,得到加密的秘钥
采用python实现
报告+代码链接
链接:https://pan.baidu.com/s/1aOIhnLFIy36Y05AnrCz8pw
提取码:zulz
Caesar_encryption.py
MAX_KEY_SIZE = 26
def getMode():
while True:
print('请选择加密或解密模式,或者选择暴力破解:')
print('加密:encrypt(e)')
print('解密:decrypt(d)')
print('暴力破解:brute(b)')
mode = input().lower()
if mode in 'encrypt e decrypt d brute b'.split():
return mode
else:
print('请输入"encrypt"或"e"或"decrypt"或"d"或"brute"或"b"!')
def getMessage():
print('请输入你的信息:')
return input()
def getKey():
key = 0
while True:
print('请输入密钥数字(1-25)')
key = int(input())
if (key >=1 and key <= MAX_KEY_SIZE):
return key
def getTranslatedMessage(mode, message, key):
if mode[0] == 'd':
key = -key #如果是解密的 就反过来
translated = ''
for symbol in message:
if symbol.isalpha(): #是否都是字母
num = ord(symbol) #得到字符symbol对应的ASCII码
num += key #解密出对应的ASCII码
if symbol.isupper():
if num > ord('Z'):
num -= 26
elif num < ord('A'):
num += 26
elif symbol.islower():
if num > ord('z'):
num -= 26
elif num < ord('a'):
num += 26
translated += chr(num) #与ord对应 将码转字符
else:
translated += symbol #不是字母的位置就直接添加了
write_file(translated,"cipher.txt")
return translated
def write_file(str,filename):
with open(filename,'w') as fp:
fp.write(str)
mode = getMode()
message = getMessage()
if mode[0] != 'b':
key = getKey()
print('你要翻译的信息是:')
if mode[0] != 'b':
print(getTranslatedMessage(mode, message, key))
else:
for key in range(1, MAX_KEY_SIZE + 1):
print(key, getTranslatedMessage('decrypt', message, key))
Caesar_ Decrypt.py
########################功能#######################
#1、文件读入4万个常用单词、密文
#2、解密后存文件
#3、cipher 是存的原始密文 cipher_no 是把原始密文的标点符号过滤后的密文
########################程序###########################
import time
#存放100常用单词的文本
WORDS_FILE_NAME = 'diction.txt'
#WORDS_FILE_NAME = 'words.txt'
#加密所用的对照表
KEY_FILE_NAME = 'duizhao.txt'
#存放密文的文本
CIPHER_FILE_NAME = 'cipher.txt'
#存放明文的文本
PLAIN_FILE_NAME = 'plain.txt'
letters = 'abcdefghijklmnopqrstuvwxyz'
#读入一个文本
def read_file(filename):
lines = ''
with open(filename,'r') as fp:
for line in fp:
lines += line
return lines
#写入字符串到文本
def write_file(str,filename):
with open(filename,'w') as fp:
fp.write(str)
#根据秘钥加密
def encrypt(key,textFileString):
lines = ''
#读入待加密文本
lines = read_file(textFileString)
lines.lower()
#读入对应转换表
letters1 = '' #选择对应的转换表
with open(KEY_FILE_NAME, 'r') as fp1:
for line in fp1:
key -= 1
if key == -1: #找到key那一行
letters1 += line
#进行加密
cstring = ''
length = len(lines)
for i in range(0, length):
if lines[i] >= 'a' and lines[i] <= 'z':
for j in range(0, 26):
if lines[i] == letters[j]: #letters = 'abcdefghijklmnopqrstuvwxyz'
cstring += letters1[j] #进行相应位的替换
else:
cstring += lines[i]
return cstring
#根据指定的秘钥解密
def decipher1(key,textFileString):
#从文件读入数据
lines = ''
cstring = ''#密文结果
lines = read_file(textFileString)
#全部转换为小写字母
lines.lower()
#根据key进行解密
letters1 = ''
with open(KEY_FILE_NAME,'r') as fp1:
for line in fp1:
key += 1
if key == 27:
letters1+=line
#开始解密
length = len(lines)
for i in range(0,length):
if lines[i]>='a' and lines[i] <= 'z':
for j in range(0, 26):
if lines[i] == letters[j]:
cstring+=letters1[j]
else:
cstring += lines[i]
return cstring
#根据常用词汇表解密
def decipher2(textFileString):
#读入一百个单词
words = read_file(WORDS_FILE_NAME).split('
') #读取文件所有内容 并以回车分词
max = 0;
index = 0;
#暴力破解选择一个秘钥
filename_no = read_file(textFileString).replace(',','')
filename_no = filename_no.replace('.','')
filename_no = filename_no.replace(':','')
filename_no = filename_no.replace('"','')
filename_no = filename_no.replace('?','')
write_file(filename_no,'cipher_no.txt') #符号替换完 存入_no
print("暴力破解的所有结果:")
for i in range(1,26):
pstring = decipher1(i,'cipher_no.txt')
print("秘钥为%d"%i,"时,解密的结果:",pstring)
plainWords = pstring.split(' ')
#对比
length = len(plainWords)
temp = 0
for j in range(0,length):
if plainWords[j] in words:
temp += 1 # 在词库里面的词语数量
if temp > max:
max = temp
index = i
print("经过词典比对,最终得出正确的明文:")
print('破解秘钥是%d'%index)
# print('单词个数%d',length)
print('单词的匹配程度{}%'.format(float('%.2f'%(max*100/length))))
#写入文件
str = decipher1(index,textFileString)
write_file(str,PLAIN_FILE_NAME)
return decipher1(index,textFileString)
def main():
print('主程序入口')
start_time = time.time()
print('解密后的结果:
'+decipher2('cipher.txt')) #解密
#print('加密后的结果:
'+encrypt(3, 'D:\2018Python_Learningpythonlearning\plain.txt')) #加密
end_time = time.time()
run_time = end_time-start_time
print('程序的运行时间为:{}'.format(run_time))
if __name__ == '__main__':
main()
有一些txt见网盘链接