My code is as shown below:
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
});
}
I have written the above code as per the steps given below:
- UTF-8 encode the TempToken string and generate an MD5 hash of it.
- UTF-8 encode the Key-Value Pair string and encrypt using AES-128 encryption using Cipher Block Chaining (CBC) mode. a. Set both the key and initialization vector (IV) equal to result from step 1.
- Base64 encode the result of 2
But the above code gives me the error as shown below:
TypeError: size must be a number >= 0
<br> at TypeError (native)
<br> at createCipher (C:\Users\anand\quFlipApi\controller\test.js:17:31)
<br> at Layer.handle [as handle_request] (C:\Users\anand\quFlipApi\node_modules\express\lib\router\layer.js:95:5)
<br> at trim_prefix (C:\Users\anand\quFlipApi\node_modules\express\lib\router\index.js:317:13)
<br> at C:\Users\anand\quFlipApi\node_modules\express\lib\router\index.js:284:7
<br> at Function.process_params (C:\Users\anand\quFlipApi\node_modules\express\lib\router\index.js:335:12)
<br> at next (C:\Users\anand\quFlipApi\node_modules\express\lib\router\index.js:275:10)
<br> at C:\Users\anand\quFlipApi\node_modules\express\lib\router\index.js:635:15
<br> at next (C:\Users\anand\quFlipApi\node_modules\express\lib\router\index.js:260:14)
<br> at Function.handle (C:\Users\anand\quFlipApi\node_modules\express\lib\router\index.js:174:3)
<br> at router (C:\Users\anand\quFlipApi\node_modules\express\lib\router\index.js:47:12)
<br> at Layer.handle [as handle_request] (C:\Users\anand\quFlipApi\node_modules\express\lib\router\layer.js:95:5)
<br> at trim_prefix (C:\Users\anand\quFlipApi\node_modules\express\lib\router\index.js:317:13)
<br> at C:\Users\anand\quFlipApi\node_modules\express\lib\router\index.js:284:7
<br> at Function.process_params (C:\Users\anand\quFlipApi\node_modules\express\lib\router\index.js:335:12)
<br> at next (C:\Users\anand\quFlipApi\node_modules\express\lib\router\index.js:275:10)
<br> at C:\Users\anand\quFlipApi\node_modules\body-parser\lib\read.js:129:5
<br> at invokeCallback (C:\Users\anand\quFlipApi\node_modules\raw-body\index.js:262:16)
<br> at done (C:\Users\anand\quFlipApi\node_modules\raw-body\index.js:251:7)
<br> at IncomingMessage.onEnd (C:\Users\anand\quFlipApi\node_modules\raw-body\index.js:307:7)
<br> at emitNone (events.js:86:13)
<br> at IncomingMessage.emit (events.js:185:7)
Here my code does not accept hash generated from previous step in crypto.randomBytes(hash)
. Is there any method missing in this execution?
via M thaker
No comments:
Post a Comment