鱼C论坛

 找回密码
 立即注册
查看: 6432|回复: 29

[技术交流] python小练习(003):简单字符串加密解密(提高篇)

[复制链接]
发表于 2016-11-15 12:47:29 | 显示全部楼层 |阅读模式

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

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

x
昨天的python小练习中大家练习了简单字符串异或加密和解密的方法,如果还不知道的,可以点这里:传送门

相信大家都已经掌握了,那么今天就稍微提高一点难度。

题目是这样的:
已知:我有一组明文是由普通英语单词及数字符号(ascii码符号范围内)构成的文章,用一个由3个小写字母构成的常用单词(密钥)进行了异或加密,得到了一组密文。
你有办法在我不告诉你密钥的情况下,破解这组密文并且获取我的密钥吗?

密文如下:
  1. 91,33,6,22,85,41,28,6,30,22,25,78,28,19,78,57,26,6,29,89,78,16,29,15,3,1,11,1,85,95,90,85,95,83,60,0,83,1,6,22,85,12,22,18,7,29,27,7,29,18,78,7,29,11,83,34,1,1,17,78,18,25,28,22,20,10,10,85,11,11,28,29,7,16,10,93,85,38,22,85,25,18,6,78,4,28,26,27,85,41,28,17,66,83,20,0,23,85,6,22,85,25,18,6,78,52,26,10,93,85,92,83,61,11,83,2,15,0,85,7,29,85,26,27,16,78,17,16,9,26,27,0,26,27,9,83,2,7,7,29,78,52,26,10,93,85,93,83,61,11,83,22,28,22,20,26,22,17,78,22,3,11,1,12,26,27,28,0,20,85,26,27,16,28,22,85,7,0,91,78,61,26,26,27,28,0,20,85,11,11,28,29,7,6,78,7,29,15,7,85,6,22,85,10,26,17,0,84,1,78,30,20,5,22,91,78,71,85,34,26,19,11,83,28,26,0,16,2,21,85,25,18,6,78,26,27,78,27,28,3,95,85,15,29,17,78,7,29,7,0,85,2,26,19,11,83,18,7,5,16,29,83,25,7,20,29,26,83,1,1,83,16,24,22,7,23,28,27,11,93,85,91,83,33,6,22,85,2,26,18,6,7,85,29,27,28,0,22,6,78,7,29,28,28,0,9,27,85,26,27,16,78,23,20,28,24,27,11,0,6,66,83,20,0,23,85,26,27,16,78,23,20,28,24,27,11,0,6,78,16,20,0,83,27,11,5,16,28,83,16,22,7,28,0,20,0,7,0,29,78,26,1,64,83,67,78,52,26,10,83,6,11,29,1,78,57,26,6,29,85,26,27,16,78,49,20,30,7,28,29,7,85,89,83,1,1,83,1,11,31,25,78,22,3,11,1,12,1,29,16,78,18,23,1,6,1,78,7,29,11,83,25,7,20,29,26,83,6,1,83,1,6,18,1,78,22,3,11,1,12,1,29,16,78,30,28,9,27,1,78,17,16,2,26,16,24,22,85,12,22,22,15,6,6,11,83,26,8,83,29,7,0,85,26,22,6,26,26,24,1,29,12,64,83,77,78,57,26,6,29,85,6,26,24,29,22,25,8,83,2,15,0,85,0,28,1,78,7,29,11,83,25,7,20,29,26,72,85,6,22,85,25,18,6,78,28,27,2,10,85,15,83,2,7,7,27,11,0,6,78,7,26,78,7,29,11,83,25,7,20,29,26,93,85,87,83,33,6,22,85,1,29,16,78,4,29,1,83,28,29,83,1,6,22,85,26,1,0,11,83,25,7,20,29,26,95,85,25,27,26,78,20,28,24,22,6,78,31,28,9,27,1,78,7,26,78,22,3,11,1,12,1,29,16,66,83,2,15,0,85,9,28,28,0,20,85,26,28,85,13,28,24,11,83,28,0,7,26,78,7,29,11,83,2,1,1,25,10,93,85,95,67,85,44,6,1,78,18,25,26,27,26,27,20,29,78,7,29,11,83,2,1,1,25,10,83,2,15,0,85,3,18,17,11,83,1,6,1,26,27,20,29,78,27,28,3,95,85,26,27,16,78,4,26,28,31,17,78,23,28,10,29,82,26,83,7,11,16,26,9,29,28,20,22,85,6,26,24,78,4,29,11,29,85,6,22,85,13,18,24,11,93,85,95,66,85,43,5,16,0,83,28,0,83,29,7,0,85,1,4,27,78,31,20,0,23,85,15,29,17,78,18,24,1,29,18,78,27,28,29,83,26,25,29,85,30,22,26,30,31,16,66,83,29,11,83,2,15,0,85,0,28,1,78,18,22,13,22,5,26,22,17,64,83,68,92,83,55,27,7,85,26,28,85,15,31,25,78,4,29,1,83,23,11,31,28,11,5,16,10,83,29,7,30,85,15,29,17,78,18,22,13,22,5,26,22,17,78,27,28,3,95,85,6,22,85,9,18,3,11,83,1,6,22,85,28,26,18,6,7,85,26,28,85,12,22,22,1,30,16,78,16,29,7,31,17,28,22,27,78,28,19,78,52,26,10,93,85,95,64,85,58,27,16,23,83,20,28,22,85,28,22,23,1,1,27,79,83,33,6,26,6,78,26,6,78,29,26,26,83,20,78,3,29,23,0,28,13,18,25,78,17,28,28,7,29,78,1,16,29,6,25,26,26,27,9,83,19,28,28,24,78,27,0,3,18,27,78,3,20,29,0,28,1,29,85,1,1,85,30,31,20,0,95,85,26,27,28,29,83,7,11,17,28,28,7,29,78,16,26,3,22,6,78,21,7,1,30,85,41,28,17,64,66,65,78,32,26,78,7,29,11,83,34,1,1,17,78,17,16,13,18,24,11,83,29,27,30,20,0,83,20,0,23,85,2,26,3,11,23,85,6,22,7,11,83,26,0,83,16,15,1,1,6,83,20,3,28,27,9,83,0,29,93,85,38,22,85,25,18,6,78,21,0,2,31,85,1,21,85,27,29,19,15,26,25,7,29,18,78,31,26,24,22,85,15,29,17,78,21,20,7,7,29,8,6,25,0,22,6,29,93,85,47,29,17,78,4,16,78,27,20,24,22,85,29,22,16,0,83,29,7,0,85,9,31,26,28,10,89,78,7,29,11,83,18,2,28,7,23,83,26,8,83,1,6,22,85,1,29,25,23,83,38,1,29,85,1,21,85,26,27,16,78,53,20,26,27,16,28
复制代码

评分

参与人数 1鱼币 +5 收起 理由
SixPy + 5

查看全部评分

本帖被以下淘专辑推荐:

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

使用道具 举报

 楼主| 发表于 2016-11-15 12:52:07 | 显示全部楼层
本帖最后由 jerryxjr1220 于 2016-11-16 07:29 编辑

我的解答:利用英语文章中空格最多的特点,先用空格异或密文,然后统计转换后的字母哪个出现次数最多就是密钥。
  1. file = "91,33,6,22,85,41,28,6,30,22,25,78,28,19,78,57,26,6,29,89,78,16,29,15,3,1,11,1,85,95,90,85,95,83,60,0,83,1,6,22,85,12,22,18,7,29,27,7,29,18,78,7,29,11,83,34,1,1,17,78,18,25,28,22,20,10,10,85,11,11,28,29,7,16,10,93,85,38,22,85,25,18,6,78,4,28,26,27,85,41,28,17,66,83,20,0,23,85,6,22,85,25,18,6,78,52,26,10,93,85,92,83,61,11,83,2,15,0,85,7,29,85,26,27,16,78,17,16,9,26,27,0,26,27,9,83,2,7,7,29,78,52,26,10,93,85,93,83,61,11,83,22,28,22,20,26,22,17,78,22,3,11,1,12,26,27,28,0,20,85,26,27,16,28,22,85,7,0,91,78,61,26,26,27,28,0,20,85,11,11,28,29,7,6,78,7,29,15,7,85,6,22,85,10,26,17,0,84,1,78,30,20,5,22,91,78,71,85,34,26,19,11,83,28,26,0,16,2,21,85,25,18,6,78,26,27,78,27,28,3,95,85,15,29,17,78,7,29,7,0,85,2,26,19,11,83,18,7,5,16,29,83,25,7,20,29,26,83,1,1,83,16,24,22,7,23,28,27,11,93,85,91,83,33,6,22,85,2,26,18,6,7,85,29,27,28,0,22,6,78,7,29,28,28,0,9,27,85,26,27,16,78,23,20,28,24,27,11,0,6,66,83,20,0,23,85,26,27,16,78,23,20,28,24,27,11,0,6,78,16,20,0,83,27,11,5,16,28,83,16,22,7,28,0,20,0,7,0,29,78,26,1,64,83,67,78,52,26,10,83,6,11,29,1,78,57,26,6,29,85,26,27,16,78,49,20,30,7,28,29,7,85,89,83,1,1,83,1,11,31,25,78,22,3,11,1,12,1,29,16,78,18,23,1,6,1,78,7,29,11,83,25,7,20,29,26,83,6,1,83,1,6,18,1,78,22,3,11,1,12,1,29,16,78,30,28,9,27,1,78,17,16,2,26,16,24,22,85,12,22,22,15,6,6,11,83,26,8,83,29,7,0,85,26,22,6,26,26,24,1,29,12,64,83,77,78,57,26,6,29,85,6,26,24,29,22,25,8,83,2,15,0,85,0,28,1,78,7,29,11,83,25,7,20,29,26,72,85,6,22,85,25,18,6,78,28,27,2,10,85,15,83,2,7,7,27,11,0,6,78,7,26,78,7,29,11,83,25,7,20,29,26,93,85,87,83,33,6,22,85,1,29,16,78,4,29,1,83,28,29,83,1,6,22,85,26,1,0,11,83,25,7,20,29,26,95,85,25,27,26,78,20,28,24,22,6,78,31,28,9,27,1,78,7,26,78,22,3,11,1,12,1,29,16,66,83,2,15,0,85,9,28,28,0,20,85,26,28,85,13,28,24,11,83,28,0,7,26,78,7,29,11,83,2,1,1,25,10,93,85,95,67,85,44,6,1,78,18,25,26,27,26,27,20,29,78,7,29,11,83,2,1,1,25,10,83,2,15,0,85,3,18,17,11,83,1,6,1,26,27,20,29,78,27,28,3,95,85,26,27,16,78,4,26,28,31,17,78,23,28,10,29,82,26,83,7,11,16,26,9,29,28,20,22,85,6,26,24,78,4,29,11,29,85,6,22,85,13,18,24,11,93,85,95,66,85,43,5,16,0,83,28,0,83,29,7,0,85,1,4,27,78,31,20,0,23,85,15,29,17,78,18,24,1,29,18,78,27,28,29,83,26,25,29,85,30,22,26,30,31,16,66,83,29,11,83,2,15,0,85,0,28,1,78,18,22,13,22,5,26,22,17,64,83,68,92,83,55,27,7,85,26,28,85,15,31,25,78,4,29,1,83,23,11,31,28,11,5,16,10,83,29,7,30,85,15,29,17,78,18,22,13,22,5,26,22,17,78,27,28,3,95,85,6,22,85,9,18,3,11,83,1,6,22,85,28,26,18,6,7,85,26,28,85,12,22,22,1,30,16,78,16,29,7,31,17,28,22,27,78,28,19,78,52,26,10,93,85,95,64,85,58,27,16,23,83,20,28,22,85,28,22,23,1,1,27,79,83,33,6,26,6,78,26,6,78,29,26,26,83,20,78,3,29,23,0,28,13,18,25,78,17,28,28,7,29,78,1,16,29,6,25,26,26,27,9,83,19,28,28,24,78,27,0,3,18,27,78,3,20,29,0,28,1,29,85,1,1,85,30,31,20,0,95,85,26,27,28,29,83,7,11,17,28,28,7,29,78,16,26,3,22,6,78,21,7,1,30,85,41,28,17,64,66,65,78,32,26,78,7,29,11,83,34,1,1,17,78,17,16,13,18,24,11,83,29,27,30,20,0,83,20,0,23,85,2,26,3,11,23,85,6,22,7,11,83,26,0,83,16,15,1,1,6,83,20,3,28,27,9,83,0,29,93,85,38,22,85,25,18,6,78,21,0,2,31,85,1,21,85,27,29,19,15,26,25,7,29,18,78,31,26,24,22,85,15,29,17,78,21,20,7,7,29,8,6,25,0,22,6,29,93,85,47,29,17,78,4,16,78,27,20,24,22,85,29,22,16,0,83,29,7,0,85,9,31,26,28,10,89,78,7,29,11,83,18,2,28,7,23,83,26,8,83,1,6,22,85,1,29,25,23,83,38,1,29,85,1,21,85,26,27,16,78,53,20,26,27,16,28"
  2. f = file.split(',')
  3. list1,list2,list3 = f[::3],f[1::3],f[2::3]
  4. d1,d2,d3 = [],[],[]
  5. for s1 in list1:
  6.         d1.append(chr(int(s1) ^ ord(' ')))
  7. for s2 in list2:
  8.         d2.append(chr(int(s2) ^ ord(' ')))
  9. for s3 in list3:
  10.         d3.append(chr(int(s3) ^ ord(' ')))
  11. letter1 = max(d1,key=d1.count)
  12. letter2 = max(d2,key=d2.count)
  13. letter3 = max(d3,key=d3.count)

  14. key = letter1+letter2+letter3
  15. print 'key is :' + key

  16. def decodes(string, key):
  17.         string = string.split(',')
  18.         keys = key*(len(string)//len(key))+key[:len(string)%len(key)]
  19.         output = []
  20.         for i,s in enumerate(string):
  21.                 output.append(chr(int(s)^ord(keys[i])))
  22.         return ''.join(output)
  23.        
  24. print decodes(file,key)
复制代码


输出:
无标题.png

提示:
1. 可以用所有由3个小写字母组成的单词进行暴力破解。
2. 可以利用英语文章的字符特点进行匹配。
3. 其他词频分析的方法。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-11-15 14:24:18 | 显示全部楼层
先把明文写出;来,程序再优化。
  1. (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
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-11-15 17:03:46 | 显示全部楼层
冬雪雪冬 发表于 2016-11-15 14:24
先把明文写出;来,程序再优化。

非常正确,期待你的解法!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2016-11-15 19:23:38 | 显示全部楼层
sun
  1. from itertools import (islice, cycle,
  2.                        permutations as perm)                       

  3. lower = 'abcdefghijklmnopqrstuvwxyz'

  4. def fn_XOR(txt,key):
  5.     key=bytes(key,'ascii')
  6.     return (a^b for a,b in zip(islice(cycle(key),len(txt)),txt))

  7. txt = open('crypt_data.txt').read()
  8. txt1 = bytes(map(int, txt.split(',')))

  9. txt = txt1[:200]

  10. for key in perm(lower,3):
  11.     key = ''.join(key)
  12.     b = bytes(fn_XOR(txt,key))
  13.     cnt = b.count(b'The ') + b.count(b'the ')
  14.     if cnt:
  15.         print (key,cnt,b.decode())
  16.         print ('-'*70)
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-11-15 19:31:48 | 显示全部楼层

'crypt_data.txt' 就是在 1楼 的密文,保存在txt文件中。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-11-15 19:44:23 | 显示全部楼层
我的思路:
1.小写字母为97~122,将97~122分别与密码的0,3,6...;1,4,7...;2,5,8...分别xor,如果范围在32~126之间,即为正常可显字符,则把这个小写字母存下来,再将3个组合成单词----实在无法判断3个字母组合的是否是单词,想过从单词表查询,但引入单词表的开销也不小。分别作为密钥解密,直到有单词'THE ‘出现为之。
  1. import itertools
  2. list1 = [91,33,6,22,85,41,28,6,30,22,25,78,28,19,78,57,26,6,29,89,78,16,29,15,3,
  3. #这里省略了部分列表
  4.          20,26,27,16,28]
  5. list2 = [[], [], []]
  6. for n in range(3):
  7.     for i in range(97, 123):
  8.         for j in list1[n::3]:
  9.             if (i ^ j) < 32 or (i ^ j) > 126:
  10.                 break
  11.         else:
  12.             list2[n].append(i)
  13. list3 = [(i, j, k) for i in list2[0] for j in list2[1] for k in list2[2]]
  14. for i in list3:
  15.     str1 = ''
  16.     for j, k in zip(list1, itertools.cycle(i)):
  17.         str1 += chr(j ^ k)
  18.     if 'THE ' in str1.upper():
  19.         break
  20. print(str1)
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-11-15 20:04:19 | 显示全部楼层
两位版主大大都是用英文单词the作为突破口的,为啥你们不认为英语文章里其实应该是空格是用的最多的符号呢?我有提示明文是正常的英语文章哦。
@冬雪雪冬 @SixPy
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-11-15 20:09:59 | 显示全部楼层
jerryxjr1220 发表于 2016-11-15 20:04
两位版主大大都是用英文单词the作为突破口的,为啥你们不认为英语文章里其实应该是空格是用的最多的符号呢 ...

考虑过其他字符,包括空格,但怕仅一个空格容易误判。如果以空格数量多与一个阈值(如10个)作为判据,可能比'the‘更费时间。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-11-15 20:10:28 | 显示全部楼层
jerryxjr1220 发表于 2016-11-15 20:04
两位版主大大都是用英文单词the作为突破口的,为啥你们不认为英语文章里其实应该是空格是用的最多的符号呢 ...

本来想写词频统计的,试了一下 'Tthe ',发现能很好的区分无效组合。
就懒得写完整的程序了~
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-11-15 20:21:27 | 显示全部楼层
冬雪雪冬 发表于 2016-11-15 20:09
考虑过其他字符,包括空格,但怕仅一个空格容易误判。如果以空格数量多与一个阈值(如10个)作为判据,可 ...

其实不会啊,只要是正常的英语文章肯定是空格是最多的符号。
你试一下就知道了,用空格和密文异或来还原,然后判断重复次数最多的单词就是秘钥,速度很快的。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-11-15 21:23:25 | 显示全部楼层
jerryxjr1220 发表于 2016-11-15 20:21
其实不会啊,只要是正常的英语文章肯定是空格是最多的符号。
你试一下就知道了,用空格和密文异或来还原 ...

按你的提示重新写了。
  1. list1 = [ 1楼的数据]
  2. n = [[], [], []]
  3. for x in range(3):
  4.     list2 = list1[x::3]
  5.     temp = []
  6.     m = 0
  7.     for i in list2:
  8.         if i not in temp:
  9.             temp.append(i)
  10.             if list2.count(i) > m:
  11.                 m = list2.count(i)
  12.                 n[x] = i
  13. n = [x ^ 32 for x in n] #得到密钥
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-11-16 07:33:35 | 显示全部楼层
更新完毕
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-11-29 11:16:35 | 显示全部楼层
本帖最后由 776667 于 2016-11-29 11:18 编辑
  1. plaintext = "91,33,6,22,85,41,28,6,30,22,25,78,28,19,78,57,26,6,29,89,78,16,29,15,3,1,11,1,85,95,90,85,95,83,60,0,83,1,6,22,85,12,22,18,7,29,27,7,29,18,78,7,29,11,83,34,1,1,17,78,18,25,28,22,20,10,10,85,11,11,28,29,7,16,10,93,85,38,22,85,25,18,6,78,4,28,26,27,85,41,28,17,66,83,20,0,23,85,6,22,85,25,18,6,78,52,26,10,93,85,92,83,61,11,83,2,15,0,85,7,29,85,26,27,16,78,17,16,9,26,27,0,26,27,9,83,2,7,7,29,78,52,26,10,93,85,93,83,61,11,83,22,28,22,20,26,22,17,78,22,3,11,1,12,26,27,28,0,20,85,26,27,16,28,22,85,7,0,91,78,61,26,26,27,28,0,20,85,11,11,28,29,7,6,78,7,29,15,7,85,6,22,85,10,26,17,0,84,1,78,30,20,5,22,91,78,71,85,34,26,19,11,83,28,26,0,16,2,21,85,25,18,6,78,26,27,78,27,28,3,95,85,15,29,17,78,7,29,7,0,85,2,26,19,11,83,18,7,5,16,29,83,25,7,20,29,26,83,1,1,83,16,24,22,7,23,28,27,11,93,85,91,83,33,6,22,85,2,26,18,6,7,85,29,27,28,0,22,6,78,7,29,28,28,0,9,27,85,26,27,16,78,23,20,28,24,27,11,0,6,66,83,20,0,23,85,26,27,16,78,23,20,28,24,27,11,0,6,78,16,20,0,83,27,11,5,16,28,83,16,22,7,28,0,20,0,7,0,29,78,26,1,64,83,67,78,52,26,10,83,6,11,29,1,78,57,26,6,29,85,26,27,16,78,49,20,30,7,28,29,7,85,89,83,1,1,83,1,11,31,25,78,22,3,11,1,12,1,29,16,78,18,23,1,6,1,78,7,29,11,83,25,7,20,29,26,83,6,1,83,1,6,18,1,78,22,3,11,1,12,1,29,16,78,30,28,9,27,1,78,17,16,2,26,16,24,22,85,12,22,22,15,6,6,11,83,26,8,83,29,7,0,85,26,22,6,26,26,24,1,29,12,64,83,77,78,57,26,6,29,85,6,26,24,29,22,25,8,83,2,15,0,85,0,28,1,78,7,29,11,83,25,7,20,29,26,72,85,6,22,85,25,18,6,78,28,27,2,10,85,15,83,2,7,7,27,11,0,6,78,7,26,78,7,29,11,83,25,7,20,29,26,93,85,87,83,33,6,22,85,1,29,16,78,4,29,1,83,28,29,83,1,6,22,85,26,1,0,11,83,25,7,20,29,26,95,85,25,27,26,78,20,28,24,22,6,78,31,28,9,27,1,78,7,26,78,22,3,11,1,12,1,29,16,66,83,2,15,0,85,9,28,28,0,20,85,26,28,85,13,28,24,11,83,28,0,7,26,78,7,29,11,83,2,1,1,25,10,93,85,95,67,85,44,6,1,78,18,25,26,27,26,27,20,29,78,7,29,11,83,2,1,1,25,10,83,2,15,0,85,3,18,17,11,83,1,6,1,26,27,20,29,78,27,28,3,95,85,26,27,16,78,4,26,28,31,17,78,23,28,10,29,82,26,83,7,11,16,26,9,29,28,20,22,85,6,26,24,78,4,29,11,29,85,6,22,85,13,18,24,11,93,85,95,66,85,43,5,16,0,83,28,0,83,29,7,0,85,1,4,27,78,31,20,0,23,85,15,29,17,78,18,24,1,29,18,78,27,28,29,83,26,25,29,85,30,22,26,30,31,16,66,83,29,11,83,2,15,0,85,0,28,1,78,18,22,13,22,5,26,22,17,64,83,68,92,83,55,27,7,85,26,28,85,15,31,25,78,4,29,1,83,23,11,31,28,11,5,16,10,83,29,7,30,85,15,29,17,78,18,22,13,22,5,26,22,17,78,27,28,3,95,85,6,22,85,9,18,3,11,83,1,6,22,85,28,26,18,6,7,85,26,28,85,12,22,22,1,30,16,78,16,29,7,31,17,28,22,27,78,28,19,78,52,26,10,93,85,95,64,85,58,27,16,23,83,20,28,22,85,28,22,23,1,1,27,79,83,33,6,26,6,78,26,6,78,29,26,26,83,20,78,3,29,23,0,28,13,18,25,78,17,28,28,7,29,78,1,16,29,6,25,26,26,27,9,83,19,28,28,24,78,27,0,3,18,27,78,3,20,29,0,28,1,29,85,1,1,85,30,31,20,0,95,85,26,27,28,29,83,7,11,17,28,28,7,29,78,16,26,3,22,6,78,21,7,1,30,85,41,28,17,64,66,65,78,32,26,78,7,29,11,83,34,1,1,17,78,17,16,13,18,24,11,83,29,27,30,20,0,83,20,0,23,85,2,26,3,11,23,85,6,22,7,11,83,26,0,83,16,15,1,1,6,83,20,3,28,27,9,83,0,29,93,85,38,22,85,25,18,6,78,21,0,2,31,85,1,21,85,27,29,19,15,26,25,7,29,18,78,31,26,24,22,85,15,29,17,78,21,20,7,7,29,8,6,25,0,22,6,29,93,85,47,29,17,78,4,16,78,27,20,24,22,85,29,22,16,0,83,29,7,0,85,9,31,26,28,10,89,78,7,29,11,83,18,2,28,7,23,83,26,8,83,1,6,22,85,1,29,25,23,83,38,1,29,85,1,21,85,26,27,16,78,53,20,26,27,16,28"

  2. def key_decode(list_x):
  3.     max_count = 0
  4.     max_count_temp = ''
  5.     for i in list_x:
  6.         if list_x.count(i) > max_count:
  7.             max_count = list_x.count(i)
  8.             max_count_temp = i
  9.     return chr(int(max_count_temp)^32)

  10. plaintext = plaintext.split(',')
  11. list_1 = plaintext[::3]
  12. list_2 = plaintext[1::3]
  13. list_3 = plaintext[2::3]
  14. key = key_decode(list_1) + key_decode(list_2) + key_decode(list_3)
  15. print(key)
复制代码


看了LZ提示才想出来的,但是总觉得这种办法还是有点玄学
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-11-29 11:44:59 | 显示全部楼层
776667 发表于 2016-11-29 11:16
看了LZ提示才想出来的,但是总觉得这种办法还是有点玄学

这题目其实只是给你一个思路的拓展,真正遇到加密的时候不可能预先告诉你用的是异或加密,也不可能告诉你我的秘钥的构成
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-11-29 11:48:20 | 显示全部楼层
对了,顺带一提,本题取自“欧拉计划第59题”,传送门
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-12-2 11:19:27 | 显示全部楼层
jerryxjr1220 发表于 2016-11-15 12:52
我的解答:利用英语文章中空格最多的特点,先用空格异或密文,然后统计转换后的字母哪个出现次数最多就是密 ...

问一下,
  1. keys = key*(len(string)//len(key))+key[:len(string)%len(key)]
复制代码

这个是通过密钥来将要解码的东西解码吗?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-12-2 11:22:54 | 显示全部楼层
776667 发表于 2016-11-29 11:16
看了LZ提示才想出来的,但是总觉得这种办法还是有点玄学
  1. chr(int(max_count_temp)^32)
复制代码

请问
为什么要与32做异或?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-12-2 13:22:44 | 显示全部楼层
墨菲斯托 发表于 2016-12-2 11:19
问一下,
这个是通过密钥来将要解码的东西解码吗?

这个是为了把秘钥复制成与明文同样的长度
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-12-2 13:23:20 | 显示全部楼层
墨菲斯托 发表于 2016-12-2 11:22
请问
为什么要与32做异或?

asc码的32就是空格
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-17 03:02

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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