crypto.randomBytes不接受md5生成的utf8字符串

时间:2017-06-07 06:52:54

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

我的代码如下所示:

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
  4. 的结果

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

    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)中上一步生成的哈希。这次执行中是否缺少任何方法?

1 个答案:

答案 0 :(得分:0)

crypto.randomBytes takes 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);