admin 管理员组文章数量: 1086019
I am trying to encrypt and decrypt data on react-native. so I decided to use the crypto node module in my react native project through browserify. Below is the code snippet I used for encryption but it throws the error TypeError: The first argument must be one of type string, Buffer, ArrayBuffer, Array, or Array-like Object. Received type object in cryptoJS. additionally when I use the code in nodeJS, it works fine but in react native it throws that error. what am I doing wrong here? I think the error was initiated from BUffer.from statement which thinks that the variable k is not an array or more like object. but this is my thought I don't know what the real cause is. Here is the code snippet
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);
I am trying to encrypt and decrypt data on react-native. so I decided to use the crypto node module in my react native project through browserify. Below is the code snippet I used for encryption but it throws the error TypeError: The first argument must be one of type string, Buffer, ArrayBuffer, Array, or Array-like Object. Received type object in cryptoJS. additionally when I use the code in nodeJS, it works fine but in react native it throws that error. what am I doing wrong here? I think the error was initiated from BUffer.from statement which thinks that the variable k is not an array or more like object. but this is my thought I don't know what the real cause is. Here is the code snippet
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);
Share
Improve this question
edited Jul 13, 2020 at 9:57
hanan
asked Jul 13, 2020 at 6:19
hananhanan
6322 gold badges10 silver badges29 bronze badges
3
-
1
In the code
ciphertext
andcipher
are mixed up and alsociph
andciphertext
. Fix that. Try to replacenull
with''
increateCipheriv
. – Topaco Commented Jul 13, 2020 at 8:24 - 1 Thank you for your ment. can you structure as an answer the replace null with '' in createCipheriv, just to accept as an anwser. Thanks – hanan Commented Jul 13, 2020 at 10:03
-
I doubt that
var k = new Buffer.from(key);
is correct, according to node documentation new Buffer has been replaced by Buffer.from(key), so you can't have both new and from (in node versions later than node 8), so it should bevar k = Buffer.from(key);
– stamstam Commented Feb 23, 2021 at 16:39
1 Answer
Reset to default 4The issue was resolved, by just replacing null with ' ' in createCipheriv, thanks @Topaco
本文标签:
版权声明:本文标题:javascript - TypeError: The first argument must be one of type string, Buffer, ArrayBuffer, Array, or Array-like Object. Receive 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.roclinux.cn/p/1743639184a2454940.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论