1# 使用ECC压缩/非压缩公钥格式转换(C/C++) 2 3可通过指定ECC公钥数据,生成公钥对象([PubKey](../../reference/apis-crypto-architecture-kit/_crypto_asym_key_api.md#oh_cryptopubkey));也可从公钥对象中,获取ECC公钥数据。 4当前仅支持ECC算法中,满足X509规范的压缩/非压缩格式的公钥数据。此处的公钥数据应当是完整的X509公钥,对于只使用点数据的情况,请参考[使用ECC压缩/非压缩点格式转换](crypto-convert-compressed-or-uncompressed-ECC-point.md)。 5ECC的算法规格请查看[非对称密钥生成和转换规格:ECC](crypto-asym-key-generation-conversion-spec.md#ecc)。 6通过传入字符串参数,可指定需要获取的ECC公钥数据格式。如果需要获取满足X509规范的压缩格式数据,则指定参数为:"X509|COMPRESSED";需要获取非压缩格式,则指定参数为:"X509|UNCOMPRESSED"。 7 8## 指定非压缩公钥数据转换为压缩公钥数据 9 101. 指定uint8_t类型的ECC非压缩公钥数据,封装成[Crypto_DataBlob](../../reference/apis-crypto-architecture-kit/_crypto_common_api.md#crypto_datablob)。 11公钥和私钥可只传入其中一个,此处示例以传入非压缩公钥为例。 122. 调用[OH_CryptoAsymKeyGenerator_Create](../../reference/apis-crypto-architecture-kit/_crypto_asym_key_api.md#oh_cryptoasymkeygenerator_generate),指定字符串参数'ECC_BrainPoolP256r1',创建密钥算法为ECC、密钥长度为256位的非对称密钥生成器(OH_CryptoAsymKeyGenerator)。 133. 调用[OH_CryptoAsymKeyGenerator_Convert](../../reference/apis-crypto-architecture-kit/_crypto_asym_key_api.md#oh_cryptoasymkeygenerator_convert),传入封装后的[Crypto_DataBlob](../../reference/apis-crypto-architecture-kit/_crypto_common_api.md#crypto_datablob),生成非对称密钥对象(OH_CryptoKeyPair)。 144. 调用[OH_CryptoPubKey_Encode](../../reference/apis-crypto-architecture-kit/_crypto_asym_key_api.md#oh_cryptopubkey_encode),设置参数为'X509|COMPRESSED',获取压缩公钥数据的字节流。 15 16```c++ 17#include "CryptoArchitectureKit/crypto_common.h" 18#include "CryptoArchitectureKit/crypto_asym_key.h" 19 20static OH_Crypto_ErrCode doTestEccDataCovert() 21{ 22 OH_CryptoAsymKeyGenerator *generator = nullptr; 23 OH_CryptoKeyPair *keyPair = nullptr; 24 Crypto_DataBlob returnBlob = { .data = nullptr, .len = 0 }; 25 OH_Crypto_ErrCode ret = CRYPTO_INVALID_PARAMS; 26 27 ret = OH_CryptoAsymKeyGenerator_Create("ECC_BrainPoolP256r1", &generator); 28 if (ret != CRYPTO_SUCCESS) { 29 return ret; 30 } 31 uint8_t pubKeyBlobData[] = { 32 48, 90, 48, 20, 6, 7, 42, 134, 72, 206, 61, 2, 1, 6, 9, 43, 36, 3, 3, 2, 33 8, 1, 1, 7, 3, 66, 0, 4, 143, 39, 57, 249, 145, 50, 63, 222, 35, 70, 178, 34 121, 202, 154, 21, 146, 129, 75, 76, 63, 8, 195, 157, 111, 40, 217, 215, 35 148, 120, 224, 205, 82, 83, 92, 185, 21, 211, 184, 5, 19, 114, 33, 86, 85, 36 228,123, 242, 206, 200, 98, 178, 184, 130, 35, 232, 45, 5, 202, 189, 11, 37 46, 163, 156, 152 38 }; 39 Crypto_DataBlob pubKeyUncompressedBlob = { 40 .data = pubKeyBlobData, 41 .len = sizeof(pubKeyBlobData), 42 }; 43 ret = OH_CryptoAsymKeyGenerator_Convert(generator, CRYPTO_DER, &pubKeyUncompressedBlob, nullptr, &keyPair); 44 if (ret != CRYPTO_SUCCESS) { 45 OH_CryptoAsymKeyGenerator_Destroy(generator); 46 return ret; 47 } 48 49 OH_CryptoPubKey *pubKey = OH_CryptoKeyPair_GetPubKey(keyPair); 50 ret = OH_CryptoPubKey_Encode(pubKey, CRYPTO_DER, "X509|COMPRESSED", &returnBlob); 51 if (ret != CRYPTO_SUCCESS) { 52 OH_CryptoAsymKeyGenerator_Destroy(generator); 53 OH_CryptoKeyPair_Destroy(keyPair); 54 return ret; 55 } 56 OH_CryptoAsymKeyGenerator_Destroy(generator); 57 OH_CryptoKeyPair_Destroy(keyPair); 58 return ret; 59} 60``` 61