需要帮助修复此代码才能与CrptoJs或OpenSSL命令一起使用

时间:2018-11-01 15:09:51

标签: node.js openssl cryptojs

尊敬的StackOverflow成员, 我在一个封闭的环境中,除了此crypto-Js模块的OpenSSL或CrptoJs版本之外,无法使用节点js或任何其他语言。

我的代码在Node Js中工作,我在这里附加了工作版本。 你们中的任何人都可以帮助我在OpenSSL或CrptoJs中获得可用的版本吗?

'use strict';
const crypto = require('crypto');

// The two keys gotten from Trustpilot
let encryptionKeyBase64 = 'JB+FsIQEMWjdhB/xLus/SpbaZje9wQQ785tECAsHzF4=';
let authenticationKeyBase64 = 'HFLYS943++xwKuj3qTypmB9F+pFzmNavccKJO+Mm2nY=';

// Our info to encrypt
let info = {
    "email":"xyz@domain.com",
    "name":"John Smith",
    "ref":"1234",
    "skus":["sku1","sku2","sku3"],
    "tags":["tag1","tag2","tag3"]
};
let jsonSerializedOrder = JSON.stringify(info);

// When you get the keys from Trustpilot, they are base64 encoded, so first we need to decode them
let encryptionKey = Buffer.from(encryptionKeyBase64, 'base64');
console.log('encryptionKey'+encryptionKey);
let authenticationKey = Buffer.from(authenticationKeyBase64, 'base64');
console.log('authenticationKey'+authenticationKey);
// Generate a random initialization vector
let iv = crypto.randomBytes(16);
console.log('iv'+iv);
// Encrypt our order
let cipher = crypto.createCipheriv('aes-256-cbc', encryptionKey, iv);
console.log('cipher'+cipher);
let cipherText = Buffer.concat([cipher.update(jsonSerializedOrder, 'utf8'), cipher.final()]);
console.log('cipherText'+cipherText);
// Compute the HMAC
let hmac = crypto.createHmac('sha256', authenticationKey).update(Buffer.concat([iv, cipherText])).digest();
console.log('hmac'+hmac);
// Base64 encode the IV + cipherText + HMAC
let base64Payload = Buffer.concat([iv, cipherText, hmac]).toString("base64");
console.log('base64Payload'+base64Payload);
// URL encode to get the final payload
let payload = encodeURIComponent(base64Payload);


console.log(payload);

0 个答案:

没有答案