我正在创建一个像crytography代码一样的AES,我有我需要帮助的错误。AttributeError:加密实例没有属性'__getitem__'
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])
这可能意味着你的'array_8'不是可转位的,也就是说你不能写'array_8 [i]' – 0sh 2014-11-23 11:11:08
另外:调用一个模块'try'是一个坏主意,因为这是一个Python关键字,所以你不能使用它导入它像'import try'这样的东西。 – DSM 2014-11-23 11:11:34
你的帖子没有足够的数据来回答,请更新方法self.sunBytes并检查(或简单添加打印)它返回的结果,答案是 – Rustem 2014-11-23 11:25:57