From 73de50af1778b59495ec0af00b31dd0790ee7392 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9F=B3=E4=B8=9C=E6=B5=B7?= Date: Tue, 15 Jul 2025 10:26:44 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BB=A3=E7=A0=81=E8=A7=84=E8=8C=83?= =?UTF-8?q?=E9=97=AE=E9=A2=98=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../entry/src/main/ets/pages/GetKey.ets | 1 - .../main/ets/pages/ObtainCertificateHashValue_Two.ets | 2 +- .../entry/src/main/ets/pages/PubKeysConvert.ets | 5 ++--- .../entry/src/main/ets/pages/PublicKeysStored.ets | 1 - .../entry/src/main/ets/pages/RsaDecrypt.ets | 10 ++++++++++ .../entry/src/main/ets/pages/TestAesMultiUpdate.ets | 6 ++++++ .../entry/src/main/ets/pages/SetNetSpecifier.ets | 6 ++---- 7 files changed, 21 insertions(+), 10 deletions(-) diff --git a/CryptoArchitectureKit/entry/src/main/ets/pages/GetKey.ets b/CryptoArchitectureKit/entry/src/main/ets/pages/GetKey.ets index cae077f1..8879b813 100644 --- a/CryptoArchitectureKit/entry/src/main/ets/pages/GetKey.ets +++ b/CryptoArchitectureKit/entry/src/main/ets/pages/GetKey.ets @@ -21,7 +21,6 @@ import { buffer, util } from '@kit.ArkTS'; import { cryptoFramework } from '@kit.CryptoArchitectureKit'; - @Entry @Component struct GetKey { diff --git a/CryptoArchitectureKit/entry/src/main/ets/pages/ObtainCertificateHashValue_Two.ets b/CryptoArchitectureKit/entry/src/main/ets/pages/ObtainCertificateHashValue_Two.ets index 2103f47c..64d773b2 100644 --- a/CryptoArchitectureKit/entry/src/main/ets/pages/ObtainCertificateHashValue_Two.ets +++ b/CryptoArchitectureKit/entry/src/main/ets/pages/ObtainCertificateHashValue_Two.ets @@ -20,7 +20,7 @@ // [Start ObtainCertificateHashValue_Two] import { common } from '@kit.AbilityKit'; import { BusinessError } from '@kit.BasicServicesKit'; -import { hash }from '@kit.CoreFileKit'; +import { hash } from '@kit.CoreFileKit'; let context = AppStorage.get("context") as common.UIAbilityContext; let pathDir = context.filesDir; diff --git a/CryptoArchitectureKit/entry/src/main/ets/pages/PubKeysConvert.ets b/CryptoArchitectureKit/entry/src/main/ets/pages/PubKeysConvert.ets index a19cf6df..8b2afaeb 100644 --- a/CryptoArchitectureKit/entry/src/main/ets/pages/PubKeysConvert.ets +++ b/CryptoArchitectureKit/entry/src/main/ets/pages/PubKeysConvert.ets @@ -23,15 +23,14 @@ import { buffer, util } from '@kit.ArkTS'; @Entry @Component struct PubKeysConvert { - build() { Column(){ - Button('公钥转十六进制').onClick(()=>{ + Button('公钥转十六进制').onClick(() => { let pubKeyData = '公钥' let res = buffer.from(pubKeyData).toString('hex') console.info('公钥转十六进制',res) }) - Button('公钥转base64').onClick(()=>{ + Button('公钥转base64').onClick(() => { let pubKeyUint8Array = new Uint8Array(buffer.from('公钥','utf-8').buffer) let res = new util.Base64Helper().encodeToStringSync(pubKeyUint8Array) console.info('公钥转base64',res) diff --git a/CryptoArchitectureKit/entry/src/main/ets/pages/PublicKeysStored.ets b/CryptoArchitectureKit/entry/src/main/ets/pages/PublicKeysStored.ets index 93428888..f0ac58fc 100644 --- a/CryptoArchitectureKit/entry/src/main/ets/pages/PublicKeysStored.ets +++ b/CryptoArchitectureKit/entry/src/main/ets/pages/PublicKeysStored.ets @@ -44,7 +44,6 @@ struct PublicKeysStored { console.info('base64 byte=',byte) // Convert the converted array into a public key object let keyBlob: cryptoFramework.DataBlob = { data: byte } - }) Button('公钥转为十六进制数据存储').onClick(() => { diff --git a/CryptoArchitectureKit/entry/src/main/ets/pages/RsaDecrypt.ets b/CryptoArchitectureKit/entry/src/main/ets/pages/RsaDecrypt.ets index 8b67080f..48859e5e 100644 --- a/CryptoArchitectureKit/entry/src/main/ets/pages/RsaDecrypt.ets +++ b/CryptoArchitectureKit/entry/src/main/ets/pages/RsaDecrypt.ets @@ -20,10 +20,12 @@ // [Start RsaDecrypt] import { cryptoFramework } from '@kit.CryptoArchitectureKit'; import { buffer, util } from '@kit.ArkTS'; + // 字符串转成字节流 function stringToUint8Array(str: string) { return new Uint8Array(buffer.from(str, 'utf-8').buffer); } + // 字节流转成可理解的字符串 function uint8ArrayToString(array: Uint8Array) { // 将UTF-8编码转换成Unicode编码 @@ -57,16 +59,19 @@ function uint8ArrayToString(array: Uint8Array) { } return out; } + export class KeyPair { publicKey: string = ''; privateKey: string = ''; } + export class RSA { private ASY_KEY_NAME_RSA_3072: string = 'RSA1024'; private ALG_NAME_RSA_3072: string = 'RSA|PKCS1'; static priKey: Uint8Array = new Uint8Array(); //用于临时保存 static pubKey: Uint8Array = new Uint8Array(); //用于临时保存 private base: util.Base64Helper = new util.Base64Helper(); + public async generateRsaKeyPair(): Promise { let keyPair: KeyPair = new KeyPair(); try { @@ -81,6 +86,7 @@ export class RSA { } return keyPair; } + public async add(str: string, publicKey: string): Promise { let result = ''; try { @@ -100,6 +106,7 @@ export class RSA { } return result; } + public async rsaDecrypt(message: string | Uint8Array, privateKey: string): Promise { let result = ''; try { @@ -122,16 +129,19 @@ export class RSA { return result; } } + @Entry @Component struct EncryptedText { @State word: string = '加解密文字'; private EncryptionAndDecryption = new RSA(); + async aboutToAppear(): Promise { let key = await this.EncryptionAndDecryption.generateRsaKeyPair(); let result = await this.EncryptionAndDecryption.add(this.word, key.publicKey); this.EncryptionAndDecryption.rsaDecrypt(result, key.privateKey); } + build() { } } diff --git a/CryptoArchitectureKit/entry/src/main/ets/pages/TestAesMultiUpdate.ets b/CryptoArchitectureKit/entry/src/main/ets/pages/TestAesMultiUpdate.ets index ce4b5b1d..7d0324d3 100644 --- a/CryptoArchitectureKit/entry/src/main/ets/pages/TestAesMultiUpdate.ets +++ b/CryptoArchitectureKit/entry/src/main/ets/pages/TestAesMultiUpdate.ets @@ -19,6 +19,7 @@ // [Start TestAesMultiUpdate] import { cryptoFramework } from '@kit.CryptoArchitectureKit'; + function genIvParamsSpec() { let arr = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; let dataIv = new Uint8Array(arr); @@ -29,6 +30,7 @@ function genIvParamsSpec() { }; return ivParamsSpec; } + function stringToUint8Array(str: string): Uint8Array { let arr: Array = []; for (let i = 0, j = str.length; i < j; i++) { @@ -36,6 +38,7 @@ function stringToUint8Array(str: string): Uint8Array { } return new Uint8Array(arr); } + async function testAesMultiUpdate(plainText: string) { let symAlgName = 'SM4_128'; let length = 1024; @@ -74,13 +77,16 @@ async function testAesMultiUpdate(plainText: string) { await globalCipher.init(cryptoFramework.CryptoMode.DECRYPT_MODE, promiseSymKey, genIvParamsSpec()); console.info('TEST == 长度' + contentTemp.length); } + @Entry @Component struct SM4Encryption { @State message: string = 'Hello World'; + aboutToAppear(): void { testAesMultiUpdate('123456789102345566478416518498454151689546549849'); } + build() { RelativeContainer() { Text(this.message) diff --git a/NetworkKit/entry/src/main/ets/pages/SetNetSpecifier.ets b/NetworkKit/entry/src/main/ets/pages/SetNetSpecifier.ets index 0b134346..de066b1e 100644 --- a/NetworkKit/entry/src/main/ets/pages/SetNetSpecifier.ets +++ b/NetworkKit/entry/src/main/ets/pages/SetNetSpecifier.ets @@ -30,7 +30,6 @@ function listen_network() { }; let conn = connection.createNetConnection(netSpecifier); - conn.register((err: BusinessError, data: void) => { console.warn('register 网络 ' + JSON.stringify(err)) }); @@ -95,14 +94,13 @@ function sub_network() { @Entry @Component struct NetWork { - build() { Row() { Column() { - Button('监听网络').onClick(()=>{ + Button('监听网络').onClick(() => { listen_network() }) - Button('获取网络状态').onClick(()=>{ + Button('获取网络状态').onClick(() => { sub_network() }) } -- Gitee