【问题标题】:Pycrypto: Incrementing AES Counter ModePycrypto:递增 AES 计数器模式
【发布时间】:2012-07-25 03:38:30
【问题描述】:

我真的很困惑如何使用计数器模式在 pycrypto 中进行 AES 解密。据我了解这个过程,如果我从一个已知的 IV 开始解密第一个块,那么对于每个连续的块,我必须增加 IV。但我不知道如何做到这一点。

另外,我对 python 完全陌生,你会很容易看到。我的问题在于我如何实现我的课程以及我如何从解密器中调用它。

这是我迄今为止编写的代码:

class IVCounter(object):
    def incrIV(self):
        return self[:15] + chr(ord(self[15:]) + 1)

def decryptCTR(key, ciphertext):

    #convert the key into a 16 byte string
    key = array.array('B', key.decode("hex")).tostring()

    #convert the iv into a 16 byte string
    iv = array.array('B', iv.decode("hex")).tostring()

    print AES.new(key, mode, counter=IVCounter.incrIV(iv)).decrypt(ciphertext)
    return

这是我得到的错误:

TypeError: unbound 方法 incrIV() 必须以 IVCounter 实例作为第一个参数调用(取而代之的是 str 实例)

无论我尝试什么,我都无法让它工作。有人可以帮我理顺吗?

谢谢!

【问题讨论】:

    标签: python aes pycrypto


    【解决方案1】:
    class IVCounter(object):
        @staticmethod
        def incrIV(arry):
            return arry[:15] + chr(ord(arry[15:]) + 1)
    

    它在抱怨,因为它希望第一个参数是一个实例。使用 staticmethod 装饰器将其关闭。

    【讨论】:

    • 谢谢。这让我通过了错误消息,但解密仍然无法正常工作。我将作为一个新问题重新发布。感谢您的帮助!
    • 第 256 个区块会发生什么?
    • @AnuragUniyal,从技术上讲,它可能发生在第 1 块和第 4095 块之间的任何地方,但我认为它会引发异常。无论如何,这就是 chr(256) 为我所做的。
    猜你喜欢
    • 2017-09-17
    • 2012-07-24
    • 2021-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多