31 lines
1.1 KiB
JavaScript
31 lines
1.1 KiB
JavaScript
const crypto = require('crypto');
|
|
const path = require('path');
|
|
require('dotenv').config({ path: path.join(__dirname, '../.env') });
|
|
|
|
const ALGORITHM = 'aes-256-cbc';
|
|
const SECRET_KEY = crypto.createHash('sha256').update(process.env.CRYPTO_KEY || 'default_secret_key_12345').digest();
|
|
|
|
const encryptedText = process.argv[2];
|
|
if (!encryptedText) {
|
|
console.log('Utilizzo: node decrypt.js "testo_criptato:valore"');
|
|
process.exit(1);
|
|
}
|
|
|
|
try {
|
|
const textParts = encryptedText.split(':');
|
|
if (textParts.length < 2) {
|
|
throw new Error('Formato testo cifrato non valido (manca il separatore ":")');
|
|
}
|
|
const iv = Buffer.from(textParts.shift(), 'hex');
|
|
const encrypted = Buffer.from(textParts.join(':'), 'hex');
|
|
const decipher = crypto.createDecipheriv(ALGORITHM, SECRET_KEY, iv);
|
|
let decrypted = decipher.update(encrypted, 'hex', 'utf8');
|
|
decrypted += decipher.final('utf8');
|
|
|
|
console.log('Testo Criptato: ', encryptedText);
|
|
console.log('Testo Decriptato:', decrypted);
|
|
} catch (err) {
|
|
console.error('Errore durante la decriptazione:', err.message);
|
|
process.exit(1);
|
|
}
|