单字节XOR密码(python) [英] Single-Byte XOR Cipher (python)

查看:99
本文介绍了单字节XOR密码(python)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我目前正在学习的现代密码学课程。

This is for a modern cryptography class that I am currently taking.

挑战是cryptopals挑战3:单字节XOR密码,我正在尝试使用python 3可以帮助完成此操作。

The challenge is the cryptopals challenge 3: Single-Byte XOR Cipher, and I am trying to use python 3 to help complete this.

我知道我应该对字符串进行XOR并转换为英语。十六进制字符串是 1b37373331363f78151b7f2b783431333d78397828372d363c78373e783a393b3736,将其转换为十进制格式的 80674845337190290240905117429187545859274380033758542156654920679664283653682239286。

I know that I am supposed to XOR the string and converted to English. The hex string is "1b37373331363f78151b7f2b783431333d78397828372d363c78373e783a393b3736" which converts to "806748453371902409051174291875458592743800337585421566549206796642836053682239286" in decimal form.

我已针对多个十六进制字节组合(2个十六进制数字)对此进行了异或运算,但是我不知道如何将其转换为英语。

I have XOR'd this against multiple hex byte combinations (2 hex digits), but I do not know how to convert this into English. Is it just brute force and educated guessing at this point?

我知道ETAOIN SHRDLU,但实际上并没有那么大的帮助。

I know about ETAOIN SHRDLU, but this hasn't really been that helpful.

感谢您的时间和帮助。

已添加:
另外,我尝试了挑战#4,但此代码似乎无效。但这确实对挑战3有效,所以我感到困惑。

ADDED: Additionally, I tried Challenge #4 but this code does not seem to work. But it did work for Challenge #3 so I am confused.

挑战#3
挑战#4

推荐答案

以@falsetru的答案为基础,但仅显示具有最多空格字符的解码字符串:

Building on @falsetru's answer, but showing just the decoded string with the most space characters:

>>> encoded = '1b37373331363f78151b7f2b783431333d78397828372d363c78373e783a393b3736'
>>> import binascii
>>> nums = binascii.unhexlify(encoded)
>>> strings = (''.join(chr(num ^ key) for num in nums) for key in range(256))
>>> max(strings, key=lambda s: s.count(' '))
"Cooking MC's like a pound of bacon"

您可以使用 ETAOIN SHRDLU ( 权重是英语中12个最常用的字母的大概频率顺序),但是这里没有必要。

Instead of counting spaces, you could use ETAOIN SHRDLU ("the approximate order of frequency of the 12 most commonly used letters in the English language") for weights, but it's not necessary here.

顺便说一句,我想如果您已链接到挑战,那就很好了。

Btw, I think it would've been good if you had linked to the challenge.

编辑:或者,您可以尝试找到密钥(或一些最有前途的密钥),然后仅使用该键(或那几个键)。例如,假设计算空格将确定获胜者:

Alternatively, you can try to find the key (or a few most promising keys) and then only decode using that key (or those few keys). For example, assuming that counting the spaces will determine the winner:

>>> encoded = '1b37373331363f78151b7f2b783431333d78397828372d363c78373e783a393b3736'
>>> import binascii
>>> nums = binascii.unhexlify(encoded)
>>> key = max(nums, key=nums.count) ^ ord(' ')
>>> ''.join(chr(num ^ key) for num in nums)
"Cooking MC's like a pound of bacon"

这甚至可以很容易地手动完成(尽管挑战告诉您不要这样做)。

This could even easily be done by hand (though the challenge tells you not to do that).

这篇关于单字节XOR密码(python)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆