Encryption and Decryption with a DES Symmetric Key (ECB Mode) (C/C++)
For details about the algorithm specifications, see DES.
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 DES and the key length being 64 bits.
In addition to the example in this topic, DES and Converting Binary Data into a Symmetric Key may help you better understand how to generate a DES symmetric key pair. Note that the input parameters in the reference documents may be different from those in the example below.
Encryption
-
Call OH_CryptoSymCipher_Create with the string parameter 'DES64|ECB|PKCS7' to create a Cipher instance for encryption. The key type is DES64, block cipher mode is ECB, and the padding mode is PKCS7.
-
Call OH_CryptoSymCipher_Init to initialize the Cipher instance. Specifically, set mode to CRYPTO_ENCRYPT_MODE, and specify the key for encryption (OH_CryptoSymKey).
If ECB mode is used, set params to null.
-
Call OH_CryptoSymCipher_Update to update data (in plaintext).
- If a small amount of data is to be encrypted, you can use OH_CryptoSymCipher_Final() immediately after OH_CryptoSymCipher_Init().
- If a large amount of data is to be encrypted, you can call OH_CryptoSymCipher_Update multiple times to pass in the data by segment.
-
Call OH_CryptoSymCipher_Final to obtain the encrypted data.
- If OH_CryptoSymCipher_Update is used to pass in data, set data to null. If OH_CryptoSymCipher_Final is used to pass in data, pass in the plaintext via data.
- The output of OH_CryptoSymCipher_Final may be null. To avoid exceptions, always check whether the result is null before accessing specific data.
Decryption
-
Call OH_CryptoSymCipher_Create with the string parameter 'DES64|ECB|PKCS7' to create a Cipher instance for decryption. The key type is DES64, block cipher mode is ECB, and the padding mode is PKCS7.
-
Call OH_CryptoSymCipher_Init to initialize the Cipher instance. Specifically, set mode to CRYPTO_DECRYPT_MODE, and specify the key for decryption (OH_CryptoSymKey). If ECB mode is used, pass in null.
-
Call OH_CryptoSymCipher_Update to update data (in ciphertext).
- If a small amount of data is to be encrypted, you can use OH_CryptoSymCipher_Final() immediately after OH_CryptoSymCipher_Init().
- If a large amount of data is to be encrypted, you can call OH_CryptoSymCipher_Update multiple times to pass in the data by segment.
- You can determine the method to use based on the data size. For example, if the message is greater than 20 bytes, use Cipher.update.
-
Call OH_CryptoSymCipher_Final to obtain the decrypted data.
- If OH_CryptoSymCipher_Update is used to pass in data, set data to null. If OH_CryptoSymCipher_Final is used to pass in data, pass in the ciphertext via data.
- The output of OH_CryptoSymCipher_Final may be null. To avoid exceptions, always check whether the result is null before accessing specific data.
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 <cstring>
#include "file.h"
OH_Crypto_ErrCode doTestDesEcb()
{
OH_CryptoSymKeyGenerator *genCtx = nullptr;
OH_CryptoSymCipher *encCtx = nullptr;
OH_CryptoSymCipher *decCtx = nullptr;
OH_CryptoSymKey *keyCtx = nullptr;
char *plainText = const_cast<char *>("this is test!");
Crypto_DataBlob input = {.data = (uint8_t *)(plainText), .len = strlen(plainText)};
Crypto_DataBlob encData = {.data = nullptr, .len = 0};
Crypto_DataBlob decData = {.data = nullptr, .len = 0};
// Generate a symmetric key randomly.
OH_Crypto_ErrCode ret = OH_CryptoSymKeyGenerator_Create("DES64", &genCtx);
if (ret != CRYPTO_SUCCESS) {
goto end;
}
ret = OH_CryptoSymKeyGenerator_Generate(genCtx, &keyCtx);
if (ret != CRYPTO_SUCCESS) {
goto end;
}
// Encrypt the message.
ret = OH_CryptoSymCipher_Create("DES64|ECB|PKCS7", &encCtx);
if (ret != CRYPTO_SUCCESS) {
goto end;
}
ret = OH_CryptoSymCipher_Init(encCtx, CRYPTO_ENCRYPT_MODE, keyCtx, nullptr);
if (ret != CRYPTO_SUCCESS) {
goto end;
}
ret = OH_CryptoSymCipher_Final(encCtx, &input, &encData);
if (ret != CRYPTO_SUCCESS) {
goto end;
}
// Decrypt the message.
ret = OH_CryptoSymCipher_Create("DES64|ECB|PKCS7", &decCtx);
if (ret != CRYPTO_SUCCESS) {
goto end;
}
ret = OH_CryptoSymCipher_Init(decCtx, CRYPTO_DECRYPT_MODE, keyCtx, nullptr);
if (ret != CRYPTO_SUCCESS) {
goto end;
}
ret = OH_CryptoSymCipher_Final(decCtx, &encData, &decData);
if (ret != CRYPTO_SUCCESS) {
goto end;
}
end:
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