harmony 鸿蒙Converting Binary Data into a Symmetric Key Pair (C/C++)

  • 2025-06-12
  • 浏览 (7)

Converting Binary Data into a Symmetric Key Pair (C/C++)

This topic uses 3DES and HMAC as an example to describe how to convert binary data into a symmetric key (OH_CryptoSymKey). That is, convert a piece of external or internal binary data into a key object for subsequent operations, such as encryption and decryption.

Adding the Dynamic Library in the CMake Script

target_link_libraries(entry PUBLIC libohcrypto.so)

Converting Binary Data into a 3DES Key

For details about the algorithm specifications, see 3DES.

  1. Obtain the 3DES binary key data and encapsulate it into a Crypto_DataBlob object.

  2. Call OH_CryptoSymKeyGenerator_Create with the string parameter ‘3DES192’ to create a symmetric key generator (OH_CryptoSymKeyGenerator) object for a 192-bit 3DES key.

  3. Call OH_CryptoSymKeyGenerator_Convert to convert the binary data into a symmetric key object (OH_CryptoSymKey).

  4. Call OH_CryptoSymKey_GetKeyData to obtain the binary data of the key object.

Example: Convert binary data into a 3DES key.

  #include "CryptoArchitectureKit/crypto_common.h"
  #include "CryptoArchitectureKit/crypto_sym_key.h"

  static OH_Crypto_ErrCode doTestDataCovertSymKey() {
      const char *algName = "3DES192";
      OH_CryptoSymKeyGenerator *ctx = nullptr;
      OH_CryptoSymKey *convertKeyCtx = nullptr;
      Crypto_DataBlob out = {.data = nullptr, .len = 0};
      OH_Crypto_ErrCode ret;
      uint8_t arr[] = {0xba, 0x3d, 0xc2, 0x71, 0x21, 0x1e, 0x30, 0x56, 0xad, 0x47, 0xfc, 0x5a,
                      0x46, 0x39, 0xee, 0x7c, 0xba, 0x3b, 0xc2, 0x71, 0xab, 0xa0, 0x30, 0x72};
      Crypto_DataBlob convertBlob = {.data = arr, .len = sizeof(arr)};
      ret = OH_CryptoSymKeyGenerator_Create(algName, &ctx);
      if (ret != CRYPTO_SUCCESS) {
          return ret;
      }
      ret = OH_CryptoSymKeyGenerator_Convert(ctx, &convertBlob, &convertKeyCtx);
      if (ret != CRYPTO_SUCCESS) {
          OH_CryptoSymKeyGenerator_Destroy(ctx);
          return ret;
      }
      ret = OH_CryptoSymKey_GetKeyData(convertKeyCtx, &out);
      OH_CryptoSymKeyGenerator_Destroy(ctx);
      OH_CryptoSymKey_Destroy(convertKeyCtx);
      if (ret != CRYPTO_SUCCESS) {
          return ret;
      }
      OH_Crypto_FreeDataBlob(&out);
      return ret;
  }

Converting Binary Data into an HMAC Key

For details about the algorithm specifications, see HMAC.

  1. Obtain the HMAC binary key and encapsulate it into a Crypto_DataBlob object.

  2. Call OH_CryptoSymKeyGenerator_Create with the string parameter ‘HMAC’ to create a symmetric key generator (OH_CryptoSymKeyGenerator) object for an HMAC key of [1, 32768] bits.

  3. Call OH_CryptoSymKeyGenerator_Convert to convert the binary data into a symmetric key object (OH_CryptoSymKey).

  4. Call OH_CryptoSymKey_GetKeyData to obtain the binary data of the key object.

Example: Convert binary data into an HMAC key.

  #include "CryptoArchitectureKit/crypto_common.h"
  #include "CryptoArchitectureKit/crypto_sym_key.h"
  #include <string.h>

  static OH_Crypto_ErrCode testConvertHmacKey() {
      const char *algName = "HMAC";
      OH_CryptoSymKeyGenerator *ctx = nullptr;
      OH_CryptoSymKey *convertKeyCtx = nullptr;
      Crypto_DataBlob out = {.data = nullptr, .len = 0};
      OH_Crypto_ErrCode ret;

      char *arr = const_cast<char *>("12345678abcdefgh12345678abcdefgh12345678abcdefgh12345678abcdefgh");
      Crypto_DataBlob convertBlob = {.data = (uint8_t *)(arr), .len = strlen(arr)};
      ret = OH_CryptoSymKeyGenerator_Create(algName, &ctx);
      if (ret != CRYPTO_SUCCESS) {
          return ret;
      }
      ret = OH_CryptoSymKeyGenerator_Convert(ctx, &convertBlob, &convertKeyCtx);
      if (ret != CRYPTO_SUCCESS) {
          OH_CryptoSymKeyGenerator_Destroy(ctx);
          return ret;
      }
      ret = OH_CryptoSymKey_GetKeyData(convertKeyCtx, &out);
      OH_CryptoSymKeyGenerator_Destroy(ctx);
      OH_CryptoSymKey_Destroy(convertKeyCtx);
      if (ret != CRYPTO_SUCCESS) {
          return ret;
      }
      OH_Crypto_FreeDataBlob(&out);
      return ret;
  }

你可能感兴趣的鸿蒙文章

harmony 鸿蒙Crypto Architecture Kit

harmony 鸿蒙Encryption and Decryption with a 3DES Symmetric Key (ECB Mode) (C/C++)

harmony 鸿蒙Encryption and Decryption with a 3DES Symmetric Key (ECB Mode) (ArkTS)

harmony 鸿蒙Encryption and Decryption with an AES Symmetric Key (CBC Mode) (C/C++)

harmony 鸿蒙Encryption and Decryption with an AES Symmetric Key (CBC Mode) (ArkTS)

harmony 鸿蒙Encryption and Decryption with an AES Symmetric Key (CCM Mode) (C/C++)

harmony 鸿蒙Encryption and Decryption with an AES Symmetric Key (CCM Mode) (ArkTS)

harmony 鸿蒙Encryption and Decryption with an AES Symmetric Key (ECB Mode) (C/C++)

harmony 鸿蒙Encryption and Decryption with an AES Symmetric Key (ECB Mode) (ArkTS)

harmony 鸿蒙Encryption and Decryption by Segment with an AES Symmetric Key (GCM Mode) (C/C++)

0  赞