首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Pycrypto:递增CTR模式

Pycrypto:递增CTR模式
EN

Stack Overflow用户
提问于 2012-07-25 18:15:38
回答 1查看 15.9K关注 0票数 4

还是不能让这件事起作用。我的问题是如何使解密行工作。以下是我所写的:

代码语言:javascript
复制
class IVCounter(object):
    @staticmethod
    def incrIV(self):
        temp = hex(int(self, 16)+1)[2:34]
        return array.array('B', temp.decode("hex")).tostring()


def decryptCTR(key, ciphertext):

    iv = ciphertext[:32] #extracts the first 32 characters of the ciphertext

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

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

我的错误消息是:

ValueError:“计数器”参数必须是可调用的对象

我就是搞不懂墓穴是怎么组织第三个论点的。

有人能帮忙吗?谢谢!

在实现以下建议之后编辑新代码。还是卡住了!

代码语言:javascript
复制
class IVCounter(object):
    def __init__(self, start=1L):
        print start #outputs the number 1 (not my IV as hoped)
        self.value = long(start)

   def __call__(self):
        print self.value  #outputs 1 - need this to be my iv in long int form
        print self.value + 1L  #outputs 2
        self.value += 1L
        return somehow_convert_this_to_a_bitstring(self.value) #to be written

def decryptCTR(key, ciphertext):

    iv = ciphertext[:32] #extracts the first 32 characters of the ciphertext
    iv = int(iv, 16)

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

    ctr = IVCounter()
    Crypto.Util.Counter.new(128, initial_value = iv)

    print AES.new(key, AES.MODE_CTR, counter=ctr).decrypt(ciphertext)
    return

编辑仍然无法使其工作。非常沮丧和完全没有想法。下面是最新的代码:(请注意,我的输入字符串是32位十六进制字符串,必须以两位数字对解释才能转换为长整数。)

代码语言:javascript
复制
class IVCounter(object):
    def __init__(self, start=1L):
        self.value = long(start)

    def __call__(self):
        self.value += 1L
        return hex(self.value)[2:34]

def decryptCTR(key, ciphertext):
    iv = ciphertext[:32] #extracts the first 32 characters of the ciphertext
    iv = array.array('B', iv.decode("hex")).tostring()

    ciphertext = ciphertext[32:]

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

    #ctr = IVCounter(long(iv))
    ctr = Crypto.Util.Counter.new(16, iv)

    print AES.new(key, AES.MODE_CTR, counter=ctr).decrypt(ciphertext)
    return

TypeError: CTR计数器函数返回长度为16的字符串

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-07-25 22:04:03

在Python中,将函数视为对象是完全有效的。将任何将__call__(self, ...)定义为函数的对象处理也是完全有效的。

所以你想要的可能是这样的:

代码语言:javascript
复制
class IVCounter(object):
    def __init__(self, start=1L):
        self.value = long(start)
    def __call__(self):
        self.value += 1L
        return somehow_convert_this_to_a_bitstring(self.value)

ctr = IVCounter()
... make some keys and ciphertext ...
print AES.new(key, AES.MODE_CTR, counter=ctr).decrypt(ciphertext)

但是,PyCrypto为您提供了一个比纯Python快得多的计数器方法:

代码语言:javascript
复制
import Crypto.Util.Counter
ctr = Crypto.Util.Counter.new(NUM_COUNTER_BITS)

ctr现在是一个有状态的函数(同时也是一个可调用的对象),它在每次调用它时都会增加并返回它的内部状态。然后你就可以

代码语言:javascript
复制
print AES.new(key, AES.MODE_CTR, counter=ctr).decrypt(ciphertext)

就像以前一样。

下面是一个用用户指定的初始化向量在CTR模式下使用Crypto.Cipher.AES的工作示例:

代码语言:javascript
复制
import Crypto.Cipher.AES
import Crypto.Util.Counter

key = "0123456789ABCDEF" # replace this with a sensible value, preferably the output of a hash
iv = "0000000000009001" # replace this with a RANDOMLY GENERATED VALUE, and send this with the ciphertext!

plaintext = "Attack at dawn" # replace with your actual plaintext

ctr = Crypto.Util.Counter.new(128, initial_value=long(iv.encode("hex"), 16))

cipher = Crypto.Cipher.AES.new(key, Crypto.Cipher.AES.MODE_CTR, counter=ctr)
print cipher.encrypt(plaintext)
票数 15
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11656045

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档