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
}
HarmonyOS
1天前
浏览
收藏 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')
}

然后编写在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)
  }
}
分享
微博
QQ
微信
回复
1天前
相关问题
HarmonyOS rsa加密报错
50浏览 • 1回复 待解决
RSA加密,使用自有私钥解密报错
276浏览 • 1回复 待解决
HarmonyOS AES128|CBC|NoPadding 加密报错
58浏览 • 1回复 待解决
HarmonyOS 使用官网的des加密报错
57浏览 • 1回复 待解决
HarmonyOS AES解密报错
720浏览 • 1回复 待解决
HarmonyOS crypto-js加密没有返回值
34浏览 • 1回复 待解决
加密算法(crypto-js)Arkts转化代码
1147浏览 • 1回复 待解决
RSA非对称加密-@hms-security/agoh-crypto
566浏览 • 1回复 待解决
@ohos/smartrefreshlayout运行报错
402浏览 • 1回复 待解决
HarmonyOS crypto-js 性能存在问题
437浏览 • 1回复 待解决
HarmonyOS 使用crypto进行秘钥交换
59浏览 • 1回复 待解决
import asset from '@ohos.security.asset'报错
2197浏览 • 1回复 待解决
HarmonyOS crypto-js 解密中乱码问题
760浏览 • 1回复 待解决