我希望任何人都能提供一些关于如何在Python中实现这个算法的输入:
“混淆数据的一种常见技术是使用排他的-或(xor)和一些密钥;它是廉价和对称的。当在文件格式(如包含大量空值的可移植可执行文件)上使用时会出现问题,因为xor‘’ing和您的键最终会写入您的键。一个稍微复杂的算法是实现一个线性反馈移位寄存器,以生成一个密钥流,它将与数据进行xor‘’ed。对于这个版本,您将使用8位移位寄存器。移位寄存器用一个值初始化,并逐步生成密钥流。键的下一个字节在每一步之后从移位寄存器中读取。
按步骤登记:
函数应该遵循以下签名: String (int dataLength,字节*initial_value,无符号char initial_value)示例测试: data:'apple‘key: 0x61结果:'\xC4\xB7\x86\x17\xCD’数据:'\xC4\xB7\x86\x17\xCD‘键: 0x61结果:'apple’
我不知道如何开始这方面的工作,我的Python知识还处于初级阶段。
发布于 2015-11-12 05:50:04
在Python3中,bytes对象已经知道它的长度,因此不需要显式地将它传递给函数
>>> def LFSR(data, iv):
... for c in data:
... lsb = 1 & iv # save the rightmost bit
... iv >>= 1 # shift
... if lsb:
... iv ^= 0x95
... yield c ^ iv # yield one byte
...
>>> print(bytes(LFSR(b'apple', 0x61)))
b'\xc4\xb7\x86\x17\xcd'
>>> print(bytes(LFSR(b'\xc4\xb7\x86\x17\xcd', 0x61)))
b'apple'这是一个生成器,而不是一个函数,因为它使用yield而不是return。我们可以说LFSR是ints的一个可迭代的。
如果您查看帮助的字节数
>>> help(bytes)
class bytes(object)
| bytes(iterable_of_ints) -> bytes
| bytes(string, encoding[, errors]) -> bytes
| bytes(bytes_or_buffer) -> immutable copy of bytes_or_buffer
| bytes(int) -> bytes object of size given by the parameter initialized with null bytes
| bytes() -> empty bytes object
|
| Construct an immutable array of bytes from:
| - an iterable yielding integers in range(256)
| - a text string encoded using the specified encoding
| - any object implementing the buffer API.
| - an integer您可以看到,我们使用的是第一个构造函数:bytes(iterable_of_ints)
https://stackoverflow.com/questions/33664888
复制相似问题