在 python 中对二进制文件进行异或

发布于 2025-01-07 03:41:53 字数 535 浏览 0 评论 0原文

我正在编写一个简单的哈希函数,它获取输入并创建一个 32 位哈希文件。我陷入了异或部分。我不确定应该如何编写异或函数。请帮我解决这个问题。这是我已经完成的代码:

import BitVector
import io
import math
import struct

if __name__ == "__main__":
    message = raw_input("your message:")
    f= open('file.dat','w')
    f.write(message)
    f.close()

    f = open('file.dat')
    while 1:
       r = f.readline(1)
       hx = r.encode("hex")
       data = bin(int(hx, 16))[2:]
       key = 11111111
       x = int(data) ^ int(key)
       print hex(x)
       if not r:break
    f.close()

I'm in the middle of writing a simple hash function which get the input and creates a 32 bit hash file. I'm stuck in the xor part. And i'm not sure how i should write the function for xor. please help me with this. here is the code that i have done yet:

import BitVector
import io
import math
import struct

if __name__ == "__main__":
    message = raw_input("your message:")
    f= open('file.dat','w')
    f.write(message)
    f.close()

    f = open('file.dat')
    while 1:
       r = f.readline(1)
       hx = r.encode("hex")
       data = bin(int(hx, 16))[2:]
       key = 11111111
       x = int(data) ^ int(key)
       print hex(x)
       if not r:break
    f.close()

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

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

发布评论

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

评论(2

玻璃人 2025-01-14 03:41:53

运算符 ^ 对您来说已经足够了。

>>> 2 ^ 1
3
>>> 3 ^ 1
2

您应该关心的是密钥的位大小和“异或”数据的位大小,以确保功能。

顺便说一句,异或运算应该只适用于整数。

import struct

key = 0xFEEEFEEE
with open('file', 'rb') as f:
    integer = f.read(4) # In fact, you could read all in.
    while len(integer) >= 4:
        # if integer is not a string longer than 4, next line crash.
        s, = struct.unpack('i', integer) # The return value is a tuple with integers.
        print key ^ s
        integer = f.read(4)

The operator ^ is quite enough for you.

>>> 2 ^ 1
3
>>> 3 ^ 1
2

All you should care about is the the bit size of your key and the bit size of data to 'xor', to make sure the functionality.

By the way, xor operations should just apply to integers.

import struct

key = 0xFEEEFEEE
with open('file', 'rb') as f:
    integer = f.read(4) # In fact, you could read all in.
    while len(integer) >= 4:
        # if integer is not a string longer than 4, next line crash.
        s, = struct.unpack('i', integer) # The return value is a tuple with integers.
        print key ^ s
        integer = f.read(4)
ま昔日黯然 2025-01-14 03:41:53

尝试使用它进行异或:

def xor_orig(data, key):
    return bool(data) ^ bool(key)

对于二进制异或,其中数据和密钥是二进制值并使用 ^ 字符,
您可以使用:

def xor_orig(data, key):
    return int(data,2) ^ int(key,2)


In [1]: data = '1010'
In [2]: key = '0101'    
In [3]: int(data,2) ^ int(key,2)
Out[3]: 15
In [4]: data = '10'    
In [5]: key = '01'    
In [6]: int(data,2) ^ int(key,2)
Out[6]: 3
In [7]: data = '10'    
In [8]: key = '10'    
In [9]: int(data,2) ^ int(key,2)
Out[9]: 0

让我知道这是否有帮助。

try using this for xoring:

def xor_orig(data, key):
    return bool(data) ^ bool(key)

For binary xoring, where data and key are binary values and using ^ character,
you can use:

def xor_orig(data, key):
    return int(data,2) ^ int(key,2)


In [1]: data = '1010'
In [2]: key = '0101'    
In [3]: int(data,2) ^ int(key,2)
Out[3]: 15
In [4]: data = '10'    
In [5]: key = '01'    
In [6]: int(data,2) ^ int(key,2)
Out[6]: 3
In [7]: data = '10'    
In [8]: key = '10'    
In [9]: int(data,2) ^ int(key,2)
Out[9]: 0

Let me know if this helps.

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