pythonAES算法CTR模式加解密

python实现AES算法CTR模式加解密,计数器和明文和密钥均为用户输入

不知道你这个问题是否已经解决, 如果还没有解决的话:
  • 这篇博客: Python_头条推荐系统_离线用户召回集与排序计算(2)中的 3.6.1 离线排序模型-CTR预估 部分也许能够解决你的问题, 你可以仔细阅读以下内容或者直接跳转源博客中阅读:
    • CTR(Click-Through Rate)预估:给定一个Item,预测该Item会被点击的概率

      • 离线的模型训练:排序的各种模型训练评估
      • 特征服务平台:为了提高模型在排序时候的特征读取处理速率,直接将处理好的特征写入HBASE

  • 以下回答来自chatgpt:

    使用Python中的pycryptodome库可以实现AES算法的CTR模式加解密。具体步骤如下:

    1. 导入库
    from Crypto.Cipher import AES
    
    1. 定义密钥、计数器、明文
    key = b'this is a 16 key'
    iv = b'counter for aes-ctr'
    plaintext = b'this is some plaintext'
    
    1. 创建AES加密对象
    aes = AES.new(key, AES.MODE_CTR, nonce=iv) # 使用CTR模式,设置计数器为iv
    
    1. 加密明文
    ciphertext = aes.encrypt(plaintext)
    
    1. 解密密文
    aes = AES.new(key, AES.MODE_CTR, nonce=iv) # 必须重新创建新的加密对象
    decrypted_plaintext = aes.decrypt(ciphertext)
    

    完整代码如下:

    from Crypto.Cipher import AES
    
    key = b'this is a 16 key'
    iv = b'counter for aes-ctr'
    plaintext = b'this is some plaintext'
    
    aes = AES.new(key, AES.MODE_CTR, nonce=iv)
    ciphertext = aes.encrypt(plaintext)
    
    aes = AES.new(key, AES.MODE_CTR, nonce=iv)
    decrypted_plaintext = aes.decrypt(ciphertext)
    
    print(decrypted_plaintext) # b'this is some plaintext'
    

    需要注意的是,在使用CTR模式时,加解密时必须使用相同的计数器值。因此,在加密时使用的加密对象不能被重用,必须重新创建一个新的加密对象。


如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^