1# 使用RSA非对称密钥(PKCS1模式)加解密(C/C++) 2 3<!--Kit: Crypto Architecture Kit--> 4<!--Subsystem: Security--> 5<!--Owner: @zxz--3--> 6<!--Designer: @lanming--> 7<!--Tester: @PAFT--> 8<!--Adviser: @zengyawen--> 9 10对应的算法规格请查看[非对称密钥加解密算法规格:RSA](crypto-asym-encrypt-decrypt-spec.md#rsa)。 11 12**加密** 13 141. 调用[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),生成RSA密钥类型为RSA1024、素数个数为2的非对称密钥对(keyPair)。keyPair对象中包括公钥PubKey、私钥PriKey。 15 16 如何生成RSA非对称密钥对,开发者可参考下文示例,并结合[非对称密钥生成和转换规格:RSA](crypto-asym-key-generation-conversion-spec.md#rsa)和[随机生成非对称密钥对](crypto-generate-asym-key-pair-randomly.md)理解。参考文档与当前示例可能存在入参差异,请在阅读时注意区分。 17 182. 调用[OH_CryptoAsymCipher_Create](../../reference/apis-crypto-architecture-kit/capi-crypto-asym-cipher-h.md#oh_cryptoasymcipher_create),指定字符串参数'RSA1024|PKCS1',创建非对称密钥类型为RSA1024、填充模式为PKCS1的Cipher实例,用于完成加解密操作。 19 203. 调用[OH_CryptoAsymCipher_Init](../../reference/apis-crypto-architecture-kit/capi-crypto-asym-cipher-h.md#oh_cryptoasymcipher_init),设置模式为加密(CRYPTO_ENCRYPT_MODE),指定加密密钥(keyPair),初始化加密Cipher实例。 21 224. 调用[OH_CryptoAsymCipher_Final](../../reference/apis-crypto-architecture-kit/capi-crypto-asym-cipher-h.md#oh_cryptoasymcipher_final),传入明文,获取加密后的数据。 23 24 - OH_CryptoAsymCipher_Final输出结果可能为NULL,在访问具体数据前,需要先判断结果是否为NULL,避免产生异常。 25 - 当数据量较大时,可以多次调用OH_CryptoAsymCipher_Final,即[分段加解密](crypto-rsa-asym-encrypt-decrypt-by-segment-ndk.md)。 26 27**解密** 28 291. 由于RSA算法的Cipher实例不支持重复init操作,需要调用[OH_CryptoAsymCipher_Create](../../reference/apis-crypto-architecture-kit/capi-crypto-asym-cipher-h.md#oh_cryptoasymcipher_create),重新生成Cipher实例。 30 312. 调用[OH_CryptoAsymCipher_Init](../../reference/apis-crypto-architecture-kit/capi-crypto-asym-cipher-h.md#oh_cryptoasymcipher_init),设置模式为解密(CRYPTO_DECRYPT_MODE),指定解密密钥(keyPair)初始化解密Cipher实例。 32 333. 调用[OH_CryptoAsymCipher_Final](../../reference/apis-crypto-architecture-kit/capi-crypto-asym-cipher-h.md#oh_cryptoasymcipher_final),传入密文,获取解密后的数据。 34 35```C++ 36#include "CryptoArchitectureKit/crypto_architecture_kit.h" 37#include <string.h> 38 39static OH_Crypto_ErrCode doTestRsaEncDec() 40{ 41 OH_CryptoAsymKeyGenerator *keyGen = nullptr; 42 OH_Crypto_ErrCode ret = OH_CryptoAsymKeyGenerator_Create("RSA1024", &keyGen); 43 if (ret != CRYPTO_SUCCESS) { 44 return ret; 45 } 46 47 OH_CryptoKeyPair *keyPair = nullptr; 48 ret = OH_CryptoAsymKeyGenerator_Generate(keyGen, &keyPair); 49 if (ret != CRYPTO_SUCCESS) { 50 OH_CryptoAsymKeyGenerator_Destroy(keyGen); 51 return ret; 52 } 53 54 OH_CryptoAsymCipher *cipher = nullptr; 55 ret = OH_CryptoAsymCipher_Create("RSA1024|PKCS1", &cipher); 56 if (ret != CRYPTO_SUCCESS) { 57 OH_CryptoKeyPair_Destroy(keyPair); 58 OH_CryptoAsymKeyGenerator_Destroy(keyGen); 59 return ret; 60 } 61 62 ret = OH_CryptoAsymCipher_Init(cipher, CRYPTO_ENCRYPT_MODE, keyPair); 63 if (ret != CRYPTO_SUCCESS) { 64 OH_CryptoAsymCipher_Destroy(cipher); 65 OH_CryptoKeyPair_Destroy(keyPair); 66 OH_CryptoAsymKeyGenerator_Destroy(keyGen); 67 return ret; 68 } 69 70 const char *testData = "Hello, RSA!"; 71 Crypto_DataBlob in = { 72 .data = (uint8_t *)testData, 73 .len = strlen(testData) 74 }; 75 76 Crypto_DataBlob out = { 0 }; 77 ret = OH_CryptoAsymCipher_Final(cipher, &in, &out); 78 if (ret != CRYPTO_SUCCESS) { 79 OH_CryptoAsymCipher_Destroy(cipher); 80 OH_CryptoKeyPair_Destroy(keyPair); 81 OH_CryptoAsymKeyGenerator_Destroy(keyGen); 82 return ret; 83 } 84 85 OH_CryptoAsymCipher_Destroy(cipher); 86 cipher = nullptr; 87 ret = OH_CryptoAsymCipher_Create("RSA1024|PKCS1", &cipher); 88 if (ret != CRYPTO_SUCCESS) { 89 OH_Crypto_FreeDataBlob(&out); 90 OH_CryptoKeyPair_Destroy(keyPair); 91 OH_CryptoAsymKeyGenerator_Destroy(keyGen); 92 return ret; 93 } 94 95 ret = OH_CryptoAsymCipher_Init(cipher, CRYPTO_DECRYPT_MODE, keyPair); 96 if (ret != CRYPTO_SUCCESS) { 97 OH_CryptoAsymCipher_Destroy(cipher); 98 OH_Crypto_FreeDataBlob(&out); 99 OH_CryptoKeyPair_Destroy(keyPair); 100 OH_CryptoAsymKeyGenerator_Destroy(keyGen); 101 return ret; 102 } 103 Crypto_DataBlob decrypted = { 0 }; 104 ret = OH_CryptoAsymCipher_Final(cipher, &out, &decrypted); 105 if (ret != CRYPTO_SUCCESS) { 106 OH_CryptoAsymCipher_Destroy(cipher); 107 OH_Crypto_FreeDataBlob(&out); 108 OH_CryptoKeyPair_Destroy(keyPair); 109 OH_CryptoAsymKeyGenerator_Destroy(keyGen); 110 return ret; 111 } 112 if ((decrypted.len != strlen(testData)) || (memcmp(decrypted.data, testData, decrypted.len) != 0)) { 113 OH_Crypto_FreeDataBlob(&decrypted); 114 OH_CryptoAsymCipher_Destroy(cipher); 115 OH_Crypto_FreeDataBlob(&out); 116 OH_CryptoKeyPair_Destroy(keyPair); 117 OH_CryptoAsymKeyGenerator_Destroy(keyGen); 118 return CRYPTO_OPERTION_ERROR; 119 } 120 121 OH_Crypto_FreeDataBlob(&decrypted); 122 OH_CryptoAsymCipher_Destroy(cipher); 123 OH_Crypto_FreeDataBlob(&out); 124 OH_CryptoKeyPair_Destroy(keyPair); 125 OH_CryptoAsymKeyGenerator_Destroy(keyGen); 126 return ret; 127} 128```