【问题标题】:crypto.randomBytes does not accept md5 generated utf8 stringcrypto.randomBytes 不接受 md5 生成的 utf8 字符串
【发布时间】:2017-11-08 09:17:01
【问题描述】:

我的代码如下图:

let createCipher = (req, res) => {
    const token = req.body.token;
    let keyVal = req.body.keyVal;
    const codeToken = utf8.encode(token);
    keyVal = utf8.encode(keyVal);
    console.log("keyVal " + keyVal);
    let hash = crypto.createHash('md5').update(codeToken).digest('hex');
    console.log("hash " + hash);
    var sharedSecret = crypto.randomBytes(hash);
    var initializationVector = crypto.randomBytes(hash);
    console.log("iv " + initializationVector);
    var encrypted;
    cipher = crypto.Cipheriv('aes-128-cbc', sharedSecret, initializationVector);
    encrypted += cipher.update(keyVal, 'utf8', 'base64');
    encrypted += cipher.final('base64');
    res.json({
        status: '200',
        cipher: encrypted
    });
}

我已经按照下面给出的步骤编写了上面的代码:

  1. UTF-8 编码 TempToken 字符串并生成它的 MD5 哈希。
  2. UTF-8 对键值对字符串进行编码,并使用密码块链接 (CBC) 模式使用 AES-128 加密进行加密。 一种。将密钥和初始化向量 (IV) 设置为等于步骤 1 的结果。
  3. Base64 编码 2 的结果

但是上面的代码给了我如下所示的错误:

TypeError: size must be a number >= 0
            <br> &nbsp; &nbsp;at TypeError (native)
            <br> &nbsp; &nbsp;at createCipher (C:\Users\anand\quFlipApi\controller\test.js:17:31)
            <br> &nbsp; &nbsp;at Layer.handle [as handle_request] (C:\Users\anand\quFlipApi\node_modules\express\lib\router\layer.js:95:5)
            <br> &nbsp; &nbsp;at trim_prefix (C:\Users\anand\quFlipApi\node_modules\express\lib\router\index.js:317:13)
            <br> &nbsp; &nbsp;at C:\Users\anand\quFlipApi\node_modules\express\lib\router\index.js:284:7
            <br> &nbsp; &nbsp;at Function.process_params (C:\Users\anand\quFlipApi\node_modules\express\lib\router\index.js:335:12)
            <br> &nbsp; &nbsp;at next (C:\Users\anand\quFlipApi\node_modules\express\lib\router\index.js:275:10)
            <br> &nbsp; &nbsp;at C:\Users\anand\quFlipApi\node_modules\express\lib\router\index.js:635:15
            <br> &nbsp; &nbsp;at next (C:\Users\anand\quFlipApi\node_modules\express\lib\router\index.js:260:14)
            <br> &nbsp; &nbsp;at Function.handle (C:\Users\anand\quFlipApi\node_modules\express\lib\router\index.js:174:3)
            <br> &nbsp; &nbsp;at router (C:\Users\anand\quFlipApi\node_modules\express\lib\router\index.js:47:12)
            <br> &nbsp; &nbsp;at Layer.handle [as handle_request] (C:\Users\anand\quFlipApi\node_modules\express\lib\router\layer.js:95:5)
            <br> &nbsp; &nbsp;at trim_prefix (C:\Users\anand\quFlipApi\node_modules\express\lib\router\index.js:317:13)
            <br> &nbsp; &nbsp;at C:\Users\anand\quFlipApi\node_modules\express\lib\router\index.js:284:7
            <br> &nbsp; &nbsp;at Function.process_params (C:\Users\anand\quFlipApi\node_modules\express\lib\router\index.js:335:12)
            <br> &nbsp; &nbsp;at next (C:\Users\anand\quFlipApi\node_modules\express\lib\router\index.js:275:10)
            <br> &nbsp; &nbsp;at C:\Users\anand\quFlipApi\node_modules\body-parser\lib\read.js:129:5
            <br> &nbsp; &nbsp;at invokeCallback (C:\Users\anand\quFlipApi\node_modules\raw-body\index.js:262:16)
            <br> &nbsp; &nbsp;at done (C:\Users\anand\quFlipApi\node_modules\raw-body\index.js:251:7)
            <br> &nbsp; &nbsp;at IncomingMessage.onEnd (C:\Users\anand\quFlipApi\node_modules\raw-body\index.js:307:7)
            <br> &nbsp; &nbsp;at emitNone (events.js:86:13)
            <br> &nbsp; &nbsp;at IncomingMessage.emit (events.js:185:7)

这里我的代码不接受crypto.randomBytes(hash) 中上一步生成的hash。这次执行中是否缺少任何方法?

【问题讨论】:

  • “crypto.randomBytes 不接受 md5 生成的 utf8 字符串” - 为什么你认为它应该? crypto.randomBytes 生成的名称已经暗示了随机字节。您可以提供的唯一输入是它应该生成的字节数。你想通过它达到什么目的? AES-128-CBC 需要一个 16 字节的密钥和一个 16 字节的 IV。您将生成的密钥存储在哪里?

标签: node.js encryption hash encoding utf-8


【解决方案1】:

crypto.randomBytestakes a number as argument,它返回随机字节,正如名字所说。

你不能给它一个字符串作为参数。

我真的不明白,为什么你会生成两次随机字节。也许这就足够了?

let hash = crypto.createHash('md5').update(codeToken).digest();
console.log("hash" + hash.toString('hex'));
var initializationVector = crypto.randomBytes(16);
console.log("iv " + initializationVector); 
var encrypted = crypto.Cipheriv('aes-128-cbc', hash, initializationVector);

【讨论】:

  • 但是我怎样才能像我在步骤中提到的那样传递 key 和 iv?
  • 我不知道你的算法,所以这里是一个猜测。
  • 我试过你的代码,但它给了我错误。我已经更新了代码中的算法步骤,那么该算法的代码可以吗?
  • @Mthaker 我修复了这段代码中的大小问题(十六进制编码的 MD5 哈希是 32 字节长而不是 16 字节长)。
  • IV 必须是不可预测的(阅读:随机)。不要使用静态IV或从密钥派生的IV(在这种情况下相同),因为这使密码具有确定性,因此在语义上不安全。观察密文的攻击者可以确定之前发送相同消息前缀的时间。 IV 不是秘密,因此您可以将其与密文一起发送。通常,它只是简单地添加到密文中,并在解密之前被切掉。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-02-22
  • 1970-01-01
  • 2021-09-28
  • 2018-12-10
  • 1970-01-01
  • 1970-01-01
  • 2011-04-01
相关资源
最近更新 更多