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 salt = encryptedBytes.subarray(0, 16);
const iv = encryptedBytes.subarray(16, 32);
const ciphertext = encryptedBytes.subarray(32);
console.log('Salt length:', salt.length);
console.log('Salt bytes:', Buffer.from(salt).toString('hex'));
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 saltWA = CryptoJS.lib.WordArray.create(salt);
const key = CryptoJS.PBKDF2(password, saltWA, {
keySize: 256/32,
iterations: 10000,
hasher: CryptoJS.algo.SHA256
});
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 = "my secret password";
const decrypted = decryptFromRust(encrypted, password);
console.log("Decrypted:", decrypted);