300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > 云安全-Python实现凯撒密码和替换密码的加密解密与暴力破解

云安全-Python实现凯撒密码和替换密码的加密解密与暴力破解

时间:2024-06-16 16:21:37

相关推荐

云安全-Python实现凯撒密码和替换密码的加密解密与暴力破解

记录顺带保存一次课堂作业,部分参考了现有的实现,结合需求进行了一波码,能用就行,人和代码能跑一个就好,替换密码暴力破解实在懒得搞了,或许以后会更?

import getoptimport sys#接收参数,规定参数名opts, args = getopt.getopt(sys.argv[1:], '-h-v-m:-a:-p:-c:-k:-A:', ['help', 'version', 'mode=', 'action=', 'plaintext=', 'ciphertext=', 'key=', 'admin='])for opt_name, opt_value in opts:if opt_name in ('-h', '--help'):print("Help Info: \n""-m or --mode, value can use caesar or permute \n""-a or --action, value can use encode or decode or bruteAttack \n""-p or --plaintext \n""-c or --ciphertext \n""-k or --key \n""example-1:-m caesar -a encode -p helloworld -k 3 \n""example-2:--mode permute --action=decode --ciphertext=helloworld -k word \n")sys.exit()elif opt_name in ('-v', '--version'):print("Version is 1.0, Update in /4/26 ")sys.exit()elif opt_name in ('-m', '--mode'):mode = opt_valueelif opt_name in ('-a', '--action'):action = opt_valueelif opt_name in ('-p', '--plaintext'):plaintext = opt_valueelif opt_name in ('-c', '--ciphertext'):ciphertext = opt_valueelif opt_name in ('-k', '--key'):key = opt_valueelif opt_name in ('-A', '--admin'):if(opt_value == "admin"):print("Developer:SZPT-AI")print("Mode =", mode)print("Action =", action)#凯撒加密def caesarEncode(plaintext, key):encodeText = []for word in plaintext:encodeText.append(chr(ord('a') + ((ord(word) - ord('a')) + int(key)) % 26))ciphertext = "".join(encodeText)print(ciphertext)#凯撒解密def caesarDecode(ciphertext, key):DecodeText = []for word in ciphertext:DecodeText.append(chr(ord('a') + ((ord(word) - ord('a')) - int(key)) % 26))plaintext = "".join(DecodeText)print(plaintext)#凯撒暴力破解def caesarBruteAttack(ciphertext):#遍历所有偏移量for key in range(1,26):DecodeText = []for word in ciphertext:DecodeText.append(chr(ord('a') + ((ord(word) - ord('a')) - int(key)) % 26))plaintext = "".join(DecodeText)print(plaintext)def keyIntFormat(key):# 将key转化为数字tmp_key1 = []for keyword in key:tmp_key1.append(ord(keyword) - 96)# 建立临时列表进行排序tmp_key2 = sorted(tmp_key1)tmp_key = [] * len(key)for num in range(len(tmp_key2)):for keynum in range(len(tmp_key1)):if (tmp_key1[num] == tmp_key2[keynum]):tmp_key.append(str(keynum + 1))key = "".join(tmp_key)return key#置换加密def permuteEncode(plaintext, key):keylen = len(key)ciphertext = ""key = keyIntFormat(key)#根据key长度对明文进行分段for part in range(0, len(plaintext), keylen):tmp_ciphertext = [""] * keylen#超长度内容处理if part + keylen > len(plaintext):tmp_plaintext = plaintext[part:]#内容处理else:tmp_plaintext = plaintext[part:part + keylen]for word in range(len(tmp_plaintext)):tmp_ciphertext[int(key[word]) - 1] = tmp_plaintext[word]ciphertext += "".join(tmp_ciphertext)print(ciphertext)#置换解密def permuteDecode(ciphertext, key):keylen = len(key)plaintext = ""key = keyIntFormat(key)#根据key长度对密文进行分段for part in range(0, len(ciphertext), keylen):#构筑矩阵tmp_plaintext = [""] * keylen#超长度内容处理if part + keylen >= len(ciphertext):tmp_ciphertext = ciphertext[part:]re_key = []#根据key确认位置for word in range(len(tmp_ciphertext)):re_key.append(int(key[word]) - 1)re_key.sort()for word in range(len(tmp_ciphertext)):tmp_plaintext[word] = tmp_ciphertext[re_key.index(int(key[word]) - 1)]#内容处理else:#分段内容tmp_ciphertext = ciphertext[part:part + keylen]for word in range(len(tmp_ciphertext)):tmp_plaintext[word] = tmp_ciphertext[int(key[word]) - 1]plaintext += "".join(tmp_plaintext)print(plaintext)#置换暴力破解def permuteBruteAttack(ciphertext):#其实就是太麻烦了我不想写#此处思路可以根据出现频率高的单词字母去匹配字典有可能的日常高频单词(就是随缘撞库)print("Develop in future release")if(mode == "caesar"): #凯撒加密if(action == "encode"):caesarEncode(plaintext, key)elif(action == "decode"):caesarDecode(ciphertext, key)elif(action == "bruteAttack"):caesarBruteAttack(ciphertext)elif(mode == "permute"): #置换加密if(action == "encode"):permuteEncode(plaintext, key)elif(action == "decode"):permuteDecode(ciphertext, key)elif(action == "bruteAttack"):permuteBruteAttack(ciphertext)

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。