1# Converting SM2 Ciphertext (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 10The Crypto framework supports the SM2 ciphertext in ASN.1 format. The SM2 ciphertext consists of C1, C3 (the hash value), and C2 (the encrypted data). For details about the specifications, see [SM2 Ciphertext Format](crypto-asym-encrypt-decrypt-spec.md#sm2-ciphertext-format). 11 12You can convert the SM2 ciphertext into ASN.1 format based on the SM2 parameters specified or obtain SM2 parameters from the SM2 ciphertext in ASN.1 format. 13 14**Generating Ciphertext in ASN.1 Format** 15 161. Call [OH_CryptoSm2CiphertextSpec_Create](../../reference/apis-crypto-architecture-kit/capi-crypto-asym-cipher-h.md#oh_cryptosm2ciphertextspec_create) to create an empty SM2 ciphertext specification object. 17 182. Call [OH_CryptoSm2CiphertextSpec_SetItem](../../reference/apis-crypto-architecture-kit/capi-crypto-asym-cipher-h.md#oh_cryptosm2ciphertextspec_setitem) to set the ciphertext parameters (**C1.x**,** C1.y**, **C2**, and **C3**). 19 203. Call [OH_CryptoSm2CiphertextSpec_Encode](../../reference/apis-crypto-architecture-kit/capi-crypto-asym-cipher-h.md#oh_cryptosm2ciphertextspec_encode) to generate ciphertext in ASN.1 format. (Currently, only SM3 ciphertext can be converted. During implementation, the hash length of 32 bytes is verified only.) 21 224. Call [OH_CryptoSm2CiphertextSpec_Destroy](../../reference/apis-crypto-architecture-kit/capi-crypto-asym-cipher-h.md#oh_cryptosm2ciphertextspec_destroy) to destroy the SM2 ciphertext specification object. 23 24```C++ 25#include "CryptoArchitectureKit/crypto_architecture_kit.h" 26 27static OH_Crypto_ErrCode doTestGenCipherTextBySpec() 28{ 29 // Prepare SM2 ciphertext parameters. 30 uint8_t c1x[] = {45, 153, 88, 82, 104, 221, 226, 43, 174, 21, 122, 248, 5, 232, 105, 41, 92, 95, 102, 224, 31 216, 149, 85, 236, 110, 6, 64, 188, 149, 70, 70, 183}; 32 uint8_t c1y[] = {107, 93, 198, 247, 119, 18, 40, 110, 90, 156, 193, 158, 205, 113, 170, 128, 146, 109, 75, 33 17, 181, 109, 110, 91, 149, 5, 110, 233, 209, 78, 229, 96}; 34 uint8_t c2[] = {100, 227, 78, 195, 249, 179, 43, 70, 242, 69, 169, 10, 65, 123}; 35 uint8_t c3[] = {87, 167, 167, 247, 88, 146, 203, 234, 83, 126, 117, 129, 52, 142, 82, 54, 152, 226, 201, 111, 36 143, 115, 169, 125, 128, 42, 157, 31, 114, 198, 109, 244}; 37 38 // Create an empty SM2 ciphertext specification object. 39 OH_CryptoSm2CiphertextSpec *sm2CipherSpec = nullptr; 40 OH_Crypto_ErrCode ret = OH_CryptoSm2CiphertextSpec_Create(nullptr, &sm2CipherSpec); 41 if (ret != CRYPTO_SUCCESS) { 42 return ret; 43 } 44 45 // Set parameters. 46 Crypto_DataBlob c1xBlob = {c1x, sizeof(c1x)}; 47 Crypto_DataBlob c1yBlob = {c1y, sizeof(c1y)}; 48 Crypto_DataBlob c2Blob = {c2, sizeof(c2)}; 49 Crypto_DataBlob c3Blob = {c3, sizeof(c3)}; 50 51 ret = OH_CryptoSm2CiphertextSpec_SetItem(sm2CipherSpec, CRYPTO_SM2_CIPHERTEXT_C1_X, &c1xBlob); 52 if (ret != CRYPTO_SUCCESS) { 53 OH_CryptoSm2CiphertextSpec_Destroy(sm2CipherSpec); 54 return ret; 55 } 56 ret = OH_CryptoSm2CiphertextSpec_SetItem(sm2CipherSpec, CRYPTO_SM2_CIPHERTEXT_C1_Y, &c1yBlob); 57 if (ret != CRYPTO_SUCCESS) { 58 OH_CryptoSm2CiphertextSpec_Destroy(sm2CipherSpec); 59 return ret; 60 } 61 ret = OH_CryptoSm2CiphertextSpec_SetItem(sm2CipherSpec, CRYPTO_SM2_CIPHERTEXT_C2, &c2Blob); 62 if (ret != CRYPTO_SUCCESS) { 63 OH_CryptoSm2CiphertextSpec_Destroy(sm2CipherSpec); 64 return ret; 65 } 66 ret = OH_CryptoSm2CiphertextSpec_SetItem(sm2CipherSpec, CRYPTO_SM2_CIPHERTEXT_C3, &c3Blob); 67 if (ret != CRYPTO_SUCCESS) { 68 OH_CryptoSm2CiphertextSpec_Destroy(sm2CipherSpec); 69 return ret; 70 } 71 72 // Encode the ciphertext in ASN.1 format. 73 Crypto_DataBlob encoded = { 0 }; 74 ret = OH_CryptoSm2CiphertextSpec_Encode(sm2CipherSpec, &encoded); 75 if (ret != CRYPTO_SUCCESS) { 76 OH_CryptoSm2CiphertextSpec_Destroy(sm2CipherSpec); 77 return ret; 78 } 79 80 // Free resources. 81 OH_Crypto_FreeDataBlob(&encoded); 82 OH_CryptoSm2CiphertextSpec_Destroy(sm2CipherSpec); 83 return ret; 84} 85``` 86 87**Obtaining Parameters from Ciphertext in ASN.1 Format** 88 891. Call [OH_CryptoSm2CiphertextSpec_Create](../../reference/apis-crypto-architecture-kit/capi-crypto-asym-cipher-h.md#oh_cryptosm2ciphertextspec_create) to create an SM2 ciphertext specification object from the ASN.1 ciphertext. 90 912. Call [OH_CryptoSm2CiphertextSpec_GetItem](../../reference/apis-crypto-architecture-kit/capi-crypto-asym-cipher-h.md#oh_cryptosm2ciphertextspec_getitem) to obtain the ciphertext parameters (**C1.x**, **C1.y**, **C2**, and **C3**). 92 933. Call [OH_CryptoSm2CiphertextSpec_Destroy](../../reference/apis-crypto-architecture-kit/capi-crypto-asym-cipher-h.md#oh_cryptosm2ciphertextspec_destroy) to destroy the SM2 ciphertext specification object. 94 95```C++ 96static OH_Crypto_ErrCode doTestGetCipherTextSpec() 97{ 98 // Prepare the standard ASN.1 ciphertext. 99 uint8_t cipherTextArray[] = {48, 118, 2, 32, 45, 153, 88, 82, 104, 221, 226, 43, 174, 21, 122, 248, 5, 232, 105, 100 41, 92, 95, 102, 224, 216, 149, 85, 236, 110, 6, 64, 188, 149, 70, 70, 183, 2, 32, 107, 101 93, 198, 247, 119, 18, 40, 110, 90, 156, 193, 158, 205, 113, 170, 128, 146, 109, 75, 17, 102 181, 109, 110, 91, 149, 5, 110, 233, 209, 78, 229, 96, 4, 32, 87, 167, 167, 247, 88, 146, 103 203, 234, 83, 126, 117, 129, 52, 142, 82, 54, 152, 226, 201, 111, 143, 115, 169, 125, 128, 104 42, 157, 31, 114, 198, 109, 244, 4, 14, 100, 227, 78, 195, 249, 179, 43, 70, 242, 69, 169, 105 10, 65, 123}; 106 Crypto_DataBlob cipherText = {cipherTextArray, sizeof(cipherTextArray)}; 107 108 // Create an SM2 ciphertext specification object from the ASN.1 ciphertext. 109 OH_CryptoSm2CiphertextSpec *sm2CipherSpec = nullptr; 110 OH_Crypto_ErrCode ret = OH_CryptoSm2CiphertextSpec_Create(&cipherText, &sm2CipherSpec); 111 if (ret != CRYPTO_SUCCESS) { 112 return ret; 113 } 114 115 // Obtain the parameters. 116 Crypto_DataBlob c1x = { 0 }; 117 Crypto_DataBlob c1y = { 0 }; 118 Crypto_DataBlob c2 = { 0 }; 119 Crypto_DataBlob c3 = { 0 }; 120 121 ret = OH_CryptoSm2CiphertextSpec_GetItem(sm2CipherSpec, CRYPTO_SM2_CIPHERTEXT_C1_X, &c1x); 122 if (ret != CRYPTO_SUCCESS) { 123 goto EXIT; 124 } 125 ret = OH_CryptoSm2CiphertextSpec_GetItem(sm2CipherSpec, CRYPTO_SM2_CIPHERTEXT_C1_Y, &c1y); 126 if (ret != CRYPTO_SUCCESS) { 127 goto EXIT; 128 } 129 ret = OH_CryptoSm2CiphertextSpec_GetItem(sm2CipherSpec, CRYPTO_SM2_CIPHERTEXT_C2, &c2); 130 if (ret != CRYPTO_SUCCESS) { 131 goto EXIT; 132 } 133 ret = OH_CryptoSm2CiphertextSpec_GetItem(sm2CipherSpec, CRYPTO_SM2_CIPHERTEXT_C3, &c3); 134 if (ret != CRYPTO_SUCCESS) { 135 goto EXIT; 136 } 137 138EXIT: 139 OH_Crypto_FreeDataBlob(&c1x); 140 OH_Crypto_FreeDataBlob(&c1y); 141 OH_Crypto_FreeDataBlob(&c2); 142 OH_Crypto_FreeDataBlob(&c3); 143 OH_CryptoSm2CiphertextSpec_Destroy(sm2CipherSpec); 144 return ret; 145} 146``` 147