我正在尝试使用 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/