类型错误：&lt;&lt;：&#39;str&#39; 和 &#39;int&#39; 的操作数类型不受支持

[英]TypeError: unsupported operand type(s) for <<: 'str' and 'int'

``````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'
``````

了解错误信息

``````TypeError: unsupported operand type(s) for <<: 'str' and 'int'
``````

了解发生错误的线路

``````sponge = (sponge << 128) + j                  # Concatenate the current sponge value and the block
``````

`sponge`向左移动 128 位，然后将小于 128 位的数字添加到结果中。 实际上，这是将`sponge`位与`j`位连接起来。 回到我们的十进制类比：如果`x`是一个数字，而`y`是一个小于 100 的数字，那么数字`x * 100 + y`是通过连接`x``y`的数字获得的数字。 例如， `1374 * 100 + 56 = 137456`

将文本转换为 int

``````plaintext_string = raw_input("The value to be hashed: ") # Get the user to input the data to be hashed
nonce_string = raw_input("The nonce to be used: ")       # Get the user to input the nonce to be used
key_string = raw_input("The key to be used: ")           # Get the user to input the key to be used

def string_to_int(txt):
number = 0
for c in txt:
number = (number << 8) + ord(c)
return number

plaintext = string_to_int(plaintext_string)
nonce = string_to_int(plaintext_string)
key = string_to_int(key_string)
``````

``````def string_to_intblocks(txt, blocksize):
blocks = []
block_number = 0
for i,c in enumerate(txt):
block_number = (block_number << 8) + ord(c)
if i % blocksize == 0:
blocks.append(block_number)
block_number = 0
return blocks
``````

``````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)
``````

© 2020-2024 STACKOOM.COM