2014-11-23 131 views
0

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

這可能意味着你的'array_8'不是可轉位的,也就是說你不能寫'array_8 [i]' – 0sh 2014-11-23 11:11:08

+0

另外:調用一個模塊'try'是一個壞主意,因爲這是一個Python關鍵字,所以你不能使用它導入它像'import try'這樣的東西。 – DSM 2014-11-23 11:11:34

+0

你的帖子沒有足夠的數據來回答,請更新方法self.sunBytes並檢查(或簡單添加打印)它返回的結果,答案是 – Rustem 2014-11-23 11:25:57

回答

0

功能byte_construct()是不是類加密的實例方法,但是你調用它喜歡它是

permutation_result = self.byte_construct(substitution_result) 

這意味着,現在byte_construct將使用self作爲它的第一個參數(在這種情況下爲array-8)和substitution_result作爲其第二個參數(bit_pos)。由於self是一個加密對象,我猜它並不是要編入索引的(即你沒有爲它定義self.__getitem__()),所以你有上面的錯誤。

+0

那麼您可以在這裏提供什麼解決方案? – 2014-11-24 14:17:11

+0

解決方法是,您必須仔細查看'byte_construct()'的聲明並相應地調整您的調用。目前,您正在像調用Encryption類的實例方法一樣調用它,但它沒有被定義爲這樣(即它沒有在Encryption類中定義,也沒有將self用作其第一個參數)。問問你自己:'array_8'應該是什麼類型?如果它應該是一個加密對象,那麼使'byte_construct'成爲一個實例方法。如果不應該,那麼在調用它時刪除'self.'前綴,併爲它提供應該是'array_8'的對象。 – oxymor0n 2014-11-24 19:40:43

相關問題