c - MBED TLS 对称 key 包装

标签 c arm encryption-symmetric mbedtls

我正在尝试使用 MBED TLS 加密函数来解开已使用我拥有的对称 key 使用 AES-128 key 包装进行加密的 key 。

我是加密新手,我的理解是 key 包装/解开与加密/解密不同。这是正确的吗?

this page 上列出了示例但 aes 示例正在执行解密而不是 key 解包,并且包装示例使用公钥而不是对称 key 。

是否有一些使用 MBED TLS 使用 AES-128 加密进行 key 解包的引用或示例?

我尝试简单地使用解密函数,但没有得到正确的数据。见下文。

  //Initialise AES context
  mbedtls_aes_init( &aes_ctx );
  //Set-up the context
  mbedtls_aes_setkey_dec( &aes_ctx, AES_key, 128 );
  //Process the encrypted data in blocks of 16 bytes
  for(i = 0; i< encryptedDataLength; i+= 16)
  {
    mbedtls_aes_crypt_ecb( &aes_ctx, MBEDTLS_AES_DECRYPT, pEncryptedData + i, pPlainValue + i); 
  }
  //Free the context
  mbedtls_aes_free( &aes_ctx );

谢谢!

最佳答案

我找到了一个解决方案,使用nist_kw接口(interface)

  //Initialise key-wrap context
  mbedtls_nist_kw_init(&kw_ctx);
  //Set up the context
  mbedtls_nist_kw_setkey(&kw_ctx, MBEDTLS_CIPHER_ID_AES, AES_key, 128, 0);

  //Process the encrypted data
  mbedtls_nist_kw_unwrap(&kw_ctx, MBEDTLS_KW_MODE_KW, 
                         pEncryptedData, encrypted_length,
                         pPlainValue, &decrypted_length,
                         encryptedDataLength);

  //Free the context
  mbedtls_nist_kw_free(&kw_ctx);

这为我正确解密了数据。感谢您成为我的橡皮鸭!

关于c - MBED TLS 对称 key 包装,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59063383/

相关文章:

assembly - 使用arm neon进行RGB到灰度转换

javascript - 简单的异或消息(Javascript/Tcl)?

c - C 书的练习

c - C 中分割数字的总和

linux - 预链接错误 : Layout error: section size too small for data

cryptography - 初始化向量 - 最佳实践(对称加密)

java - java中AES256加密的问题

C UNIX 多进程间信号量同步

c - 将 YUV 转换为 RGBA 的最快近似方法?

assembly - assembly 中的搬迁