TypeError: The first argument must be one of type string, Buffer, ArrayBuffer, Array, or Array-like Object. Received type object in cryptoJS
TypeError: The first argument must be one of type string, Buffer, ArrayBuffer, Array, or Array-like Object. Received type object in cryptoJS
我正在尝试在 react-native 上加密和解密数据。所以我决定通过 browserify 在我的 React Native 项目中使用加密节点模块。下面是我用于加密的代码片段,但它抛出错误 TypeError:第一个参数必须是字符串、缓冲区、ArrayBuffer、数组或类数组对象之一。在 cryptoJS 中接收到类型对象。另外,当我在 nodeJS 中使用代码时,它工作正常但在本机反应中它会抛出该错误。我在这里做错了什么?我认为错误是由 BUffer.from 语句引发的,该语句认为变量 k 不是数组或更像对象。但这是我的想法,我不知道真正的原因是什么。
这是代码片段
const algorithm = 'des-ede';
const key = [
43,
57,
97,
-68,
-63,
-61,
-40,
9,
50,
87,
-104,
101,
63,
34,
-78,
60,
];
var CryptoJS = require('../crypto/crypto');
var k = new Buffer.from(key);
let cipher = CryptoJS.createCipheriv(algorithm, k, null);
cipher.setAutoPadding(true); //default true
var ciph = cipher.update("Hello World!'", 'utf8', 'base64');
ciph += cipher.final('base64');
console.log(ciph);
问题已解决,只需在 createCipheriv 中将 null 替换为 ' ',谢谢 @Topaco
我正在尝试在 react-native 上加密和解密数据。所以我决定通过 browserify 在我的 React Native 项目中使用加密节点模块。下面是我用于加密的代码片段,但它抛出错误 TypeError:第一个参数必须是字符串、缓冲区、ArrayBuffer、数组或类数组对象之一。在 cryptoJS 中接收到类型对象。另外,当我在 nodeJS 中使用代码时,它工作正常但在本机反应中它会抛出该错误。我在这里做错了什么?我认为错误是由 BUffer.from 语句引发的,该语句认为变量 k 不是数组或更像对象。但这是我的想法,我不知道真正的原因是什么。 这是代码片段
const algorithm = 'des-ede';
const key = [
43,
57,
97,
-68,
-63,
-61,
-40,
9,
50,
87,
-104,
101,
63,
34,
-78,
60,
];
var CryptoJS = require('../crypto/crypto');
var k = new Buffer.from(key);
let cipher = CryptoJS.createCipheriv(algorithm, k, null);
cipher.setAutoPadding(true); //default true
var ciph = cipher.update("Hello World!'", 'utf8', 'base64');
ciph += cipher.final('base64');
console.log(ciph);
问题已解决,只需在 createCipheriv 中将 null 替换为 ' ',谢谢 @Topaco