2014-11-23 59 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

相关问题