javascript - TypeError: The first argument must be one of type string, Buffer, ArrayBuffer, Array, or Array-like Object. Receive

I am trying to encrypt and decrypt data on react-native. so I decided to use the crypto node module in

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 and cipher are mixed up and also ciph and ciphertext. Fix that. Try to replace null with '' in createCipheriv. – 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 be var k = Buffer.from(key); – stamstam Commented Feb 23, 2021 at 16:39
Add a ment  | 

1 Answer 1

Reset to default 4

The issue was resolved, by just replacing null with ' ' in createCipheriv, thanks @Topaco

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743639184a4482670.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信