【发布时间】:2020-09-02 10:34:55
【问题描述】:
对于我 10 岁的儿子正在尝试的 Python 项目,谁能给我一些广泛的指导?我正在寻找特定的编码解决方案,但我希望这是一个提出问题的好地方。我想看看我的儿子是否对他的编码项目感兴趣,以及是否有一种相对简单的方法让他学习正确的步骤。或者,对于一个喜欢阅读和尝试各种编码项目只是为了好玩的 10 岁孩子来说,这只是一个不合群的东西吗?如您所见,我不是编码员,对此类项目知之甚少,因此我们将不胜感激!
我儿子喜欢密码学,他告诉我他尝试了下面的 Python 代码。他希望构建一个类似海绵的函数来加密按摩,使其无法被解密。这是受他的书“Serious Cryptography”(J. Aumasson 着)中的一个章节启发的,该章节的标题是“Permutation-based Hash Functions: Sponge Functions”。当他运行他编写的代码时,他收到错误消息“TypeError: unsupported operand type(s) for
非常感谢!亚历山大
这是他的代码:
import math
import textwrap
plaintext = raw_input("The value to be hashed: ") # Get the user to input the data to be hashed
nonce = raw_input("The nonce to be used: ") # Get the user to input the nonce to be used
key = raw_input("The key to be used: ") # Get the user to input the key to be used
blocks = textwrap.wrap(plaintext, 16) # Split the string into 128-bit blocks
if len(blocks[len(blocks)-1]) < 16: # Check if the last block is less than 128 bits
while len(blocks[len(blocks)-1]) < 16: # Keep iterating the following code
blocks[len(blocks)-1] += "." # Add padding to the end of the block to make it 128-bit
sponge = nonce # Set the sponge's initial state to that of the nonce
for j in blocks: # Absorb all of the blocks
sponge = (sponge << 128) + j # Concatenate the current sponge value and the block
sponge = textwrap.wrap(sponge, 128) # Convert the sponge into 128-bit blocks
for z in sponge: # Keep iterating the following code
z = z^j # XOR the sponge block with the message block
sponge = join(sponge) # Convert the blocks back into a string
sponge = textwrap.wrap(sponge, len(key)*8) # Convert the sponge into blocks with the same length of the key
output = sponge # Create a new variable to save space
del nonce, blocks # Delete variables to save space
while len(output) > 1: # Keep iterating the following code
output[1] = output[1]^output[0] >> output[0] # XOR the second element with the first, then shift forward
del output[0] # Delete the first element, so it can repeat again
tag = ((output^plaintext) <<< sponge) + output # Generate an authentication tag. That's not overkill, is it?
print output # Oh yeah, just print it in hexadecimal, I dunno how to
当他在终端运行脚本时,这是交互:
- 要散列的值:abcioagdsbvasizfuvbosuif
- 要使用的随机数:iugzaliuglieas
- 要使用的密钥:asljdgadskj
例外:
Traceback (most recent call last):
File "DarkKnight-Sponge.py", line 13, in <module>
sponge = (sponge << 128) + j # Concatenate the current sponge value and the block
TypeError: unsupported operand type(s) for <<: 'str' and 'int'
【问题讨论】:
-
sponge是一个字符串,您不能将<<与它一起使用 -
如果您能在书中写出描述该算法的段落,那将非常有帮助。我确信从
for j in blocks:到sponge = join(sponge)的六行代码中存在逻辑错误,但如果不知道这六行代码的用途,要修复它并不容易
标签: python