1# 使用SM2密文格式转换(ArkTS) 2 3<!--Kit: Crypto Architecture Kit--> 4<!--Subsystem: Security--> 5<!--Owner: @zxz--3--> 6<!--Designer: @lanming--> 7<!--Tester: @PAFT--> 8<!--Adviser: @zengyawen--> 9 10当前支持的SM2密文格式为国密标准的ASN.1格式,其中各参数组合顺序为C1C3C2,具体参数含义请参考[转换SM2密文格式](crypto-asym-encrypt-decrypt-spec.md#转换sm2密文格式)。 11 12开发者可指定SM2密文的参数,将其转换成符合国密标准的ASN.1格式密文。反之,也可以从国密标准的ASN.1格式密文中取出具体的SM2密文参数,便于开发者自行组合成其他格式的SM2密文。 13 14**指定密文参数,生成标准ASN.1密文** 15 161. 构造[SM2CipherTextSpec](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#sm2ciphertextspec12)对象,用于指定SM2密文参数。如果开发者使用的不是国密标准的ASN.1格式密文,需自行提取所需要的参数。 17 182. 调用[genCipherTextBySpec](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#genciphertextbyspec12),将SM2CipherTextSpec对象传入,生成符合国密标准的ASN.1格式的SM2密文。 19 203. 生成的密文可直接使用cryptoFramework进行SM2解密。 21 22```ts 23import { cryptoFramework } from '@kit.CryptoArchitectureKit'; 24 25function testGenCipherTextBySpec() { 26 let spec : cryptoFramework.SM2CipherTextSpec = { 27 xCoordinate: BigInt('20625015362595980457695435345498579729138244358573902431560627260141789922999'), 28 yCoordinate: BigInt('48563164792857017065725892921053777369510340820930241057309844352421738767712'), 29 cipherTextData: new Uint8Array([100, 227, 78, 195, 249, 179, 43, 70, 242, 69, 169, 10, 65, 123]), 30 hashData: new Uint8Array([87, 167, 167, 247, 88, 146, 203, 234, 83, 126, 117, 129, 52, 142, 82, 54, 152, 226, 201, 111, 143, 115, 169, 125, 128, 42, 157, 31, 114, 198, 109, 244]) 31 } 32 // 此处的data可直接使用cryptoFramework进行SM2解密。 33 let data = cryptoFramework.SM2CryptoUtil.genCipherTextBySpec(spec, 'C1C3C2'); 34 console.info('genCipherTextBySpec success'); 35} 36``` 37 38**从标准ASN.1密文中,获取密文参数** 39 40 411. 准备符合国密标准的ASN.1格式的SM2密文。 42 432. 调用[getCipherTextSpec](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#genciphertextbyspec12),从标准密文中,获取具体的SM2密文参数。 44 453. 根据业务需要,自行拼接SM2密文参数,形成其他格式的SM2密文。 46 47```ts 48import { cryptoFramework } from '@kit.CryptoArchitectureKit'; 49 50function testGetCipherTextSpec() { 51 let cipherTextArray = new Uint8Array([48, 118, 2, 32, 45, 153, 88, 82, 104, 221, 226, 43, 174, 21, 122, 248, 5, 232, 105, 41, 92, 95, 102, 224, 216, 149, 85, 236, 110, 6, 64, 188, 149, 70, 70, 183, 2, 32, 107, 93, 198, 247, 119, 18, 40, 110, 90, 156, 193, 158, 205, 113, 170, 128, 146, 109, 75, 17, 181, 109, 110, 91, 149, 5, 110, 233, 209, 78, 229, 96, 4, 32, 87, 167, 167, 247, 88, 146, 203, 234, 83, 126, 117, 129, 52, 142, 82, 54, 152, 226, 201, 111, 143, 115, 169, 125, 128, 42, 157, 31, 114, 198, 109, 244, 4, 14, 100, 227, 78, 195, 249, 179, 43, 70, 242, 69, 169, 10, 65, 123]); 52 let cipherText : cryptoFramework.DataBlob = {data : cipherTextArray}; 53 let spec : cryptoFramework.SM2CipherTextSpec = cryptoFramework.SM2CryptoUtil.getCipherTextSpec(cipherText, 'C1C3C2'); 54 console.info('getCipherTextSpec success'); 55} 56``` 57