• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# 使用SM2密文格式转换
2
3当前支持的SM2密文格式为国密标准的ASN.1格式,其中各参数组合顺序为C1C3C2,具体参数含义请参考[转换SM2密文格式](crypto-asym-encrypt-decrypt-spec.md#转换sm2密文格式)。
4
5开发者可指定SM2密文的参数,将其转换成符合国密标准的ASN.1格式密文。反之,也可以从国密标准的ASN.1格式密文中取出具体的SM2密文参数,便于开发者自行组合成其他格式的SM2密文。
6
7**指定密文参数,生成标准ASN.1密文**
8
91. 构造[SM2CipherTextSpec](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#sm2ciphertextspec12)对象,用于指定SM2密文参数。如果开发者使用的不是国密标准的ASN.1格式密文,需自行提取所需要的参数。
10
112. 调用[genCipherTextBySpec](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#genciphertextbyspec12),将SM2CipherTextSpec对象传入,生成符合国密标准的ASN.1格式的SM2密文。
12
133. 生成的密文可直接使用cryptoFramework进行SM2解密。
14
15```ts
16import { cryptoFramework } from '@kit.CryptoArchitectureKit';
17import { buffer } from '@kit.ArkTS';
18
19function testGenCipherTextBySpec() {
20  let spec : cryptoFramework.SM2CipherTextSpec = {
21      xCoordinate: BigInt('20625015362595980457695435345498579729138244358573902431560627260141789922999'),
22      yCoordinate: BigInt('48563164792857017065725892921053777369510340820930241057309844352421738767712'),
23      cipherTextData: new Uint8Array([100, 227, 78, 195, 249, 179, 43, 70, 242, 69, 169, 10, 65, 123]),
24      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]),
25  }
26  // 此处的data可直接使用cryptoFramework进行SM2解密。
27  let data = cryptoFramework.SM2CryptoUtil.genCipherTextBySpec(spec, 'C1C3C2');
28  console.info('genCipherTextBySpec success');
29}
30```
31
32**从标准ASN.1密文中,获取密文参数**
33
34
351. 准备符合国密标准的ASN.1格式的SM2密文。
36
372. 调用[getCipherTextSpec](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#genciphertextbyspec12),从标准密文中,获取具体的SM2密文参数。
38
393. 根据业务需要,自行拼接SM2密文参数,形成其他格式的SM2密文。
40
41```ts
42import { cryptoFramework } from '@kit.CryptoArchitectureKit';
43import { buffer } from '@kit.ArkTS';
44
45function testGetCipherTextSpec() {
46  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]);
47  let cipherText : cryptoFramework.DataBlob = {data : cipherTextArray};
48  let spec : cryptoFramework.SM2CipherTextSpec = cryptoFramework.SM2CryptoUtil.getCipherTextSpec(cipherText, 'C1C3C2');
49  console.info('getCipherTextSpec success');
50}
51```
52