我正在創建一個像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