File size: 1,888 Bytes
78c3e1e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | const crypto = require('crypto');
function deriveKeyAndIv(password, salt) {
const passwordBuf = Buffer.from(password, 'utf8');
let key = Buffer.alloc(0);
let iv = Buffer.alloc(0);
let lastHash = Buffer.alloc(0);
while (key.length + iv.length < 48) {
const hash = crypto.createHash('md5');
hash.update(lastHash);
hash.update(passwordBuf);
hash.update(salt);
lastHash = hash.digest();
let remaining = lastHash;
if (key.length < 32) {
const keyToCopy = Math.min(32 - key.length, remaining.length);
key = Buffer.concat([key, remaining.slice(0, keyToCopy)]);
remaining = remaining.slice(keyToCopy);
}
if (remaining.length > 0 && iv.length < 16) {
const ivToCopy = Math.min(16 - iv.length, remaining.length);
iv = Buffer.concat([iv, remaining.slice(0, ivToCopy)]);
}
}
return { key, iv };
}
function decrypt(ciphertext, password) {
try {
const buffer = Buffer.from(ciphertext, 'base64');
if (buffer.slice(0, 8).toString('utf8') !== 'Salted__') {
console.log('Invalid prefix');
return null;
}
const salt = buffer.slice(8, 16);
const encryptedData = buffer.slice(16);
const keyIv = deriveKeyAndIv(password, salt);
const decipher = crypto.createDecipheriv('aes-256-cbc', keyIv.key, keyIv.iv);
let decrypted = decipher.update(encryptedData);
decrypted = Buffer.concat([decrypted, decipher.final()]);
return decrypted.toString('utf8');
} catch (e) {
console.log('Error:', e.message);
return null;
}
}
const key = "jb%!SZfM%AwwS7JmdM!";
const raw = "U2FsdGVkX19FssG+jADYfE/OYAT3jc+GMe+0/PII+aocWAvEBLte2kN5BTeLZ5+Xk3Tuo/H+6tSqMn49Juxdsg==";
console.log('Decrypted:', decrypt(raw, key));
|