HarmonyOS @ohos/node-polyfill crypto加密报错

代码如下:

import Buffer from '@ohos.buffer';
import { crypto } from '@ohos/node-polyfill'

const de = (str: any) => {
  return Buffer.from(Buffer.from(str, 'hex')
    .toString()
    .split('')
    .map(val => {
      return val.charCodeAt(0)
    })
    .map(val => {
      return String.fromCharCode(val - 6)
    })
    .join(''), 'hex')
}

export function getDCSecurityKey() {
  return de('\u0033\u0062\u0033\u0037\u0033\u0039\u0033\u0036\u0033\u0039\u0033\u0063\u0033\u0061\u0033\u0065\u0033\u0062\u0033\u0062\u0033\u0039\u0033\u0062\u0033\u0063\u0033\u0038\u0033\u0063\u0036\u0061\u0033\u0039\u0033\u0061\u0033\u0063\u0036\u0063\u0033\u0064\u0033\u0064\u0033\u0061\u0033\u0037\u0033\u0063\u0033\u0062\u0033\u0061\u0033\u0061\u0033\u0063\u0033\u0039\u0033\u0061\u0033\u0065')
    .toString()
}

export function getDBSecurityKey() {
  return Buffer.from(getDCSecurityKey())
}

export function simpleChiper(data: any, key = getDCSecurityKey(), algorithm = 'aes-128-cbc') {
  let chunk = Buffer.alloc(0)
  console.log(chunk)
  let cip = crypto.createCipheriv(algorithm, key, Buffer.alloc(16, 0x00))
  console.log(cip)
  cip.setAutoPadding(false)
  console.log(cip)
  data = Buffer.concat([Buffer.from(data), Buffer.alloc(16 - Buffer.from(data).length % 16, 0x00)])
  console.log(data)
  const updateData = cip.update(data, 'binary')
  console.log(updateData)
  chunk = Buffer.concat([chunk, cip.update(data, 'binary')])
  chunk = Buffer.concat([chunk, cip.final()])
  return chunk
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
HarmonyOS
2024-12-25 09:40:03
571浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
fox280

新建Test.js文件内容如下:

import { buffer } from '@ohos/node-polyfill';

export function getDCSecurityKey() {
  return de('\u0033\u0062\u0033\u0037\u0033\u0039\u0033\u0036\u0033\u0039\u0033\u0063\u0033\u0061\u0033\u0065\u0033\u0062\u0033\u0062\u0033\u0039\u0033\u0062\u0033\u0063\u0033\u0038\u0033\u0063\u0036\u0061\u0033\u0039\u0033\u0061\u0033\u0063\u0036\u0063\u0033\u0064\u0033\u0064\u0033\u0061\u0033\u0037\u0033\u0063\u0033\u0062\u0033\u0061\u0033\u0061\u0033\u0063\u0033\u0039\u0033\u0061\u0033\u0065')
    .toString()
}

export function getDBSecurityKey() {
  return buffer.Buffer.from(getDCSecurityKey())
}

export function de(str) {
  return buffer.Buffer.from(buffer.Buffer.from(str, 'hex')
    .toString()
    .split('')
    .map(val => {
      return val.charCodeAt(0)
    })
    .map(val => {
      return String.fromCharCode(val - 6)
    })
    .join(''), 'hex')
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.

然后编写在HarmonyOS代码中引用js文件中的方法:

import { buffer, crypto } from '@ohos/node-polyfill';
import { getDBSecurityKey } from './test';

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

  simpleChiper(data: ESObject, key: string, algorithm = 'aes-128-cbc'): ESObject {
    let chunk: ESObject = buffer.Buffer.alloc(0)
    let cip: ESObject = crypto.createCipheriv(algorithm, key, buffer.Buffer.alloc(16, 0x00));
    cip.setAutoPadding(false)
    data = buffer.Buffer.concat([buffer.Buffer.from(data),
    buffer.Buffer.alloc(16 - buffer.Buffer.from(data).length % 16, 0x00)])
    console.log(data)
    const updateData: ESObject = cip.update(data, 'binary')
    console.log(updateData)
    chunk = buffer.Buffer.concat([chunk, cip.update(data, 'binary')])
       chunk = buffer.Buffer.concat([chunk, cip.final()])
       return chunk
  }

  build() {
    Column() {
      Button('点击').onClick(() => {
        let res = this.simpleChiper('1223', getDBSecurityKey()) as string
        AlertDialog.show({ message: `执行结果为:${res}` })
      })
    }
    .width("100%")
    .height("100%")
    .justifyContent(FlexAlign.Center)
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
分享
微博
QQ
微信
回复
2024-12-25 12:26:55


相关问题
HarmonyOS rsa加密报错
886浏览 • 1回复 待解决
RSA加密,使用自有私钥解密报错
1050浏览 • 1回复 待解决
HarmonyOS AES128|CBC|NoPadding 加密报错
675浏览 • 1回复 待解决
HarmonyOS 使用官网的des加密报错
863浏览 • 1回复 待解决
HarmonyOS AES解密报错
1347浏览 • 1回复 待解决
HarmonyOS AES加解密报错17630001
1033浏览 • 1回复 待解决
HarmonyOS crypto-js加密没有返回值
879浏览 • 1回复 待解决
加密算法(crypto-js)Arkts转化代码
1959浏览 • 1回复 待解决
RSA非对称加密-@hms-security/agoh-crypto
1345浏览 • 1回复 待解决
@ohos/smartrefreshlayout运行报错
718浏览 • 0回复 待解决
@ohos/smartrefreshlayout运行报错
1159浏览 • 1回复 待解决
HarmonyOS AES128/CBC/NoPadding加密模式报错
689浏览 • 1回复 待解决
HarmonyOS crypto-js 性能存在问题
1090浏览 • 1回复 待解决
HarmonyOS 使用crypto进行秘钥交换
817浏览 • 1回复 待解决
import asset from '@ohos.security.asset'报错
3193浏览 • 1回复 待解决
HarmonyOS crypto-js 解密中乱码问题
1739浏览 • 1回复 待解决