我在这里找到了由here提供的以下代码:atomicinf's code!代码是:
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)我的问题是:解密是如何工作的?(显然,我必须手动导入计数器或将当前计数器保存在某个地方)。第二,DES呢?我知道它有更小的计数器,但是我怎么定义它呢?
发布于 2012-12-13 05:28:52
CTR模式的解密与加密的工作方式相同,也就是说,要解密,您应该第二次调用'encrypt‘。这是因为在CTR模式中,对于每个下一块,IV都会递增,并使用AES算法进行加密,并将结果与明文进行异或运算。再次对其进行异或运算将返回cleartext。
https://stackoverflow.com/questions/13528444
复制相似问题