gpt4 book ai didi

python - Pycrypto:递增点击率模式

转载 作者:太空狗 更新时间:2023-10-30 01:45:02 25 4
gpt4 key购买 nike

仍然不能让它正常工作。我的问题是关于如何使解密线工作。这是我写的:

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: 'counter' parameter must be a callable object

我只是想不通 pycrypto 要我如何组织 new 的第三个参数。

有人可以帮忙吗?谢谢!

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

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

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

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 counter function returned string not of length 16

最佳答案

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

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

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 快得多:

import Crypto.Util.Counter
ctr = Crypto.Util.Counter.new(NUM_COUNTER_BITS)

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

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

和以前一样。

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

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)

关于python - Pycrypto:递增点击率模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11656045/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com