#list out all the possible scenariosdefencry(t):ift>ord('Z'):t=t-26returnchr(t)s=input()foriinrange(1,26):print(f"向右偏移{i:2}位: ",end='')forcharins:print(encry(ord(char)+i),end='')print()
# 对于key长度大于1的情况进行分析print("please enter the string to be encry")s=input()print("please enter the width of the key")key_len=input()key_len=int(key_len)# 对于key = len的情况,将其分为len组,每组进行频率分析,得到每组的频率最高的字母,认为其对应的明文字母为e,计算出每组的key字母foriinrange(key_len):# 统计每组的频率freq={}forjinrange(i,len(s),key_len):ifs[j]infreq:freq[s[j]]+=1else:freq[s[j]]=1# 找到频率前三的字母,打印出来(格式:字母+频率),并存在新数组中freq=sorted(freq.items(),key=lambdax:x[1],reverse=True)print("the frequency of the group "+str(i)+" is:")forjinrange(min(3,len(freq))):print(freq[j][0]+": "+str(freq[j][1]))
After I select a key, I can verify whether its true:
# 输入字符串,不断尝试各种key,直到找到想要的结果,输入quit退出print("please enter the string to be encry")s=input()whileTrue:# 这里输入的不是key,而是几组中对应的e的字母组成的字符串print("please enter the key to be processed")pre_key=input()ifpre_key=="quit":breakkey=""forcinpre_key:idx=(ord(c)-ord('e'))%26key+=chr(idx+ord('a'))#此时key就是我们想要验证的key# 对于key长度大于等于2的情况,开始解密,输出解密得到的字符串res=""foriinrange(len(s)):idx=(ord(s[i])-ord(key[i%len(key)]))%26res+=chr(idx+ord('a'))print("the result is:")print(res)print("the key is:")print(key)
Hence there are a total of nine possibilities: ta, tt, te, oa, ot, oe, ga, gt, ge
After verifying all of them, I find that none of them can meet the requirement.
Then I assume the length of the key is 3.
figure 3. The highest frequency
There are 27 possibilities, so I try these options one by one. Eventually, "g", "e", "x" correspond respectively to the "e" in the combined group as the figure below.
figure 4. The proper answer
Content: It is essential to seek out enemy agents who have come to conduct espionage against you and to bribe them to serve you give them instructions and care for them thus doubled agents are recruited and used Sun Tzu The Art of War
Actually I have no ideas how to do this when I see the ciphertext. However, I noticed that there are 5 "MAL" occurring in the text, which implies that it can't be vigerene. After confirming it's not caesar, I decided to find a one-to-one correspondence.
"MAL" appears the most frequently. I guess it's "THE"
Then, "MAIM" refers to "TH_T", I guess it's "THAT", A->I
"A_E"->"IZL", R->Z or G->Z or W->Z or X->Z or Y->Z
"_E"->"UL", W->U or H->U or M->U
combine the two words, I can make a guess that "UL IZL" is "WE ARE", R->Z, W->U
"xxx" is a sentence someone says, "WH_"->"UAE", "_A__"->"RICF", I just guess it's "WHO SAID"
"O_"->"EX", guess it's "OF"
Then, "_ASSWORD"->"TIRRUEZF", I guess it's "PASSWORD"
"HI_HEST"->"ACWALRM", it's "HIGHEST"
"__OW_EDGE"->"DYEUPLFWL", it's "KNOWLEDGE"
"S_RRO_NDED"->"RKZZEKYFLF", it's "SURROUNDED"
"_AN"->"OIY", "WHO SAID" is after it, I guess it's "MAN"