harmony 鸿蒙Converting a Compressed or Uncompressed ECC Public Key (C/C++)
Converting a Compressed or Uncompressed ECC Public Key (C/C++)
You can generate a public key object PubKey) from ECC public key data or obtain the ECC public key data from a public key (PubKey) object.
Currently, 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.
For details about the ECC algorithm specifications, see ECC.
You 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.
Converting Uncompressed Public Key Data to Compressed Public Key Data
- Specify uncompressed ECC public key data of the uint8_t type and encapsulate the data into a Crypto_DataBlob object. Either the public key or private key can be passed in. In this example, an uncompressed public key is passed in.
- Call OH_CryptoAsymKeyGenerator_Create with the string parameter ‘ECC_BrainPoolP256r1’ to create an asymmetric key generator (OH_CryptoAsymKeyGenerator) object for a 256-bit ECC key pair.
- Call OH_CryptoAsymKeyGenerator_Convert to convert the Crypto_DataBlob object into an asymmetric key object (OH_CryptoKeyPair).
- Call OH_CryptoPubKey_Encode with the parameter set to ‘X509|COMPRESSED’ to obtain the byte stream of the compressed public key data.
#include "CryptoArchitectureKit/crypto_common.h"
#include "CryptoArchitectureKit/crypto_asym_key.h"
static OH_Crypto_ErrCode doTestEccDataCovert()
{
OH_CryptoAsymKeyGenerator *generator = nullptr;
OH_CryptoKeyPair *keyPair = nullptr;
Crypto_DataBlob returnBlob = { .data = nullptr, .len = 0 };
OH_Crypto_ErrCode ret = CRYPTO_INVALID_PARAMS;
ret = OH_CryptoAsymKeyGenerator_Create("ECC_BrainPoolP256r1", &generator);
if (ret != CRYPTO_SUCCESS) {
return ret;
}
uint8_t pubKeyBlobData[] = {
48, 90, 48, 20, 6, 7, 42, 134, 72, 206, 61, 2, 1, 6, 9, 43, 36, 3, 3, 2,
8, 1, 1, 7, 3, 66, 0, 4, 143, 39, 57, 249, 145, 50, 63, 222, 35, 70, 178,
121, 202, 154, 21, 146, 129, 75, 76, 63, 8, 195, 157, 111, 40, 217, 215,
148, 120, 224, 205, 82, 83, 92, 185, 21, 211, 184, 5, 19, 114, 33, 86, 85,
228,123, 242, 206, 200, 98, 178, 184, 130, 35, 232, 45, 5, 202, 189, 11,
46, 163, 156, 152
};
Crypto_DataBlob pubKeyUncompressedBlob = {
.data = pubKeyBlobData,
.len = sizeof(pubKeyBlobData),
};
ret = OH_CryptoAsymKeyGenerator_Convert(generator, CRYPTO_DER, &pubKeyUncompressedBlob, nullptr, &keyPair);
if (ret != CRYPTO_SUCCESS) {
OH_CryptoAsymKeyGenerator_Destroy(generator);
return ret;
}
OH_CryptoPubKey *pubKey = OH_CryptoKeyPair_GetPubKey(keyPair);
ret = OH_CryptoPubKey_Encode(pubKey, CRYPTO_DER, "X509|COMPRESSED", &returnBlob);
if (ret != CRYPTO_SUCCESS) {
OH_CryptoAsymKeyGenerator_Destroy(generator);
OH_CryptoKeyPair_Destroy(keyPair);
return ret;
}
OH_CryptoAsymKeyGenerator_Destroy(generator);
OH_CryptoKeyPair_Destroy(keyPair);
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++)
- 所属分类: 后端技术
- 本文标签:
热门推荐
-
2、 - 优质文章
-
3、 gate.io
-
8、 golang
-
9、 openharmony
-
10、 Vue中input框自动聚焦