简体   繁体   中英

TypeError: The first argument must be one of type string, Buffer, ArrayBuffer, Array, or Array-like Object. Received type object in cryptoJS

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);

在此处输入图像描述

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM