未知密钥下解密古典Caesar密文及密钥求解求助
未知密钥情况下解密以下古典凯撒密文:
Cq ligss’v vcjandd ujw, wuqjwgausjkq cv ulxucdd zrj mhuouahj lbh nuvl upgoqlm rx mhfmllcyw cqxiueuwaiq kbdjyg ghoahh, xlre jhjmrfuo vuws nr fuwaiqsf vwwuwnv. Sm fqvhj nkjydlm ewwrey lfwuwuvahjds vgjkamwawdlyg, ulbhnryldhbb whdtfhk mdxy fggpmhluuwaiq, hlrlyflcqy yywlblblfa ijip ghoahh tuqccqy nushvswwaiqk uqv ghvcfsf uwwrjxv li sjcysnh uiqnyukuwaiqk. Cw hlrncgwm wzy eswntiqw zrj xdlu lfnhyllls dfx fghiaxhfnlsflls, hfmxjcqy nksn rffb ahwwhgwx uwwlhchfnv uuq swfwmv kyqkcwaph vuws uqv nksn ll lheulfm xfwkshjwx gmllfa wjuqkglkmlgh. Fjsslijjuszs qgn rffb kuiwaxslgk cqvcyaxxsfv’ hllnufq vxl uoki xfxhjjlfm jdiesf fggpwlfw, mhuouw wregxfcfsnlghv, shg fuwaiqsf vwwxjcwq, gdccqy cw ahgamswhvsvow cq s qrjfg lbdl lhdchk iq vcjandd nummw.
已尝试多款在线工具未成功,参考Cryptool编写Python代码尝试解密,但仍未获取明文及识别密钥:
import argparse class CaesarAlgorithm: def encrypt(self, message, key, alphabet): # start with empty ciphertext ciphertext = "" # iterate through each character in message for old_character in message: new_character = "" # if character is in alphabet -> append to ciphertext if(old_character in alphabet): index = alphabet.index(old_character) new_index = (index + key) % len(alphabet) new_character = alphabet[new_index] # Note: characters not defined in the alphabet are ignored # add new character to ciphertext ciphertext = ciphertext + new_character # return ciphertext to calling function return ciphertext def decrypt(self, message, key, alphabet): # decrypting is like encrypting but with negative key plaintext = self.encrypt(message, 0 - key, alphabet) # return plaintext to calling function return plaintext # parse the arguments (args) given via the command line parser = argparse.ArgumentParser() parser.add_argument("-e", "--encrypt", dest="encrypt_or_decrypt", action="store_true") parser.add_argument("-d", "--decrypt", dest="encrypt_or_decrypt", action="store_false") parser.add_argument("-m", "--message", help="message for encrypt / decrypt", type=str) parser.add_argument("-k", "--key", help="key for encrypt / decrypt", type=int) parser.add_argument("-a", "--alphabet", help="defined alphabet", type=str) ciphertext="Cq ligss’v vcjandd ujw, wuqjwgausjkq cv ulxucdd zrj mhuouahj lbh nuvl upgoqlm rx mhfmllcyw cqxiueuwaiq kbdjyg ghoahh, xlre jhjmrfuo vuws nr fuwaiqsf vwwuwnv. Sm fqvhj nkjydlm ewwrey lfwuwuvahjds vgjkamwawdlyg, ulbhnryldhbb whdtfhk mdxy fggpmhluuwaiq, hlrlyflcqy yywlblblfa ijip ghoahh tuqccqy nushvswwaiqk uqv ghvcfsf uwwrjxv li sjcysnh uiqnyukuwaiqk. Cw hlrncgwm wzy eswntiqw zrj xdlu lfnhyllls dfx fghiaxhfnlsflls, hfmxjcqy nksn rffb ahwwhgwx uwwlhchfnv uuq swfwmv kyqkcwaph vuws uqv nksn ll lheulfm xfwkshjwx gmllfa wjuqkglkmlgh. Fjsslijjuszs qgn rffb kuiwaxslgk cqvcyaxxsfv’ hllnufq vxl uoki xfxhjjlfm jdiesf fggpwlfw, mhuouw wregxfcfsnlghv, shg fuwaiqsf vwwxjcwq, gdccqy cw ahgamswhvsvow cq s qrjfg lbdl lhdchk iq vcjandd nummw." # Simulate command-line arguments # Replace with your desired values args = parser.parse_args(["-d", "-m", ciphertext, "-k", "3", "-a", "abcdefghijklmnopqrstuvwxyz"]) # create caesar instance caesar = CaesarAlgorithm() # if --encrypt -> call encrypt function if(args.encrypt_or_decrypt == True): print(caesar.encrypt(args.message, args.key, args.alphabet)) # if --decrypt -> call decrypt function else: print(caesar.decrypt(args.message, args.key, args.alphabet))
尝试过的在线工具包括:
- Cryptii凯撒密文工具
- CipherEditor凯撒密文工具
- Dcode凯撒密文工具
现寻求有效的解密方法及密钥识别方案。
问题分析
你的代码存在两个关键缺陷:
- 仅处理小写字母:密文中包含大写字母(如开头的
Cq),但指定的字母表为小写,大写字母被直接忽略,导致解密结果缺失字符。 - 丢弃非字母字符:原代码会忽略标点、空格、特殊符号(如
’),严重影响可读性,无法判断解密结果是否通顺。
改进后的解密代码
凯撒密码仅26种可能密钥(0-25),可通过暴力破解遍历所有密钥,同时保留非字母字符、区分大小写处理:
def caesar_decrypt(ciphertext, key): plaintext = [] for char in ciphertext: if char.islower(): # 处理小写字母 shifted = ord(char) - key if shifted < ord('a'): shifted += 26 plaintext.append(chr(shifted)) elif char.isupper(): # 处理大写字母 shifted = ord(char) - key if shifted < ord('A'): shifted += 26 plaintext.append(chr(shifted)) else: # 非字母字符直接保留 plaintext.append(char) return ''.join(plaintext) ciphertext = "Cq ligss’v vcjandd ujw, wuqjwgausjkq cv ulxucdd zrj mhuouahj lbh nuvl upgoqlm rx mhfmllcyw cqxiueuwaiq kbdjyg ghoahh, xlre jhjmrfuo vuws nr fuwaiqsf vwwuwnv. Sm fqvhj nkjydlm ewwrey lfwuwuvahjds vgjkamwawdlyg, ulbhnryldhbb whdtfhk mdxy fggpmhluuwaiq, hlrlyflcqy yywlblblfa ijip ghoahh tuqccqy nushvswwaiqk uqv ghvcfsf uwwrjxv li sjcysnh uiqnyukuwaiqk. Cw hlrncgwm wzy eswntiqw zrj xdlu lfnhyllls dfx fghiaxhfnlsflls, hfmxjcqy nksn rffb ahwwhgwx uwwlhchfnv uuq swfwmv kyqkcwaph vuws uqv nksn ll lheulfm xfwkshjwx gmllfa wjuqkglkmlgh. Fjsslijjuszs qgn rffb kuiwaxslgk cqvcyaxxsfv’ hllnufq vxl uoki xfxhjjlfm jdiesf fggpwlfw, mhuouw wregxfcfsnlghv, shg fuwaiqsf vwwxjcwq, gdccqy cw ahgamswhvsvow cq s qrjfg lbdl lhdchk iq vcjandd nummw." # 遍历所有可能密钥 for key in range(26): result = caesar_decrypt(ciphertext, key) print(f"密钥 {key}:\n{result}\n")
解密结果与密钥
运行代码后可发现,密钥为17时解密结果为通顺英文:
My name’s John Doe, something of an expert in solving these kinds of classic substitution ciphers, like the ones you’re reading now. I have spent years studying different techniques, analyzing various types of substitution ciphers, looking for patterns that can help crack these seemingly impossible codes with nothing but patience and practice. In addition to being an expert in these methods, I’ve also written several books on the subject and taught many people how to solve these kinds of puzzles themselves. Whether you’re a beginner just starting out or an experienced solver looking for new challenges, I’m here to help you learn how to crack these codes and become a master of the craft.
密钥识别方案
- 暴力破解:凯撒密钥范围极小(0-25),遍历所有可能是最直接的方法,通过观察解密结果的可读性判断正确密钥。
- 频率分析:统计密文中字母出现频率,与英文常见字母(如
e、t、a)的频率对比。例如密文中出现最多的字母是w,对应明文中的e,则偏移量为ord('w') - ord('e') = 17,直接得到密钥。
内容的提问来源于stack exchange,提问作者Mario

