HarmonyOS AES ECB加密后后台解析失败?

如题:HarmonyOS AES ECB加密后后台解析失败?

阅读 532
1 个回答

可以使用三方库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)
  }
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进