- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
几天来我一直在这里发布类似的问题,但似乎我没有问对问题,如果我的 XOR 问题让您筋疲力尽,请原谅我:D。
要点 - 我有两个十六进制字符串,我想对这些字符串进行异或运算,以便每个字节分别进行异或运算(即每对数字分别进行异或运算)。我想在 python 中执行此操作,并且我希望能够拥有不同长度的字符串。我将手动做一个例子来说明我的观点(我使用代码环境是因为它允许我在我想要的地方放置空格):
Input:
s1 = "48656c6c6f"
s2 = "61736b"
Encoding in binary:
48 65 6c 6c 6f = 01001000 01100101 01101100 01101100 01101111
61 73 6b = 01100001 01110011 01101011
XORing the strings:
01001000 01100101 01101100 01101100 01101111
01100001 01110011 01101011
00001101 00011111 00000100
Converting the result to hex:
00001101 00011111 00000100 = 0d 1f 04
Output:
0d1f04
因此,总而言之,我希望能够输入两个长度不同或相等的十六进制字符串(这些字符串通常是以十六进制编码的 ASCII 字母),并获得它们的异或,以便每个字节分别进行异或。
最佳答案
使用binascii.unhexlify()
将您的十六进制字符串转换为二进制数据,然后将其异或,返回到 binascii.hexlify()
的十六进制:
>>> from binascii import unhexlify, hexlify
>>> s1 = "48656c6c6f"
>>> s2 = "61736b"
>>> hexlify(''.join(chr(ord(c1) ^ ord(c2)) for c1, c2 in zip(unhexlify(s1[-len(s2):]), unhexlify(s2))))
'0d1f04'
实际 XOR 应用于解码数据的每个字节(使用 ord()
和 chr()
来回整数)。
请注意,就像在您的示例中一样,我将 s1
截断为与 s2
相同的长度(忽略 s1
开头的字符) .您可以通过循环字节使用较短的 key s2
对 所有 s1
进行编码:
>>> from itertools import cycle
>>> hexlify(''.join(chr(ord(c1) ^ ord(c2)) for c1, c2 in zip(unhexlify(s1), cycle(unhexlify(s2)))))
'2916070d1c'
您不必使用unhexlify()
,但它比遍历s1
和s2 容易得多
一次 2 个字符,并使用 int(twocharacters, 16)
将其转换为用于 XOR 运算的整数值。
上面的Python 3版本稍微轻一点;使用 bytes()
而不是 str.join()
并且您可以删除 chr()
和 ord()
当你直接迭代整数时调用:
>>> from binascii import unhexlify, hexlify
>>> s1 = "48656c6c6f"
>>> s2 = "61736b"
>>> hexlify(bytes(c1 ^ c2 for c1, c2 in zip(unhexlify(s1[-len(s2):]), unhexlify(s2))))
b'0d1f04'
>>> from itertools import cycle
>>> hexlify(bytes(c1 ^ c2 for c1, c2 in zip(unhexlify(s1), cycle(unhexlify(s2)))))
b'2916070d1c'
关于python - 如何异或两个十六进制字符串,以便每个字节分别异或?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17404690/
我有一个消息 static int[] message = { 0x01, 0x10, 0x00, 0x01, // port addres 01 - 08
如何将十进制转换为以下格式的十六进制(至少两位,零填充,不带 0x 前缀)? 输入:255 输出:ff 输入:2 输出:02 我尝试了 hex(int)[2:] 但它似乎显示了第一个示例而不是第二个示
这个问题已经有答案了: 已关闭12 年前。 Possible Duplicate: Large numbers in Pascal (Delphi) 我正在尝试将 66 位值转换为十进制。 我注意到d
给定一个十进制数字列表,如何将每个数字转换为其等效的十六进制值,反之亦然? 例如: (convert2hex 255 64 64); ->(FF 40 40) (convert2dec FF 40 4
var color = Math.floor(Math.random() * 16777215).toString(16); var hex = Number.parseInt(col
我一直被教导 0-9 代表 0 到 9 的值,A、B、C、D、E、F 代表 10-15。 我看到这种格式 0x00000000,它不适合十六进制模式。有没有导游或导师可以解释一下? 我在谷歌上搜索了十
我目前正尝试像十六进制编辑器一样将文件读取为十六进制值。为了解释这个问题,让我们假设我有一个test.txt,里面有一个简单的“Hello world”。我正在尝试使用接近以下代码的程序以十六进制形式
我正在尝试获取元素背景颜色 $(document).ready(function(){ $.each('.log-widget',function(){ console.log($(t
0x40130020的十六进制值是 2.296883 的浮点值, 使用本网站 http://gregstoll.dyndns.org/~gregstoll/floattohex/ .这如何实现到 Lu
就目前情况而言,这个问题不太适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、民意调查或扩展讨论。如果您觉得这个问题可以改进并可能重新开放,vis
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
谁能解释一下我们如何计算十六进制浮点常量的值。我在看书,发现0x0.3p10代表值192。 最佳答案 指数仍以十进制表示,但底数为二,尾数为十六进制。 所以 0.3P10 是 (3 × 16−1) ×
我正在尝试创建一个标签云,需要帮助来创建一个函数来计算应用于每个标签链接所需的颜色。 我有 3 个变量: 单个标签重要性(从 0.1 到 1) 最大(最重要)的标签颜色(十六进制代码,例如“fff00
大家好,我想发送尽可能短的字符串/值。如果我有以下内容 1)l23k43i221j44h55uui6n433bb4 2)124987359824369785493584379 3)kla^askdja
我知道你会写... background-color: #ff0000; ...如果你想要红色的东西。 你可以写... background-color: rgba(255, 0, 0, 0.5);
我有一些传递地理位置坐标的二进制数据流 - 纬度和经度。我需要找到它们编码的方法。 4adac812 = 74°26.2851' = 74.438085 2b6059f9 = 43°0.2763'
我想从 my_table 中选择 family,其中 family LIKE '%HEX(9D)' 家庭十六进制格式以 9D 十六进制结尾 我将excel文件转换为sqlite数据库但是 我的一些数据
我有一组二进制配置文件,每个文件有三个版本——每个文件的原始版本和两个不同修改的版本。我需要能够同时看到两个版本和原始版本之间的差异。 我需要的是一个二进制文件的三向差异工具。通过相当费力的谷歌搜索,
我正在尝试将(可变长度)十六进制字符串转换为带符号整数(我需要正值或负值)。 [Int16] [int 32]和[int64] 似乎可以在2,4+字节长的十六进制字符串上正常工作,但我在使用3个字节的
如何将十六进制的 unicode 写入 Facebook“您在想什么”框? 我尝试过写: \u00B9 "\u00B9" ¹ "¹" 到目前为止没有任何效果 (让我补充一下,我是在 M
我是一名优秀的程序员,十分优秀!