我想创建一个像crytography代码的AES,我遇到了需要帮助的错误。
File "C:\Users\work\Desktop\try.py", line 156, in byte_construct
if (pos & array_8[i]):
AttributeError: Encryption instance has no attribute '__getitem__'
我不断收到上述错误。有人可以给我一个解决方案。以下是我的来源
def rotate_byte_left(byte):
value = 0x00
value = byte & 0x80
byte = byte << 1
byte = byte & 0xFF
if value == 0x80:
byte = byte | 0x01
return byte
def rotate_byte_right(byte):
value = 0x00
value = byte & 0x01
byte = byte >> 1
byte = byte & 0xFF
if value == 0x01:
byte = byte | 0x80
return byte
def byte_construct(array_8,bit_pos):
byte = 0x00
for p in bit_pos:
pos = (0x01 << p)
for i in range(0,8): Specifically the error is poiting here.
if (pos & array_8[i]):
byte = byte & (0x01 << bit_pos)
return byte
def addRoundKey(self, state, roundKey):
"""Adds (XORs) the round key to the state."""
for i in range(0, len(state)):
state[i] ^= roundKey[i]
return state
def ecb(self, plaintext, key):
start = time.time()
pre_round1 = self.convert_hex_to_list(plaintext ^ key)
substitution_result = self.subBytes(pre_round1, False)
permutaion_result = self.byte_construct(substitution_result)
if __name__ == "__main__":
encrypt = Encryption()
encrypt.ecb(0x0000000000000000, 0x00FF00FF00FF00FF)
print encrypt.convert_list_to_hex([255,0,255])
答案 0 :(得分:0)
函数byte_construct()
不是加密类的实例方法,但是你正在调用它
permutation_result = self.byte_construct(substitution_result)
这意味着现在byte_construct
将self
作为其第一个参数(在本例中为array-8
),substitution_result
作为其第二个参数({{1} }})。由于bit_pos
是一个加密对象,我猜测它不是要编入索引(即你没有为它定义self
),你已经得到了上述内容错误。