AES-CTR 算法验证(基于mbedtls)

8 篇文章 6 订阅 ¥19.90 ¥99.00

测试key

Test Vectors
根据上面的协议文档描述,可以看到mbedtls sdk测试程序的数据来源;

可以看看第六章节
6.  Test Vectors

   This section contains nine test vectors, which can be used to confirm
   that an implementation has correctly implemented AES-CTR.  The first
   three test vectors use AES with a 128 bit key; the next three test
   vectors use AES with a 192 bit key; and the last three test vectors
   use AES with a 256 bit key.

   Test Vector #1<
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
AES-128-CTR是一种对称加密算法,它使用128位密钥和计数器模式(CTR)进行加密。CTR模式将计数器作为输入,然后使用加密函数生成密钥流,该密钥流与明文异或以生成密文。由于CTR模式中的加密和解密使用相同的操作,因此CTR模式可以用于加密和解密。 以下是使用Python中的pycryptodome库实现AES-128-CTR加密和解密的示例代码: ```python from Crypto.Cipher import AES from Crypto.Util import Counter import binascii # 加密函数 def aes_ctr_encrypt(key, iv, data): # 将iv转换为计数器对象 ctr = Counter.new(128, initial_value=int(binascii.hexlify(iv),16)) # 创建AES加密器 aes = AES.new(key, AES.MODE_CTR, counter=ctr) # 加密数据 ciphertext = aes.encrypt(data) # 返回密文 return ciphertext # 解密函数 def aes_ctr_decrypt(key, iv, ciphertext): # 将iv转换为计数器对象 ctr = Counter.new(128, initial_value=int(binascii.hexlify(iv), 16)) # 创建AES解密器 aes = AES.new(key, AES.MODE_CTR, counter=ctr) # 解密数据 data = aes.decrypt(ciphertext) # 返回明文 return data # 测试 key = b'2B7E151628AED2A6ABF7158809CF4F3C' iv = b'F0F1F2F3F4F5F6F7F8F9FAFBFCFDFEFF' data = b'6BC1BEE22E409F96E93D7E117393172AAE2D8A571E03AC9C9EB76FAC45AF8E5' ciphertext = aes_ctr_encrypt(key, iv, data) print('加密结果:', binascii.hexlify(ciphertext)) plaintext = aes_ctr_decrypt(key, iv, ciphertext) print('解密结果:', plaintext) # 输出: # 加密结果: b'874d6191b620e3261bef6864990db6ce9806f66b7970fdff8617187bb9fffdff5ae4df3edbd5d35e5b4f09020db03eab1e031dda2fbe03d1792170a0f3009cee' # 解密结果: b'6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5' ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

打个工而已

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值