• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16import { cryptoFramework } from '@kit.CryptoArchitectureKit';
17import { buffer } from '@kit.ArkTS';
18
19// 加密消息
20function encryptMessage(publicKey: cryptoFramework.PubKey, plainText: cryptoFramework.DataBlob) {
21  let cipher = cryptoFramework.createCipher('SM2_256|SM3');
22  cipher.initSync(cryptoFramework.CryptoMode.ENCRYPT_MODE, publicKey, null);
23  let encryptData = cipher.doFinalSync(plainText);
24  return encryptData;
25}
26
27// 解密消息
28function decryptMessage(privateKey: cryptoFramework.PriKey, cipherText: cryptoFramework.DataBlob) {
29  let decoder = cryptoFramework.createCipher('SM2_256|SM3');
30  decoder.initSync(cryptoFramework.CryptoMode.DECRYPT_MODE, privateKey, null);
31  let decryptData = decoder.doFinalSync(cipherText);
32  return decryptData;
33}
34
35// 生成SM2密钥对
36function genKeyPairByData(pubKeyData: Uint8Array, priKeyData: Uint8Array) {
37  let pubKeyBlob: cryptoFramework.DataBlob = { data: pubKeyData };
38  let priKeyBlob: cryptoFramework.DataBlob = { data: priKeyData };
39  let sm2Generator = cryptoFramework.createAsyKeyGenerator('SM2_256');
40  let keyPair = sm2Generator.convertKeySync(pubKeyBlob, priKeyBlob);
41  console.info('convertKeySync success');
42  return keyPair;
43}
44
45function main() {
46  let pkData =
47    new Uint8Array([48, 89, 48, 19, 6, 7, 42, 134, 72, 206, 61, 2, 1, 6, 8, 42, 129, 28, 207, 85, 1, 130, 45, 3, 66, 0,
48      4, 90, 3, 58, 157, 190, 248, 76, 7, 132, 200, 151, 208, 112, 230, 96, 140, 90, 238, 211, 155, 128, 109, 248, 40,
49      83, 214, 78, 42, 104, 106, 55, 148, 249, 35, 61, 32, 221, 135, 143, 100, 45, 97, 194, 176, 52, 73, 136, 174, 40,
50      70, 70, 34, 103, 103, 161, 99, 27, 187, 13, 187, 109, 244, 13, 7]);
51  let skData =
52    new Uint8Array([48, 49, 2, 1, 1, 4, 32, 54, 41, 239, 240, 63, 188, 134, 113, 31, 102, 149, 203, 245, 89, 15, 15, 47,
53      202, 170, 60, 38, 154, 28, 169, 189, 100, 251, 76, 112, 223, 156, 159, 160, 10, 6, 8, 42, 129, 28, 207, 85, 1,
54      130, 45]);
55  let keyPair = genKeyPairByData(pkData, skData);
56  let pubKey = keyPair.pubKey;
57  let priKey = keyPair.priKey;
58  let message = 'This is a test';
59  // 把字符串按utf-8解码为Uint8Array
60  let plainText: cryptoFramework.DataBlob = { data: new Uint8Array(buffer.from(message, 'utf-8').buffer) };
61  let encryptText = encryptMessage(pubKey, plainText);
62  let decryptText = decryptMessage(priKey, encryptText);
63  if (plainText.data.toString() === decryptText.data.toString()) {
64    console.info('decrypt ok');
65    // 把Uint8Array按utf-8编码为字符串
66    let messageDecrypted = buffer.from(decryptText.data).toString('utf-8');
67    console.info('decrypted result string:' + messageDecrypted);
68  } else {
69    console.error('decrypt failed');
70  }
71}
72
73@Entry
74@Component
75struct Index {
76  @State message: string = 'SM2 Sync';
77
78  build() {
79    Column({ space: 12 }) {
80      Text(this.message).fontSize(20).fontWeight(FontWeight.Bold)
81      Button($r('app.string.call_sm2_sync'))
82        .width('70%')
83        .onClick(() => {
84          try {
85            main();
86            this.message = 'SM2 Sync Success';
87          } catch {
88            this.message = 'SM2 Sync Fail';
89          }
90        })
91    }
92    .height('100%')
93    .width('100%')
94  }
95}