1e41f4b71Sopenharmony_ci# Converting a Compressed or Uncompressed ECC Public Key (C/C++) 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ciYou can generate a public key object [PubKey](../../reference/apis-crypto-architecture-kit/_crypto_asym_key_api.md#oh_cryptopubkey)) from ECC public key data or obtain the ECC public key data from a public key (**PubKey**) object. 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ciCurrently, only the compressed/uncompressed public key data that complies with the ECC X.509 standard is supported. The public key data mentioned in this topic is a complete X509 public key. For details about the operations on point data, see [Converting Compressed or Uncompressed ECC Point Data](crypto-convert-compressed-or-uncompressed-ECC-point.md). 6e41f4b71Sopenharmony_ci 7e41f4b71Sopenharmony_ciFor details about the ECC algorithm specifications, see [ECC](crypto-asym-key-generation-conversion-spec.md#ecc). 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ciYou can pass in the string parameter to set the format of the ECC public key to obtain. To obtain a compressed public key that complies with the X.509 standard, pass in **X509|COMPRESSED**. To obtain an uncompressed public key, pass in **X509|UNCOMPRESSED**. 10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_ci 12e41f4b71Sopenharmony_ci## Converting Uncompressed Public Key Data to Compressed Public Key Data 13e41f4b71Sopenharmony_ci 14e41f4b71Sopenharmony_ci1. Specify uncompressed ECC public key data of the uint8_t type and encapsulate the data into a [Crypto_DataBlob](../../reference/apis-crypto-architecture-kit/_crypto_common_api.md#crypto_datablob) object. 15e41f4b71Sopenharmony_ciEither the public key or private key can be passed in. In this example, an uncompressed public key is passed in. 16e41f4b71Sopenharmony_ci2. Use [OH_CryptoAsymKeyGenerator_Create](../../reference/apis-crypto-architecture-kit/_crypto_asym_key_api.md#oh_cryptoasymkeygenerator_generate) with the string parameter **'ECC_BrainPoolP256r1'** to create an asymmetric key generator (**OH_CryptoAsymKeyGenerator**) object for a 256-bit ECC key pair. 17e41f4b71Sopenharmony_ci3. Use [OH_CryptoAsymKeyGenerator_Convert](../../reference/apis-crypto-architecture-kit/_crypto_asym_key_api.md#oh_cryptoasymkeygenerator_convert) to convert the [Crypto_DataBlob](../../reference/apis-crypto-architecture-kit/_crypto_common_api.md#crypto_datablob) object into an asymmetric key object (**OH_CryptoKeyPair**). 18e41f4b71Sopenharmony_ci4. Use [OH_CryptoPubKey_Encode](../../reference/apis-crypto-architecture-kit/_crypto_asym_key_api.md#oh_cryptopubkey_encode) with the parameter set to **'X509|COMPRESSED'** to obtain the byte stream of the compressed public key data. 19e41f4b71Sopenharmony_ci 20e41f4b71Sopenharmony_ci**Example** 21e41f4b71Sopenharmony_ci 22e41f4b71Sopenharmony_ci```c++ 23e41f4b71Sopenharmony_ci#include "CryptoArchitectureKit/crypto_common.h" 24e41f4b71Sopenharmony_ci#include "CryptoArchitectureKit/crypto_asym_key.h" 25e41f4b71Sopenharmony_ci 26e41f4b71Sopenharmony_cistatic OH_Crypto_ErrCode doTestEccDataCovert() 27e41f4b71Sopenharmony_ci{ 28e41f4b71Sopenharmony_ci OH_CryptoAsymKeyGenerator *generator = nullptr; 29e41f4b71Sopenharmony_ci OH_CryptoKeyPair *keyPair = nullptr; 30e41f4b71Sopenharmony_ci Crypto_DataBlob returnBlob = { .data = nullptr, .len = 0 }; 31e41f4b71Sopenharmony_ci OH_Crypto_ErrCode ret = CRYPTO_INVALID_PARAMS; 32e41f4b71Sopenharmony_ci 33e41f4b71Sopenharmony_ci ret = HcfAsyKeyGeneratorCreate("ECC_BrainPoolP256r1", &generator); 34e41f4b71Sopenharmony_ci if (ret != CRYPTO_SUCCESS) { 35e41f4b71Sopenharmony_ci return ret; 36e41f4b71Sopenharmony_ci } 37e41f4b71Sopenharmony_ci uint8_t pubKeyBlobData[] = { 38e41f4b71Sopenharmony_ci 48, 90, 48, 20, 6, 7, 42, 134, 72, 206, 61, 2, 1, 6, 9, 43, 36, 3, 3, 2, 39e41f4b71Sopenharmony_ci 8, 1, 1, 7, 3, 66, 0, 4, 143, 39, 57, 249, 145, 50, 63, 222, 35, 70, 178, 40e41f4b71Sopenharmony_ci 121, 202, 154, 21, 146, 129, 75, 76, 63, 8, 195, 157, 111, 40, 217, 215, 41e41f4b71Sopenharmony_ci 148, 120, 224, 205, 82, 83, 92, 185, 21, 211, 184, 5, 19, 114, 33, 86, 85, 42e41f4b71Sopenharmony_ci 228,123, 242, 206, 200, 98, 178, 184, 130, 35, 232, 45, 5, 202, 189, 11, 43e41f4b71Sopenharmony_ci 46, 163, 156, 152 44e41f4b71Sopenharmony_ci }; 45e41f4b71Sopenharmony_ci Crypto_DataBlob pubKeyUncompressedBlob = { 46e41f4b71Sopenharmony_ci .data = pubKeyBlobData, 47e41f4b71Sopenharmony_ci .len = sizeof(pubKeyBlobData), 48e41f4b71Sopenharmony_ci }; 49e41f4b71Sopenharmony_ci ret = OH_CryptoAsymKeyGenerator_Convert(generator, CRYPTO_DER, &pubKeyUncompressedBlob, nullptr, &keyPair); 50e41f4b71Sopenharmony_ci if (ret != CRYPTO_SUCCESS) { 51e41f4b71Sopenharmony_ci OH_CryptoAsymKeyGenerator_Destroy(generator); 52e41f4b71Sopenharmony_ci return ret; 53e41f4b71Sopenharmony_ci } 54e41f4b71Sopenharmony_ci 55e41f4b71Sopenharmony_ci OH_CryptoPubKey *pubKey = OH_CryptoKeyPair_GetPubKey(keyPair); 56e41f4b71Sopenharmony_ci ret = OH_CryptoPubKey_Encode(pubKey, CRYPTO_DER, "X509|UNCOMPRESSED", &returnBlob); 57e41f4b71Sopenharmony_ci if (ret != CRYPTO_SUCCESS) { 58e41f4b71Sopenharmony_ci OH_CryptoAsymKeyGenerator_Destroy(generator); 59e41f4b71Sopenharmony_ci OH_CryptoKeyPair_Destroy(keyPair); 60e41f4b71Sopenharmony_ci return ret; 61e41f4b71Sopenharmony_ci } 62e41f4b71Sopenharmony_ci OH_CryptoAsymKeyGenerator_Destroy(generator); 63e41f4b71Sopenharmony_ci OH_CryptoKeyPair_Destroy(keyPair); 64e41f4b71Sopenharmony_ci return ret; 65e41f4b71Sopenharmony_ci} 66e41f4b71Sopenharmony_ci``` 67