const CryptoJS = require('crypto-js');
function decryptFromRust(encryptedBase64, password) {
console.log('Base64 input:', encryptedBase64);
const encryptedBytes = Buffer.from(encryptedBase64, 'base64');
console.log('Decoded bytes length:', encryptedBytes.length);
const iv = encryptedBytes.slice(0, 16);
const ciphertext = encryptedBytes.slice(16);
console.log('IV length:', iv.length);
console.log('IV bytes:', Buffer.from(iv).toString('hex'));
console.log('Ciphertext length:', ciphertext.length);
const ciphertextWA = CryptoJS.lib.WordArray.create(ciphertext);
const ivWA = CryptoJS.lib.WordArray.create(iv);
const keyBytes = Buffer.alloc(32); Buffer.from(password).copy(keyBytes, 0, 0, 32); console.log('Key length:', keyBytes.length);
console.log('Key bytes:', keyBytes.toString('hex'));
const key = CryptoJS.lib.WordArray.create(keyBytes);
try {
const decrypted = CryptoJS.AES.decrypt(
{
ciphertext: ciphertextWA
},
key,
{
iv: ivWA,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7
}
);
return decrypted.toString(CryptoJS.enc.Utf8);
} catch (error) {
console.error('Decryption error:', error);
return '';
}
}
const fs = require('fs');
const encrypted = fs.readFileSync('test_encrypted.txt', 'utf8').trim(); const password = "12345678901234567890123456789012";
const decrypted = decryptFromRust(encrypted, password);
console.log("Decrypted:", decrypted);