代码如下:
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
}
新建Test.js文件内容如下:
然后编写在HarmonyOS代码中引用js文件中的方法: