可以使用三方库crypto-js实现AES的加解密,三方库地址:https://gitee.com/openharmony-sig/crypto-js示例代码:import CryptoJS from '@ohos/crypto-js' @Entry @Component struct CryptoAESPage { encryptDes() { let keyHex = CryptoJS.enc.Utf8.parse('559f5f6c5e5f22a4') let encrypted = CryptoJS.AES.encrypt('我是原文', keyHex, { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 }) let enMsg = encrypted.ciphertext.toString(CryptoJS.enc.Hex) console.info('------>加密:' + enMsg) } decryptDes() { let keyHex = CryptoJS.enc.Utf8.parse('559f5f6c5e5f22a4') let enMsg = '17cea7b20cda1f320f4298b82969ca68' let words = CryptoJS.enc.Hex.parse(enMsg) let base64Str = CryptoJS.enc.Base64.stringify(words) let decrypted= CryptoJS.AES.decrypt(base64Str, keyHex, { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 }) let deMsg = decrypted.toString(CryptoJS.enc.Utf8) console.info('------>解密:' + deMsg) } build() { Column() { Text('AES加密').textAlign(TextAlign.Center) .width(200).height(100) .onClick(() => { this.encryptDes() }) Text('AES解密') .textAlign(TextAlign.Center) .width(200) .height(100) .margin({ top: 30 }) .onClick(() => { this.decryptDes() }) }.width('100%').height('100%').justifyContent(FlexAlign.Center) } }
可以使用三方库crypto-js实现AES的加解密,三方库地址:https://gitee.com/openharmony-sig/crypto-js
示例代码: