使用 JavaScript 使用 DES 和 Base64 进行加密和解密

Encrypting and decrypting with DES and Base64 with JavaScript

我正在加密以前用 base64 编码的内容。

步骤是:
1. 以 base64
编码纯文本(可读) 2.使用CryptoJS
对DES/CBC/PKCS7PADDING的base64编码文本进行加密 3. 将步骤2得到的信息再次进行base64编码

这是我在 Javascript:

中的代码
function encryptDesCbcPkcs7Padding(message, key) {
    var keyHex = CryptoJS.enc.Utf8.parse(key);
    var iv = new Uint8Array(0);
    var ivHex = CryptoJS.enc.Hex.parse(CryptoJS.enc.Utf8.parse(iv).toString(CryptoJS.enc.Hex));

    var encrypted = CryptoJS.DES.encrypt(message, keyHex, { iv: ivHex, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 });


    return encrypted;//.toString(CryptoJS.enc.Utf8);
}

var plainText = "hola";
console.log("Plain text: ", plainText);
var base64Coded = window.btoa(plainText);
console.log("Base64 coded text: ", base64Coded);

var encrypted = encryptDesCbcPkcs7Padding(base64Coded, "12345678");
console.log("Encrypted: ", encrypted);
var finalEncrypted = CryptoJS.enc.Base64.stringify(encrypted.ciphertext);
console.log("Final encrypted: ", finalEncrypted);
alert("Final encrypted: " + finalEncrypted);

使用这段代码,我得到了用前面的步骤加密的信息。

问题:我无法进行反向步骤。我做错了什么?

这些是我用于反向的代码:

function decryptDesCbcPkcs7Padding(message, key) {
    var keyHex = CryptoJS.enc.Utf8.parse(key);
    var iv = new Uint8Array(0);
    var ivHex = CryptoJS.enc.Hex.parse(CryptoJS.enc.Utf8.parse(iv).toString(CryptoJS.enc.Hex));

    var decrypted = CryptoJS.DES.decrypt(message, keyHex, { iv: ivHex, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 });


    return decrypted.toString(CryptoJS.enc.Utf8);
}

var base64Decoded = CryptoJS.enc.Base64.parse(finalEncrypted).toString();
console.log("Base64 decoded", base64Decoded);

var decrypted = decryptDesCbcPkcs7Padding(base64Decoded, "12345678");
console.log("Decrypted: ", decrypted);

var finalDecrypted = CryptoJS.enc.Base64.stringify(encrypted.ciphertext);
console.log("Final encrypted: ", finalEncrypted);
alert("Final encrypted: " + finalEncrypted);

我无法使这段代码工作,我做错了什么?

除了复制粘贴错误外,您的代码中还有许多编码问题。这是固定代码。

function encryptDesCbcPkcs7Padding(message, key) {
    var keyWords = CryptoJS.enc.Utf8.parse(key);
    var ivWords = CryptoJS.lib.WordArray.create([0, 0]);
    var encrypted = CryptoJS.DES.encrypt(message, keyWords, { iv: ivWords});
  
    return encrypted;//.toString(CryptoJS.enc.Utf8);
}

var plainText = "hola";
console.log("Plain text: ", plainText);
var base64Coded = window.btoa(plainText);
console.log("Base64 coded text: ", base64Coded);

var encrypted = encryptDesCbcPkcs7Padding(base64Coded, "12345678");
console.log("Encrypted: ", encrypted);
var finalEncrypted = CryptoJS.enc.Base64.stringify(encrypted.ciphertext);
console.log("Final encrypted: ", finalEncrypted);
alert("Final encrypted: " + finalEncrypted);

function decryptDesCbcPkcs7Padding(message, key) {
    var keyWords = CryptoJS.enc.Utf8.parse(key);
    var ivWords = CryptoJS.lib.WordArray.create([0, 0]);

    var decrypted = CryptoJS.DES.decrypt({ciphertext: message}, keyWords, { iv: ivWords });

    return decrypted.toString(CryptoJS.enc.Utf8);
}

var base64Decoded = CryptoJS.enc.Base64.parse(finalEncrypted);
console.log("Base64 decoded", base64Decoded);

var decrypted = decryptDesCbcPkcs7Padding(base64Decoded, "12345678");
console.log("Decrypted: ", decrypted);

var finalDecrypted = CryptoJS.enc.Base64.parse(decrypted.toString(CryptoJS.enc.Utf8)).toString(CryptoJS.enc.Utf8);
console.log("Final decrypted: ", finalDecrypted);
alert("Final decrypted: " + finalDecrypted);
<script src="https://cdn.rawgit.com/CryptoStore/crypto-js/3.1.2/build/rollups/tripledes.js"></script>