【问题标题】:TypeError: The first argument must be one of type string, Buffer, ArrayBuffer, Array, or Array-like Object. Received type object in cryptoJSTypeError:第一个参数必须是 string、Buffer、ArrayBuffer、Array 或 Array-like Object 类型之一。在cryptoJS中接收到类型对象
【发布时间】:2020-11-02 07:36:11
【问题描述】:

我正在尝试加密和解密 react-native 上的数据。所以我决定通过 browserify 在我的 react native 项目中使用加密节点模块。下面是我用于加密的代码 sn-p,但它抛出错误 TypeError: The first argument must be one type string, Buffer, ArrayBuffer, Array, or Array-like Object。在 cryptoJS 中接收到类型对象。另外,当我在 nodeJS 中使用代码时,它可以正常工作,但是在本机反应中会引发该错误。我在这里做错了什么?我认为错误是从认为变量 k 不是数组或更类似于对象的 BUffer.from 语句引发的。但这是我的想法,我不知道真正的原因是什么。 这是代码sn-p

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

【问题讨论】:

  • 在代码中ciphertextcipher 混淆了,还有ciphciphertext。修复它。尝试在createCipheriv 中将null 替换为''
  • 我的错,我修好了,请再看一遍
  • 感谢您的评论。您能否将 createCipheriv 中的用 '' 替换为 null 作为答案,只是为了接受作为 anwser。谢谢
  • 我怀疑var k = new Buffer.from(key); 是否正确,根据节点文档,新缓冲区已被 Buffer.from(key) 替换,因此您不能同时拥有 new 和 from(在节点版本晚于节点 8),所以应该是var k = Buffer.from(key);

标签: javascript node.js react-native encryption browserify


【解决方案1】:

问题已解决,只需将 createCipheriv 中的 null 替换为 ' ',感谢 @Topaco

【讨论】:

    猜你喜欢
    • 2020-03-29
    • 2020-12-09
    • 2021-12-07
    • 1970-01-01
    • 1970-01-01
    • 2022-01-12
    • 2020-11-21
    • 1970-01-01
    相关资源
    最近更新 更多