Encryption and Decryption of Symmetric Keys with AES-WRAP (C/C++)
From API version 22, the crypto algorithm library supports encryption and decryption with AES-WRAP.
For details, see AES-WRAP encryption and decryption algorithm specifications.
Adding the Dynamic Library in the CMake Script
target_link_libraries(entry PUBLIC libohcrypto.so)
How to Develop
Creating an Object
Call OH_CryptoSymKeyGenerator_Create and OH_CryptoSymKeyGenerator_Generate to generate a symmetric key (OH_CryptoSymKey) with the key algorithm being AES and the key length being 128 bits.
In addition to the example in this topic, AES and Randomly Generating a Symmetric Key may help you better understand how to generate an AES symmetric key. Note that the input parameters in the reference documents may be different from those in the example below.
Encryption
-
Call OH_CryptoSymCipher_Create and specify the string parameter AES128-WRAP to create a Cipher instance whose symmetric key type is AES128-WRAP for encryption.
-
Call OH_CryptoSymCipherParams_Create to create a parameter object and call OH_CryptoSymCipherParams_SetParam to set encryption parameters.
-
Call OH_CryptoSymCipher_Init to initialize the Cipher instance. Specifically, set mode to CRYPTO_ENCRYPT_MODE, and specify the key for encryption (OH_CryptoSymKey) and the encryption parameter instance (OH_CryptoSymCipherParams).
-
When the content to be encrypted is short, call OH_CryptoSymCipher_Final to obtain the encrypted data, without calling OH_CryptoSymCipher_Update.
Decryption
-
Call OH_CryptoSymCipher_Create and specify the string parameter AES128-WRAP to create a Cipher instance whose symmetric key type is AES128-WRAP for decryption.
-
Call OH_CryptoSymCipher_Init to initialize the Cipher instance. Specifically, set mode to CRYPTO_DECRYPT_MODE, and specify the decryption key (OH_CryptoSymKey) and the decryption parameter instance (OH_CryptoSymCipherParams).
-
When the content to be decrypted is short, call OH_CryptoSymCipher_Final to obtain the decrypted data, without calling OH_CryptoSymCipher_Update.
Destroying Objects
Call OH_CryptoSymKeyGenerator_Destroy, OH_CryptoSymCipher_Destroy, OH_CryptoSymKey_Destroy, and OH_Crypto_FreeDataBlob to release the allocated memory and destroy the object.
#include "CryptoArchitectureKit/crypto_common.h"
#include "CryptoArchitectureKit/crypto_sym_cipher.h"
#include <string.h>
static OH_Crypto_ErrCode doTestAesWrap()
{
OH_CryptoSymKeyGenerator *genCtx = nullptr;
OH_CryptoSymCipher *encCtx = nullptr;
OH_CryptoSymCipher *decCtx = nullptr;
OH_CryptoSymKey *keyCtx = nullptr;
OH_CryptoSymCipherParams *params = nullptr;
Crypto_DataBlob encData = {.data = nullptr, .len = 0};
Crypto_DataBlob decData = {.data = nullptr, .len = 0};
char *plainText = const_cast<char *>("this is test!");
Crypto_DataBlob msgBlob = {.data = (uint8_t *)(plainText), .len = strlen(plainText)};
uint8_t iv[8] = {1, 2, 4, 12, 3, 4, 2, 3}; // iv is generated from an array of secure random numbers.
Crypto_DataBlob ivBlob = {.data = iv, .len = sizeof(iv)};
// Generate a symmetric key.
OH_Crypto_ErrCode ret;
ret = OH_CryptoSymKeyGenerator_Create("AES128", &genCtx);
if (ret != CRYPTO_SUCCESS) {
goto end;
}
ret = OH_CryptoSymKeyGenerator_Generate(genCtx, &keyCtx);
if (ret != CRYPTO_SUCCESS) {
goto end;
}
// Create a cipher parameter object.
ret = OH_CryptoSymCipherParams_Create(¶ms);
if (ret != CRYPTO_SUCCESS) {
goto end;
}
// Set parameters.
ret = OH_CryptoSymCipherParams_SetParam(params, CRYPTO_IV_DATABLOB, &ivBlob); // Only iv needs to be set for AES-WRAP.
if (ret != CRYPTO_SUCCESS) {
goto end;
}
// Encrypt data.
ret = OH_CryptoSymCipher_Create("AES128-WRAP", &encCtx);
if (ret != CRYPTO_SUCCESS) {
goto end;
}
ret = OH_CryptoSymCipher_Init(encCtx, CRYPTO_ENCRYPT_MODE, keyCtx, params);
if (ret != CRYPTO_SUCCESS) {
goto end;
}
ret = OH_CryptoSymCipher_Final(encCtx, &msgBlob, &encData);
if (ret != CRYPTO_SUCCESS) {
goto end;
}
// Decrypt data.
ret = OH_CryptoSymCipher_Create("AES128-WRAP", &decCtx);
if (ret != CRYPTO_SUCCESS) {
goto end;
}
ret = OH_CryptoSymCipher_Init(decCtx, CRYPTO_DECRYPT_MODE, keyCtx, params); // The params value must be the same as that used in encryption.
if (ret != CRYPTO_SUCCESS) {
goto end;
}
ret = OH_CryptoSymCipher_Final(decCtx, &encData, &decData);
if (ret != CRYPTO_SUCCESS) {
goto end;
}
end:
OH_CryptoSymCipherParams_Destroy(params);
OH_CryptoSymCipher_Destroy(encCtx);
OH_CryptoSymCipher_Destroy(decCtx);
OH_CryptoSymKeyGenerator_Destroy(genCtx);
OH_CryptoSymKey_Destroy(keyCtx);
OH_Crypto_FreeDataBlob(&encData);
OH_Crypto_FreeDataBlob(&decData);
return ret;
}
你可能感兴趣的鸿蒙文章
openharmony 鸿蒙 crypto-rsa-asym-encrypt-decrypt-pkcs1-ndk
openharmony 鸿蒙 crypto-key-agreement-using-ecdh
openharmony 鸿蒙 crypto-aes-sym-encrypt-decrypt-gcm-ndk
openharmony 鸿蒙 crypto-sm4-sym-encrypt-decrypt-gcm-ndk
openharmony 鸿蒙 crypto-sm2-sign-sig-verify-pkcs1-ndk
openharmony 鸿蒙 crypto-key-derivation-using-scrypt-ndk
openharmony 鸿蒙 crypto-sym-encrypt-decrypt-spec
openharmony 鸿蒙 crypto-rsa-sign-sig-verify-pkcs1-by-segment