仍然不能完全让它发挥作用。我的问题是关于如何使解密线工作。这是我写的:
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:“计数器”参数必须是可调用对象
我只是不知道 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
编辑仍然无法让它工作。非常沮丧,完全没有想法。这是最新的代码:(请注意,我的输入字符串是 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 计数器函数返回的字符串长度不是 16