# 使用SM2非对称密钥加解密(C/C++) 对应的算法规格请查看[非对称密钥加解密算法规格:SM2](crypto-asym-encrypt-decrypt-spec.md#sm2)。 **加密** 1. 调用[OH_CryptoAsymKeyGenerator_Create](../../reference/apis-crypto-architecture-kit/capi-crypto-asym-key-h.md#oh_cryptoasymkeygenerator_create)、[OH_CryptoAsymKeyGenerator_Generate](../../reference/apis-crypto-architecture-kit/capi-crypto-asym-key-h.md#oh_cryptoasymkeygenerator_generate),生成SM2密钥类型为SM2_256的非对称密钥对(keyPair)。keyPair对象中包括公钥PubKey、私钥PriKey。 如何生成SM2非对称密钥对,开发者可参考下文示例,并结合[非对称密钥生成和转换规格:SM2](crypto-asym-key-generation-conversion-spec.md#sm2)和[随机生成非对称密钥对](crypto-generate-asym-key-pair-randomly-ndk.md)理解。参考文档与当前示例可能存在入参差异,请在阅读时注意区分。 2. 调用[OH_CryptoAsymCipher_Create](../../reference/apis-crypto-architecture-kit/capi-crypto-asym-cipher-h.md#oh_cryptoasymcipher_create),指定字符串参数'SM2_256|SM3',创建非对称密钥类型为SM2_256、摘要算法为SM3的Cipher实例,用于完成加解密操作。 3. 调用[OH_CryptoAsymCipher_Init](../../reference/apis-crypto-architecture-kit/capi-crypto-asym-cipher-h.md#oh_cryptoasymcipher_init),设置模式为加密(CRYPTO_ENCRYPT_MODE),指定加密密钥(keyPair),初始化加密Cipher实例。 4. 调用[OH_CryptoAsymCipher_Final](../../reference/apis-crypto-architecture-kit/capi-crypto-asym-cipher-h.md#oh_cryptoasymcipher_final),传入明文,获取加密后的数据。 OH_CryptoAsymCipher_Final输出结果可能为NULL,在访问具体数据前,需要先判断结果是否为NULL,避免产生异常。 **解密** 1. 由于SM2算法的Cipher实例不支持重复init操作,需要调用[OH_CryptoAsymCipher_Create](../../reference/apis-crypto-architecture-kit/capi-crypto-asym-cipher-h.md#oh_cryptoasymcipher_create),重新生成Cipher实例。 2. 调用[OH_CryptoAsymCipher_Init](../../reference/apis-crypto-architecture-kit/capi-crypto-asym-cipher-h.md#oh_cryptoasymcipher_init),设置模式为解密(CRYPTO_DECRYPT_MODE),指定解密密钥(keyPair)初始化解密Cipher实例。 3. 调用[OH_CryptoAsymCipher_Final](../../reference/apis-crypto-architecture-kit/capi-crypto-asym-cipher-h.md#oh_cryptoasymcipher_final),传入密文,获取解密后的数据。 ```C++ #include "CryptoArchitectureKit/crypto_architecture_kit.h" #include #include #include static std::vector doTestSm2Enc(OH_CryptoKeyPair *keyPair, std::vector &plainText) { std::vector cipherText; OH_CryptoAsymCipher *cipher = nullptr; OH_Crypto_ErrCode ret = OH_CryptoAsymCipher_Create("SM2_256|SM3", &cipher); if (ret != CRYPTO_SUCCESS) { return std::vector{}; } ret = OH_CryptoAsymCipher_Init(cipher, CRYPTO_ENCRYPT_MODE, keyPair); if (ret != CRYPTO_SUCCESS) { OH_CryptoAsymCipher_Destroy(cipher); return std::vector{}; } Crypto_DataBlob in = {}; in.data = plainText.data(); in.len = plainText.size(); Crypto_DataBlob out = {}; ret = OH_CryptoAsymCipher_Final(cipher, &in, &out); if (ret != CRYPTO_SUCCESS) { OH_CryptoAsymCipher_Destroy(cipher); return std::vector{}; } cipherText.insert(cipherText.end(), out.data, out.data + out.len); OH_Crypto_FreeDataBlob(&out); OH_CryptoAsymCipher_Destroy(cipher); return cipherText; } static std::vector doTestSm2Dec(OH_CryptoKeyPair *keyPair, std::vector &encryptText) { std::vector decryptText; OH_CryptoAsymCipher *cipher = nullptr; OH_Crypto_ErrCode ret = OH_CryptoAsymCipher_Create("SM2_256|SM3", &cipher); if (ret != CRYPTO_SUCCESS) { return std::vector{}; } ret = OH_CryptoAsymCipher_Init(cipher, CRYPTO_DECRYPT_MODE, keyPair); if (ret != CRYPTO_SUCCESS) { OH_CryptoAsymCipher_Destroy(cipher); return std::vector{}; } Crypto_DataBlob in = {}; in.data = encryptText.data(); in.len = encryptText.size(); Crypto_DataBlob out = {}; ret = OH_CryptoAsymCipher_Final(cipher, &in, &out); if (ret != CRYPTO_SUCCESS) { OH_CryptoAsymCipher_Destroy(cipher); return std::vector{}; } decryptText.insert(decryptText.end(), out.data, out.data + out.len); OH_Crypto_FreeDataBlob(&out); OH_CryptoAsymCipher_Destroy(cipher); return decryptText; } static OH_Crypto_ErrCode doTestSm2EncMessage() { OH_CryptoAsymKeyGenerator *keyGen = nullptr; OH_Crypto_ErrCode ret = OH_CryptoAsymKeyGenerator_Create("SM2_256", &keyGen); if (ret != CRYPTO_SUCCESS) { return ret; } OH_CryptoKeyPair *keyPair = nullptr; ret = OH_CryptoAsymKeyGenerator_Generate(keyGen, &keyPair); if (ret != CRYPTO_SUCCESS) { OH_CryptoAsymKeyGenerator_Destroy(keyGen); return ret; } std::string message = "This is a test"; std::vector plainText(message.begin(), message.end()); std::vector cipherText = doTestSm2Enc(keyPair, plainText); std::vector decryptText = doTestSm2Dec(keyPair, cipherText); if ((plainText.size() != decryptText.size()) || (!std::equal(plainText.begin(), plainText.end(), decryptText.begin()))) { OH_CryptoKeyPair_Destroy(keyPair); OH_CryptoAsymKeyGenerator_Destroy(keyGen); return CRYPTO_OPERTION_ERROR; } OH_CryptoKeyPair_Destroy(keyPair); OH_CryptoAsymKeyGenerator_Destroy(keyGen); return CRYPTO_SUCCESS; } ```