HarmonyOS 加解密方式 AES/ECB/PKCS5Padding

AES/ECB/PKCS5Padding 关于AES加解密如何处理,ECB模式,PKCS5Padding填充方式

HarmonyOS
1天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
superinsect

可参考如下代码:

import cryptoFramework from '@ohos.security.cryptoFramework';
import buffer from '@ohos.buffer';
import { util } from '@kit.ArkTS';
import { promptAction } from '@kit.ArkUI';

@Entry
@Component
struct Index {
  @State message: string = 'Hello World';

  build() {
    Column() {
      Button('点击解密').onClick(async () => {
        let str = await aesDecryptECB(HexStrTouint8Array("57ae1c7e5bfe36b6dc7b827c6df6c8a5"),
          StringToUint8Array("dd172571bf74ec8c21ccae1d1990c446"));
        console.log(`解密result:${str}`)

        promptAction.showToast({ message: `${str}`, duration: 5000 })
      })


      Button('点击加密').onClick(async () => {
        let str = await aesEncryptECB(StringToUint8Array('5488'),
          StringToUint8Array("dd172571bf74ec8c21ccae1d1990c446"));


        promptAction.showToast({ message: `${str}`, duration: 5000 })
      })
    }.width('100%').height('100%').justifyContent(FlexAlign.Center)
  }
}


export async function aesEncryptECB(text: Uint8Array, key: Uint8Array) {
  let globalResult = ""
  try {


    let cipherAlgName = 'AES256|ECB|PKCS5';
    let globalCipher = cryptoFramework.createCipher(cipherAlgName);
    let symAlgName = 'AES256';
    let symKeyGenerator = cryptoFramework.createSymKeyGenerator(symAlgName);
    let keyBlob: cryptoFramework.DataBlob = { data: key }
    let promiseSymKey = await symKeyGenerator.convertKey(keyBlob)
    await globalCipher.init(cryptoFramework.CryptoMode.ENCRYPT_MODE, promiseSymKey, null);
    let plainText: cryptoFramework.DataBlob = { data: text }
    let result = await globalCipher.doFinal(plainText)
    // globalResult = uint8ArrayToString(result.data);
    let str = Uint8ArrayToHexStr(result.data)
    console.log("加密后的密文==》" + str)

    //再解密验证
    aesDecryptECB(HexStrTouint8Array(str), StringToUint8Array("dd172571bf74ec8c21ccae1d1990c446"))

  } catch (e) {
    console.log(e.message)
  }
  return globalResult;
}

export async function aesDecryptECB(text: Uint8Array, key: Uint8Array) {
  let globalResult = ""
  try {
    let cipherAlgName = 'AES256|ECB|PKCS5';
    let globalCipher = cryptoFramework.createCipher(cipherAlgName);
    let symAlgName = 'AES256';
    let symKeyGenerator = cryptoFramework.createSymKeyGenerator(symAlgName);
    let keyBlob: cryptoFramework.DataBlob = { data: key }
    let promiseSymKey = await symKeyGenerator.convertKey(keyBlob)
    await globalCipher.init(cryptoFramework.CryptoMode.DECRYPT_MODE, promiseSymKey, null);
    let plainText: cryptoFramework.DataBlob = { data: text }
    let result = await globalCipher.doFinal(plainText)
    globalResult = uint8ArrayToString(result.data);
    console.log("解密后的明文==》" + globalResult)
  } catch (err) {
    console.log(err.message)
  }
  return globalResult;
}

export function uint8ArrayToString(input: Uint8Array) {
  let textDecoder = util.TextDecoder.create('utf-8');
  return textDecoder.decodeWithStream(input);
}

//十六进制转Uint8Array
export function HexStrTouint8Array(data: string): Uint8Array {
  return new Uint8Array(buffer.from(data, 'hex').buffer);
}

export function Uint8ArrayToHexStr(data: Uint8Array): string {
  return buffer.from(data).toString('hex')
}

export function StringToUint8Array(str: String) {
  let arr: number[] = new Array();
  for (let i = 0, j = str.length; i < j; ++i) {
    arr.push(str.charCodeAt(i));
  }
  return new Uint8Array(arr);
}
分享
微博
QQ
微信
回复
1天前
相关问题
HarmonyOS AES/ECB/PKCS7Padding 加解密
26浏览 • 1回复 待解决
HarmonyOS AES加解密咨询
89浏览 • 1回复 待解决
HarmonyOS AES加解密问题
37浏览 • 1回复 待解决
如何使用AES ECB 加密方式?
222浏览 • 1回复 待解决
HarmonyOS AES加解密与java无法通用
518浏览 • 1回复 待解决
HarmonyOS aes gcm加解密c/c++库
65浏览 • 1回复 待解决
多种加密方式实现加解密
1081浏览 • 1回复 待解决
如何进行不同规格的AES加解密
858浏览 • 1回复 待解决
HarmonyOS AES加解密过程实在是调不通
22浏览 • 1回复 待解决
AES加解密长字符串是否需要分段
619浏览 • 1回复 待解决
加解密HmacSha1 、HmacSha256、aes参考Demo
2220浏览 • 1回复 待解决
HarmonyOS 加解密问题
48浏览 • 1回复 待解决