鱼C论坛

 找回密码
 立即注册
查看: 3389|回复: 7

题目59:使用暴力攻击,你能解开这个异或加密吗?

[复制链接]
发表于 2015-6-18 23:47:17 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x

XOR decryption

Each character on a computer is assigned a unique code and the preferred standard is ASCII (American Standard Code for Information Interchange). For example, uppercase A = 65, asterisk (*) = 42, and lowercase k = 107.

A modern encryption method is to take a text file, convert the bytes to ASCII, then XOR each byte with a given value, taken from a secret key. The advantage with the XOR function is that using the same encryption key on the cipher text, restores the plain text; for example, 65 XOR 42 = 107, then 107 XOR 42 = 65.

For unbreakable encryption, the key is the same length as the plain text message, and the key is made up of random bytes. The user would keep the encrypted message and the encryption key in different locations, and without both "halves", it is impossible to decrypt the message.

Unfortunately, this method is impractical for most users, so the modified method is to use a password as a key. If the password is shorter than the message, which is likely, the key is repeated cyclically throughout the message. The balance for this method is using a sufficiently long password key for security, but short enough to be memorable.

Your task has been made easy, as the encryption key consists of three lower case characters. Using cipher.txt (right click and 'Save Link/Target As...'), a file containing the encrypted ASCII codes, and the knowledge that the plain text must contain common English words, decrypt the message and find the sum of the ASCII values in the original text.

题目:

电脑上的每个字符都有一个唯一编码,通用的标准是 ASCII (American Standard Code for Information Interchange 美国信息交换标准编码)。例如大写A = 65, 星号(*) = 42,小写k = 107。

一种现代加密方法是用一个密钥中的给定值,与一个文本文件中字符的 ASCII 值进行异或。使用异或方法的好处是对密文使用同样的加密密钥可以得到加密前的内容。例如,65 XOR 42 = 107, 然后 107 XOR 42 = 65。

对于不可攻破的加密,密钥的长度与明文信息的长度是一样的,而且密钥是由随机的字节组成的。用户将加密信息和加密密钥保存在不同地方,只有在两部分都得到的情况下,信息才能被解密。

不幸的是,这种方法对于大部分用户来说是不实用的。所以一种修改后的方案是使用一个密码作为密钥。如果密码比信息短,那么就将其不断循环直到明文的长度。平衡点在于密码要足够长来保证安全性,但是又要足够短使用户能够记得。

你的任务很简单,因为加密密钥是由三个小写字母组成的。文件 cipher1.txt (右键另存为)包含了加密后的 ASCII 码,并且已知明文是由常用英语单词组成。使用该文件来解密信息,然后算出明文中字符的 ASCII 码之和。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2016-8-31 12:48:53 | 显示全部楼层
没文件。。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-10-13 12:30:49 | 显示全部楼层

  1. 79,59,12,2,79,35,8,28,20,2,3,68,8,9,68,45,0,12,9,67,68,4,7,5,23,27,1,21,79,85,78,79,85,71,38,10,71,27,12,2,79,6,2,8,13,9,1,13,9,8,68,19,7,1,71,56,11,21,11,68,6,3,22,2,14,0,30,79,1,31,6,23,19,10,0,73,79,44,2,79,19,6,28,68,16,6,16,15,79,35,8,11,72,71,14,10,3,79,12,2,79,19,6,28,68,32,0,0,73,79,86,71,39,1,71,24,5,20,79,13,9,79,16,15,10,68,5,10,3,14,1,10,14,1,3,71,24,13,19,7,68,32,0,0,73,79,87,71,39,1,71,12,22,2,14,16,2,11,68,2,25,1,21,22,16,15,6,10,0,79,16,15,10,22,2,79,13,20,65,68,41,0,16,15,6,10,0,79,1,31,6,23,19,28,68,19,7,5,19,79,12,2,79,0,14,11,10,64,27,68,10,14,15,2,65,68,83,79,40,14,9,1,71,6,16,20,10,8,1,79,19,6,28,68,14,1,68,15,6,9,75,79,5,9,11,68,19,7,13,20,79,8,14,9,1,71,8,13,17,10,23,71,3,13,0,7,16,71,27,11,71,10,18,2,29,29,8,1,1,73,79,81,71,59,12,2,79,8,14,8,12,19,79,23,15,6,10,2,28,68,19,7,22,8,26,3,15,79,16,15,10,68,3,14,22,12,1,1,20,28,72,71,14,10,3,79,16,15,10,68,3,14,22,12,1,1,20,28,68,4,14,10,71,1,1,17,10,22,71,10,28,19,6,10,0,26,13,20,7,68,14,27,74,71,89,68,32,0,0,71,28,1,9,27,68,45,0,12,9,79,16,15,10,68,37,14,20,19,6,23,19,79,83,71,27,11,71,27,1,11,3,68,2,25,1,21,22,11,9,10,68,6,13,11,18,27,68,19,7,1,71,3,13,0,7,16,71,28,11,71,27,12,6,27,68,2,25,1,21,22,11,9,10,68,10,6,3,15,27,68,5,10,8,14,10,18,2,79,6,2,12,5,18,28,1,71,0,2,71,7,13,20,79,16,2,28,16,14,2,11,9,22,74,71,87,68,45,0,12,9,79,12,14,2,23,2,3,2,71,24,5,20,79,10,8,27,68,19,7,1,71,3,13,0,7,16,92,79,12,2,79,19,6,28,68,8,1,8,30,79,5,71,24,13,19,1,1,20,28,68,19,0,68,19,7,1,71,3,13,0,7,16,73,79,93,71,59,12,2,79,11,9,10,68,16,7,11,71,6,23,71,27,12,2,79,16,21,26,1,71,3,13,0,7,16,75,79,19,15,0,68,0,6,18,2,28,68,11,6,3,15,27,68,19,0,68,2,25,1,21,22,11,9,10,72,71,24,5,20,79,3,8,6,10,0,79,16,8,79,7,8,2,1,71,6,10,19,0,68,19,7,1,71,24,11,21,3,0,73,79,85,87,79,38,18,27,68,6,3,16,15,0,17,0,7,68,19,7,1,71,24,11,21,3,0,71,24,5,20,79,9,6,11,1,71,27,12,21,0,17,0,7,68,15,6,9,75,79,16,15,10,68,16,0,22,11,11,68,3,6,0,9,72,16,71,29,1,4,0,3,9,6,30,2,79,12,14,2,68,16,7,1,9,79,12,2,79,7,6,2,1,73,79,85,86,79,33,17,10,10,71,6,10,71,7,13,20,79,11,16,1,68,11,14,10,3,79,5,9,11,68,6,2,11,9,8,68,15,6,23,71,0,19,9,79,20,2,0,20,11,10,72,71,7,1,71,24,5,20,79,10,8,27,68,6,12,7,2,31,16,2,11,74,71,94,86,71,45,17,19,79,16,8,79,5,11,3,68,16,7,11,71,13,1,11,6,1,17,10,0,71,7,13,10,79,5,9,11,68,6,12,7,2,31,16,2,11,68,15,6,9,75,79,12,2,79,3,6,25,1,71,27,12,2,79,22,14,8,12,19,79,16,8,79,6,2,12,11,10,10,68,4,7,13,11,11,22,2,1,68,8,9,68,32,0,0,73,79,85,84,79,48,15,10,29,71,14,22,2,79,22,2,13,11,21,1,69,71,59,12,14,28,68,14,28,68,9,0,16,71,14,68,23,7,29,20,6,7,6,3,68,5,6,22,19,7,68,21,10,23,18,3,16,14,1,3,71,9,22,8,2,68,15,26,9,6,1,68,23,14,23,20,6,11,9,79,11,21,79,20,11,14,10,75,79,16,15,6,23,71,29,1,5,6,22,19,7,68,4,0,9,2,28,68,1,29,11,10,79,35,8,11,74,86,91,68,52,0,68,19,7,1,71,56,11,21,11,68,5,10,7,6,2,1,71,7,17,10,14,10,71,14,10,3,79,8,14,25,1,3,79,12,2,29,1,71,0,10,71,10,5,21,27,12,71,14,9,8,1,3,71,26,23,73,79,44,2,79,19,6,28,68,1,26,8,11,79,11,1,79,17,9,9,5,14,3,13,9,8,68,11,0,18,2,79,5,9,11,68,1,14,13,19,7,2,18,3,10,2,28,23,73,79,37,9,11,68,16,10,68,15,14,18,2,79,23,2,10,10,71,7,13,20,79,3,11,0,22,30,67,68,19,7,1,71,8,8,8,29,29,71,0,2,71,27,12,2,79,11,9,3,29,71,60,11,9,79,11,1,79,16,15,10,68,33,14,16,15,10,22,73
复制代码

加密文件内容
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-10-13 14:04:13 | 显示全部楼层
这题比较有趣,解题思路是先分析英语文章的中出现最多的字母或符号,一般应该是“空格”出现最多。(这个猜测有运气成分,如果不是空格最多,就不是这个结果了,但是可以用同样的方法,猜测其他字母或符号进行尝试。只要这个文章是英语,总归可以猜出,成功率高低罢了。)
空格的Asc码是32,然后代入按3字节一循环分成3组的密码中进行异或运算,根据解析出的结果,统计每组中出现频次最高的字母,即为密码。
(因为密码字符可以通过异或运算得出出现频次最高的空格,所以反过来空格通过异或运算出现频次最高的字母即是密码,逻辑就是这样。)
  1. cipher = [79,59,12,2,79,35,8,28,20,2,3,68,8,9,68,45,0,12,9,67,68,4,7,5,23,27,1,21,79,85,78,79,85,71,38,10,71,27,12,2,79,6,2,8,13,9,1,13,9,8,68,19,7,1,71,56,11,21,11,68,6,3,22,2,14,0,30,79,1,31,6,23,19,10,0,73,79,44,2,79,19,6,28,68,16,6,16,15,79,35,8,11,72,71,14,10,3,79,12,2,79,19,6,28,68,32,0,0,73,79,86,71,39,1,71,24,5,20,79,13,9,79,16,15,10,68,5,10,3,14,1,10,14,1,3,71,24,13,19,7,68,32,0,0,73,79,87,71,39,1,71,12,22,2,14,16,2,11,68,2,25,1,21,22,16,15,6,10,0,79,16,15,10,22,2,79,13,20,65,68,41,0,16,15,6,10,0,79,1,31,6,23,19,28,68,19,7,5,19,79,12,2,79,0,14,11,10,64,27,68,10,14,15,2,65,68,83,79,40,14,9,1,71,6,16,20,10,8,1,79,19,6,28,68,14,1,68,15,6,9,75,79,5,9,11,68,19,7,13,20,79,8,14,9,1,71,8,13,17,10,23,71,3,13,0,7,16,71,27,11,71,10,18,2,29,29,8,1,1,73,79,81,71,59,12,2,79,8,14,8,12,19,79,23,15,6,10,2,28,68,19,7,22,8,26,3,15,79,16,15,10,68,3,14,22,12,1,1,20,28,72,71,14,10,3,79,16,15,10,68,3,14,22,12,1,1,20,28,68,4,14,10,71,1,1,17,10,22,71,10,28,19,6,10,0,26,13,20,7,68,14,27,74,71,89,68,32,0,0,71,28,1,9,27,68,45,0,12,9,79,16,15,10,68,37,14,20,19,6,23,19,79,83,71,27,11,71,27,1,11,3,68,2,25,1,21,22,11,9,10,68,6,13,11,18,27,68,19,7,1,71,3,13,0,7,16,71,28,11,71,27,12,6,27,68,2,25,1,21,22,11,9,10,68,10,6,3,15,27,68,5,10,8,14,10,18,2,79,6,2,12,5,18,28,1,71,0,2,71,7,13,20,79,16,2,28,16,14,2,11,9,22,74,71,87,68,45,0,12,9,79,12,14,2,23,2,3,2,71,24,5,20,79,10,8,27,68,19,7,1,71,3,13,0,7,16,92,79,12,2,79,19,6,28,68,8,1,8,30,79,5,71,24,13,19,1,1,20,28,68,19,0,68,19,7,1,71,3,13,0,7,16,73,79,93,71,59,12,2,79,11,9,10,68,16,7,11,71,6,23,71,27,12,2,79,16,21,26,1,71,3,13,0,7,16,75,79,19,15,0,68,0,6,18,2,28,68,11,6,3,15,27,68,19,0,68,2,25,1,21,22,11,9,10,72,71,24,5,20,79,3,8,6,10,0,79,16,8,79,7,8,2,1,71,6,10,19,0,68,19,7,1,71,24,11,21,3,0,73,79,85,87,79,38,18,27,68,6,3,16,15,0,17,0,7,68,19,7,1,71,24,11,21,3,0,71,24,5,20,79,9,6,11,1,71,27,12,21,0,17,0,7,68,15,6,9,75,79,16,15,10,68,16,0,22,11,11,68,3,6,0,9,72,16,71,29,1,4,0,3,9,6,30,2,79,12,14,2,68,16,7,1,9,79,12,2,79,7,6,2,1,73,79,85,86,79,33,17,10,10,71,6,10,71,7,13,20,79,11,16,1,68,11,14,10,3,79,5,9,11,68,6,2,11,9,8,68,15,6,23,71,0,19,9,79,20,2,0,20,11,10,72,71,7,1,71,24,5,20,79,10,8,27,68,6,12,7,2,31,16,2,11,74,71,94,86,71,45,17,19,79,16,8,79,5,11,3,68,16,7,11,71,13,1,11,6,1,17,10,0,71,7,13,10,79,5,9,11,68,6,12,7,2,31,16,2,11,68,15,6,9,75,79,12,2,79,3,6,25,1,71,27,12,2,79,22,14,8,12,19,79,16,8,79,6,2,12,11,10,10,68,4,7,13,11,11,22,2,1,68,8,9,68,32,0,0,73,79,85,84,79,48,15,10,29,71,14,22,2,79,22,2,13,11,21,1,69,71,59,12,14,28,68,14,28,68,9,0,16,71,14,68,23,7,29,20,6,7,6,3,68,5,6,22,19,7,68,21,10,23,18,3,16,14,1,3,71,9,22,8,2,68,15,26,9,6,1,68,23,14,23,20,6,11,9,79,11,21,79,20,11,14,10,75,79,16,15,6,23,71,29,1,5,6,22,19,7,68,4,0,9,2,28,68,1,29,11,10,79,35,8,11,74,86,91,68,52,0,68,19,7,1,71,56,11,21,11,68,5,10,7,6,2,1,71,7,17,10,14,10,71,14,10,3,79,8,14,25,1,3,79,12,2,29,1,71,0,10,71,10,5,21,27,12,71,14,9,8,1,3,71,26,23,73,79,44,2,79,19,6,28,68,1,26,8,11,79,11,1,79,17,9,9,5,14,3,13,9,8,68,11,0,18,2,79,5,9,11,68,1,14,13,19,7,2,18,3,10,2,28,23,73,79,37,9,11,68,16,10,68,15,14,18,2,79,23,2,10,10,71,7,13,20,79,3,11,0,22,30,67,68,19,7,1,71,8,8,8,29,29,71,0,2,71,27,12,2,79,11,9,3,29,71,60,11,9,79,11,1,79,16,15,10,68,33,14,16,15,10,22,73]
  2. a= [cipher[i::3] for i in range(3)] #按3字节一循环分成3组,对应每个密码字符
  3. key=[]
  4. for i in range(3):
  5.     decode=[x^32 for x in a[i]]        #用空格(Asc码32)进行异或运算
  6.     key.append(max(decode, key=decode.count))        #把每组中的出现频次高的词加入key列表
  7. print 'The key is probably: ',key,' which is: ',''.join([chr(x) for x in key]) #打印密码
  8.    
  9. keylong=key*(len(cipher)/len(key))  #复制密码循环直到与原密文相同长度
  10. message=''.join(chr(a^b) for a,b in zip(cipher,keylong)) 密文和密码循环配对进行异或运算,结果转换Asc码加入massage中
  11. print message
  12. answer=sum([ord(x) for x in message])
  13. print answer
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-10-13 14:08:08 | 显示全部楼层
忘记贴结果了
The key is probably:  [103, 111, 100]  which is:  god
107313
[Finished in 0.1s]
原文是圣经中的一段。

(The Gospel of John, chapter 1) 1 In the beginning the Word already existed. He was with God, and he was God. 2 He was in the beginning with God. 3 He created everything there is. Nothing exists that he didn't make. 4 Life itself was in him, and this life gives light to everyone. 5 The light shines through the darkness, and the darkness can never extinguish it. 6 God sent John the Baptist 7 to tell everyone about the light so that everyone might believe because of his testimony. 8 John himself was not the light; he was only a witness to the light. 9 The one who is the true light, who gives light to everyone, was going to come into the world. 10 But although the world was made through him, the world didn't recognize him when he came. 11 Even in his own land and among his own people, he was not accepted. 12 But to all who believed him and accepted him, he gave the right to become children of God. 13 They are reborn! This is not a physical birth resulting from human passion or plan, this rebirth comes from God.14 So the Word became human and lived here on earth among us. He was full of unfailing love and faithfulness. And we have seen his glory, the glory of the only Son of the Father
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-6-27 09:37:25 | 显示全部楼层
按照ASCII码 1-123 对每三个字符挨个破解,各取出频率最高的一个字母,再反破解,如果是能读的单词(或者可以用正则筛选或者字符筛选一遍),就是正确答案。[103, 111, 100] ['g', 'o', 'd'];

[103, 111, 100] ['g', 'o', 'd'] (The Gospel of John, chapter 1) 1 In the
[102, 110, 101] ['f', 'n', 'e'] )Uid!Fnrqdm!ng!Knio-!bi`quds!0(!0!Ho!uid
[101, 109, 102] ['e', 'm', 'f'] *Vjg"Emqrgn"md"Hmjl."ajcrvgp"3+"3"Kl"vjg
[100, 108, 103] ['d', 'l', 'g'] +Wkf#Dlpsfo#le#Ilkm/#`kbswfq#2*#2#Jm#wkf
[98, 106, 97] ['b', 'j', 'a'] -Qm`%Bjvu`i%jc%Ojmk)%fmduq`w%4,%4%Lk%qm`
[97, 105, 98] ['a', 'i', 'b'] .Rnc&Aiuvcj&i`&Linh*&engvrct&7/&7&Oh&rnc
[111, 103, 108] ['o', 'g', 'l']  \`m(Og{xmd(gn(Bg`f$(k`ix|mz(9!(9(Af(|`m
[110, 102, 109] ['n', 'f', 'm'] !]al)Nfzyle)fo)Cfag%)jahy}l{)8 )8)@g)}al
[109, 101, 110] ['m', 'e', 'n'] "^bo*Meyzof*el*@ebd&*ibkz~ox*;#*;*Cd*~bo
[108, 100, 111] ['l', 'd', 'o'] #_cn+Ldx{ng+dm+Adce'+hcj{ny+:"+:+Be+cn
[107, 99, 104] ['k', 'c', 'h'] $Xdi,Kc|i`,cj,Fcdb ,odm|xi~,=%,=,Eb,xdi
[106, 98, 105] ['j', 'b', 'i'] %Yeh-Jb~}ha-bk-Gbec!-nel}yh-<$-<-Dc-yeh
[105, 97, 106] ['i', 'a', 'j'] &Zfk.Ia}~kb.ah.Daf`".mfo~zk|.?'.?.G`.zfk
[113, 121, 114] ['q', 'y', 'r'] >B~s6Qyefsz6yp6\y~x:6u~wfbsd6'?6'6_x6b~s
[112, 120, 115] ['p', 'x', 's'] ?Cr7Pxdgr{7xq7]xy;7tvgcre7&>7&7^y7cr
  1. from collections import Counter


  2. with open(r'C:\Users\wangyongzhao\Desktop\Python learning\code_file.txt') as f:
  3.     raw_file = f.read().split(',')
  4.     target = [raw_file[i:i+3] for i in range(0, len(raw_file), 3)]
  5.     for x in range(1, 123):
  6.         code = []
  7.         for i in range(3):
  8.             if Counter([int(target[j][i]) ^ x for j in range(len(target)-1)]).most_common(1)[0][0] in range(97, 122):
  9.                 code.append(Counter([int(target[j][i]) ^ x for j in range(len(target)-1)]).most_common(1)[0][0])
  10.             else:
  11.                 break
  12.         if len(code) == 3:
  13.             print(code, [chr(_) for _ in code], "".join([chr(int(each) ^ code[ix % 3]) for ix, each in enumerate(raw_file)])[:40])
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-8-22 09:01:40 | 显示全部楼层
我拿到的文章是关于巴塞尔问题的……
难不成每个人拿到的题目都不一样
原文节选
An extract taken from the introduction of one of Euler's most celebrated papers, "De summis serierum reciprocarum" [On the sums of series of reciprocals]: I have recently found, quite unexpectedly, an elegant expression for the entire sum of this series 1 + 1/4 + 1/9 + 1/16 + ……

评分

参与人数 1荣誉 +5 鱼币 +5 贡献 +3 收起 理由
永恒的蓝色梦想 + 5 + 5 + 3 发个代码呗

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-8-23 07:35:16 | 显示全部楼层
129448

Process returned 0 (0x0)   execution time : 0.031 s
Press any key to continue.
想法和4#相同,我也想到了字母频率,然而没注意到空格,先用e尝试了……
对每隔两个字母形成的子列分析,子列使用同一个字母加密。
则,对每个子列中频率最高的密文字符按位异或空格,便可得到密钥
  1. #include<iostream>
  2. #include<vector>
  3. #include<cstdio>
  4. #include<cctype>
  5. using namespace std;
  6. const int M = 1<<7;
  7. const int mode = 0; //0代表密钥第一位,1代表第二位,2代表第三位
  8. //取0时可得出答案

  9. int f[M] = {0};     //频率表
  10. vector<char> v,u;
  11. //以下是分析后得到的
  12. const int k[] = {69,88,80}; //对应出现频次最高的密文(即' '),
  13. char p_key[] = "exp";//分析得到的可能密钥

  14. void analyze(){
  15.   for (int i = 0;i < M;i++)
  16.     if (f[i]) printf("%d:%d\n",i,f[i]);

  17.   for (int i = 0;i < u.size();i++){
  18.     int r = u[i] % 3;
  19.     if (islower(v[u[i] ]))  printf("%d:%c\n",r+1,v[u[i] ]);
  20.   }
  21. }

  22. void print(){
  23.   for (int i = 0;i < v.size();i++)
  24.     cout << v[i];
  25.   cout << endl;
  26. }

  27. int main(){
  28.   freopen("i.in","r",stdin);
  29.   int t,ans = 0;

  30.   while(scanf("%d,",&t) == 1)
  31.     v.push_back(t);

  32.   for (int i = mode;i < v.size();i+=3){
  33.     f[v[i] ]++;
  34.     if (v[i] == k[mode])  u.push_back(i);

  35.     v[i] ^= ' ';
  36.   }

  37.   //analyze();

  38.   for (int i = 0;i < v.size();i+=3){
  39.     v[i] ^= ' ';

  40.     for (int j = 0;j < 3;j++){
  41.       v[i+j] ^= p_key[j];
  42.       ans += v[i+j];
  43.     }
  44.   }
  45.   cout << ans << endl;
  46.   //print();
  47.   return 0;
  48. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-3-29 03:51

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表