我正在加密先前在base64中编码的内容。
步骤如下:
1.对base64中的普通(可读)文本进行编码
2.使用CryptoJS加密带有DES / CBC / PKCS7PADDING的base64编码文本
3.在base64中再次编码步骤2中获得的信息
这是我在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);
我无法使这段代码有效,我做错了什么?
答案 0 :(得分:1)
除了复制粘贴错误之外,代码中的编码还存在许多问题。这是固定代码。
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>
&#13;