如何组合十六进制值并保持其大小相同?

发布于 2025-01-11 02:33:14 字数 2956 浏览 0 评论 0原文

所以我从三个不同的源中随机提取 256 个字节的十六进制值。

x = 0bb2b14357319415b4ca04dc297be2d9d54ff4f9292f5b8511cb0a53cf60b4be407ff95ec416f57141e2f30203a6c9e4453356dea928b166b693c122bca89fe3bb434e7c4ea7fc172151b6e9525e115843884a5d27eba06a42dc8411cdc3d9b9a8147780b95a49eeccf2d6af4307bf95b36c25539cadea67efbe4c9fe7e2db8199eba27f2c9543009589b28c36d89f6659b72f78ba3fd445c29234a167ffba7dc4c57e5635305454de3475000bd3012b1c21f18ec2b60d515fe2f82e9f602b1c6fe1aa7b55ffc98250ee24ff56f11e24f49b0a53e476c459ab8261a582dd338cebd0fec37f853e2011a9d3b0a581b3ff5d16387b250d201dc2774a5ed155f910
y = bc7bfe6577fddb25d706ca26d99694bae4b7a15349f96327324e34d25a07a60246e483e86bfc71d71007ad2b4fd88a4de4fde1295b6878cf24399fce786f747620980fb7750db8b0f28e1874d2bebfc81a6267864e8404bc42c29e752ce7b1716efd015ee4bb4928e70fac58cc79a7b9b5e86f38a11930b5be45a5cb4452b3d88b40d6beec3f1b6fcfae93be4868ab5f9ce33895485baef60a518aa63147ec42946163d250efc3f50f274241897249f6b6ce8a45e4ec22acd45b93ff62097f7c925d03c44874b934d8150add566c117402109c3ae1dce117a323dcd4429bc8bf83f1386976bc7c8179173ee6608df2833d6ba55178dcdacc144a730b296c8404
z = 2eefad7ef4d204329e23ba4e5ab76fc011e343579d6cc1eea5c3db33f5982c0481276ff077037ba63f67ad9ca202de91c7cad0f559a3b492dbb82252796c21f657265cc3bc58fcb57db809c25b17c4840feb41e29f056f83f121a0209469c38cf5cee0d6bbe8a081d61c01baed7bcd91c907c43c04b1a17364c7a7cc262f51db5897ec85109d62cb50e25636673cf464d4f0d26290e18a06903bfed7956ae1cfebf6b763ad83bc7288f542fcd7fd06a307bcb01c6539c1b241fee526f4c50b6f12af3f57714861b7428c7214f18715fe2a14720700537efd61e3978fad8a6f04eb03795da43ef0474756f793850c97dc841dfe6d6a6b3e4a25b48c7accbd3849

然后我使用此 xor 函数对它们进行异或


def xor(X, Y):
   return "".join('{:02x}'.format(ord(a) ^ ord(b)) for (a, b) in zip(X, Y))

,得到 1025 字节的十六进制值。实际上,我在这里的某个地方读到,如果我想获得相同大小的 256 字节来组合十六进制值,我需要对它们进行异或,但是,看起来情况并非如此。我想知道是否有人对如何组合三个十六进制值并保持其压缩为相同大小有任何建议?谢谢

x_xor_y_xor_z = 5201555004540206020055555d560300060353575355565556000e545c060658010156510705530a0600545f03510a0202035707035551015607060703025257000652525e0a505d555657555104530605015505075702500455050e5b585150510155575007565c545b04005509555050020a0a5a575157555b575e0e52520550520d0b0403555403505153045b530754030d54530e520d510057005357560005520e0a02560d5206525d565104540200000751015106045107060406085508575d575007060d55070d57030000575d065456540555545e5750070e03515b0c0006535b0453060b5852505d565154020703565055565a045103500006515c09015b51520504550357000a53055106565a53595c5b015a06070e5200580403500c5a0704015e0e0d565906040551520353530c030b55000705065251070203565a0055040156510406055656560753015403010303070401085b530104085454535551545e500c500606075502565452515250505f0b54030f540609050406000f54505551525456010d515201000b06510854500255020200005052005405005606085209020652000553550605040e00510a01525205010c005d06500b5a055d510201555d550a00505a5604060a010608500e575607065705005504015e5506000754520d0253050d5407565152075206035603520507560803565e0d0104

so I pull random 256 bytes each from three different sources in hex value.

x = 0bb2b14357319415b4ca04dc297be2d9d54ff4f9292f5b8511cb0a53cf60b4be407ff95ec416f57141e2f30203a6c9e4453356dea928b166b693c122bca89fe3bb434e7c4ea7fc172151b6e9525e115843884a5d27eba06a42dc8411cdc3d9b9a8147780b95a49eeccf2d6af4307bf95b36c25539cadea67efbe4c9fe7e2db8199eba27f2c9543009589b28c36d89f6659b72f78ba3fd445c29234a167ffba7dc4c57e5635305454de3475000bd3012b1c21f18ec2b60d515fe2f82e9f602b1c6fe1aa7b55ffc98250ee24ff56f11e24f49b0a53e476c459ab8261a582dd338cebd0fec37f853e2011a9d3b0a581b3ff5d16387b250d201dc2774a5ed155f910
y = bc7bfe6577fddb25d706ca26d99694bae4b7a15349f96327324e34d25a07a60246e483e86bfc71d71007ad2b4fd88a4de4fde1295b6878cf24399fce786f747620980fb7750db8b0f28e1874d2bebfc81a6267864e8404bc42c29e752ce7b1716efd015ee4bb4928e70fac58cc79a7b9b5e86f38a11930b5be45a5cb4452b3d88b40d6beec3f1b6fcfae93be4868ab5f9ce33895485baef60a518aa63147ec42946163d250efc3f50f274241897249f6b6ce8a45e4ec22acd45b93ff62097f7c925d03c44874b934d8150add566c117402109c3ae1dce117a323dcd4429bc8bf83f1386976bc7c8179173ee6608df2833d6ba55178dcdacc144a730b296c8404
z = 2eefad7ef4d204329e23ba4e5ab76fc011e343579d6cc1eea5c3db33f5982c0481276ff077037ba63f67ad9ca202de91c7cad0f559a3b492dbb82252796c21f657265cc3bc58fcb57db809c25b17c4840feb41e29f056f83f121a0209469c38cf5cee0d6bbe8a081d61c01baed7bcd91c907c43c04b1a17364c7a7cc262f51db5897ec85109d62cb50e25636673cf464d4f0d26290e18a06903bfed7956ae1cfebf6b763ad83bc7288f542fcd7fd06a307bcb01c6539c1b241fee526f4c50b6f12af3f57714861b7428c7214f18715fe2a14720700537efd61e3978fad8a6f04eb03795da43ef0474756f793850c97dc841dfe6d6a6b3e4a25b48c7accbd3849

Then I xor each of them together using this xor function


def xor(X, Y):
   return "".join('{:02x}'.format(ord(a) ^ ord(b)) for (a, b) in zip(X, Y))

and I got 1025 bytes in hex value. Actually, I read somewhere in here that if I want to get the same size of 256 bytes for combing hex value I need to XOR them, however, looks like this is not the case. I wonder if anyone has any advice on how to combine three hex value and keep it compressed in the same size? Thanks

x_xor_y_xor_z = 5201555004540206020055555d560300060353575355565556000e545c060658010156510705530a0600545f03510a0202035707035551015607060703025257000652525e0a505d555657555104530605015505075702500455050e5b585150510155575007565c545b04005509555050020a0a5a575157555b575e0e52520550520d0b0403555403505153045b530754030d54530e520d510057005357560005520e0a02560d5206525d565104540200000751015106045107060406085508575d575007060d55070d57030000575d065456540555545e5750070e03515b0c0006535b0453060b5852505d565154020703565055565a045103500006515c09015b51520504550357000a53055106565a53595c5b015a06070e5200580403500c5a0704015e0e0d565906040551520353530c030b55000705065251070203565a0055040156510406055656560753015403010303070401085b530104085454535551545e500c500606075502565452515250505f0b54030f540609050406000f54505551525456010d515201000b06510854500255020200005052005405005606085209020652000553550605040e00510a01525205010c005d06500b5a055d510201555d550a00505a5604060a010608500e575607065705005504015e5506000754520d0253050d5407565152075206035603520507560803565e0d0104

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

残月升风 2025-01-18 02:33:14

您的 XOR 函数使用 ord() 将字符 (0-F) 转换为 ASCII 值,例如 A是 65。然后对这些数字进行异或并创建大值(高于 16)。然后这些值被转换回十六进制。但是,由于使用了大量数字,因此该十六进制值有两个字符,这意味着您的结果大于输入。

为了解决这个问题,我将用 int(x, 16) 替换 ord() 。此函数将您的字符转换为其“真实”值(例如,A 为 10)。如果对这些值进行异或运算,您将得到 0 到 15 之间的数字,该数字可以转换为单个十六进制字符。

以下是该函数的新代码:

def xor(X, Y):
    return "".join('{:x}'.format(int(a,16) ^ int(b,16)) for (a, b) in zip(X, Y))

使用 xy 变量,您将获得以下相同大小的字符串:

b7c94f2620cc4f3063cccefaf0ed766331f855aa60d638a223853e81956712bc069b7ab6afea84a651e55e294c7e43a9a1ceb7f7f240c9a992aa5eecc4c7eb959bdb41cb3baa44a7d3dfae9d80e0ae9059ea2ddb696fa4d6001e1a64e12468c8c6e976de5de100c62bfd7af78f7e182c06844a6b3db4dad251fbe954a3b0685912ab74c1c0aa586f5a2721327eb03439c55417edf2647ab3c8c3be0756b8563f50a41d8465df97a1d113374182a148ddaaef7bcb265a2ffd8bb96bd1fd695460fdbca9bf1d8b70b688fb2e22009d0f50f68b966905aa254e08a1bd71c046fb336821c6aa093942a168beed56c50c417c607d9d2a5dd1fad1d63d3955f8397d14

编辑:因为您有多个值,您还可以使用接受任意数量的参数并对它们进行异或的函数,如下所示:

from functools import reduce
from operator import xor

def xor_multiple(*arg):
    return "".join(['{:x}'.format(reduce(xor, map(lambda x: int(x, 16),t))) for t in zip(*arg)])

xor_multiple(x, y, z)

Your XOR function converts the characters (0-F) to ASCII values with ord(), where e.g. A is 65. Then these numbers are XORed and create large values (higher than 16). These values are then converted back to hex. However, this hex value has two characters as large numbers are used, which means that your result is larger than the inputs.

To solve this problem I would replace ord() with int(x, 16). This function converts your characters to their "real" values (e.g. A is 10). If these values are XORed you get a digit between 0 and 15, which can be converted to a single hex character.

Following is the new code of the function:

def xor(X, Y):
    return "".join('{:x}'.format(int(a,16) ^ int(b,16)) for (a, b) in zip(X, Y))

With your x and y variables you get the following same size string:

b7c94f2620cc4f3063cccefaf0ed766331f855aa60d638a223853e81956712bc069b7ab6afea84a651e55e294c7e43a9a1ceb7f7f240c9a992aa5eecc4c7eb959bdb41cb3baa44a7d3dfae9d80e0ae9059ea2ddb696fa4d6001e1a64e12468c8c6e976de5de100c62bfd7af78f7e182c06844a6b3db4dad251fbe954a3b0685912ab74c1c0aa586f5a2721327eb03439c55417edf2647ab3c8c3be0756b8563f50a41d8465df97a1d113374182a148ddaaef7bcb265a2ffd8bb96bd1fd695460fdbca9bf1d8b70b688fb2e22009d0f50f68b966905aa254e08a1bd71c046fb336821c6aa093942a168beed56c50c417c607d9d2a5dd1fad1d63d3955f8397d14

EDIT: As you have multiple values, you can also use a function that takes an arbitrary amount of arguments and XORs them like this:

from functools import reduce
from operator import xor

def xor_multiple(*arg):
    return "".join(['{:x}'.format(reduce(xor, map(lambda x: int(x, 16),t))) for t in zip(*arg)])

xor_multiple(x, y, z)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文