Created
July 5, 2023 17:32
-
-
Save heomeco2014/a8a0afaf8013aa59ae2f28611fd17c3c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const crypto = require('crypto'); | |
const fs = require('fs'); | |
// Phát sinh một khoá bí mật Ks của thuật toán AES | |
function generateAESSecretKey() { | |
return crypto.randomBytes(32); // 32 bytes = 256 bits | |
} | |
// Mã hoá tập tin sử dụng thuật toán AES với khoá Ks | |
function encryptFileAES(filePath, secretKey) { | |
const fileContent = fs.readFileSync(filePath); | |
const iv = crypto.randomBytes(16); // Initialization vector | |
const cipher = crypto.createCipheriv('aes-256-cbc', secretKey, iv); | |
const encryptedFileContent = Buffer.concat([cipher.update(fileContent), cipher.final()]); | |
return {iv, encryptedFileContent}; | |
} | |
// Giải mã tập tin sử dụng thuật toán AES với khoá Ks | |
function decryptFileAES(filePath, secretKey, iv) { | |
const encryptedFileContent = fs.readFileSync(filePath); | |
const decipher = crypto.createDecipheriv('aes-256-cbc', secretKey, iv); | |
const decryptedFileContent = Buffer.concat([decipher.update(encryptedFileContent), decipher.final()]); | |
return decryptedFileContent; | |
} | |
// Phát sinh một cặp khoá Kprivate và Kpublic của thuật toán RSA | |
function generateRSAKeyPair() { | |
const {publicKey, privateKey} = crypto.generateKeyPairSync('rsa', { | |
modulusLength: 2048, | |
publicKeyEncoding: {type: 'spki', format: 'pem'}, | |
privateKeyEncoding: {type: 'pkcs8', format: 'pem'}, | |
}); | |
return {privateKey, publicKey}; | |
} | |
// Mã hoá một chuỗi sử dụng thuật toán RSA sử dụng khoá Kpublic | |
function encryptStringRSA(plaintext, publicKey) { | |
const encryptedText = crypto.publicEncrypt(publicKey, Buffer.from(plaintext)).toString('base64'); | |
return encryptedText; | |
} | |
// Giải mã một chuỗi sử dụng thuật toán RSA sử dụng khoá Kprivate | |
function decryptStringRSA(ciphertext, privateKey) { | |
const decryptedText = crypto.privateDecrypt(privateKey, Buffer.from(ciphertext, 'base64')).toString(); | |
return decryptedText; | |
} | |
// Tính giá trị hash của một chuỗi sử dụng thuật toán SHA-1 | |
function computeSHA1Hash(input) { | |
const hash = crypto.createHash('sha1').update(input).digest('hex'); | |
return hash; | |
} | |
// Tính giá trị hash của một chuỗi sử dụng thuật toán SHA-256 | |
function computeSHA256Hash(input) { | |
const hash = crypto.createHash('sha256').update(input).digest('hex'); | |
return hash; | |
} | |
// Ứng dụng sử dụng các chức năng của module | |
// Mã hoá một tập tin theo các bước | |
function encryptFile(filePath) { | |
const secretKey = generateAESSecretKey(); | |
const {iv, encryptedFileContent} = encryptFileAES(filePath, secretKey); | |
const {publicKey, privateKey} = generateRSAKeyPair(); | |
const encryptedKey = encryptStringRSA(secretKey.toString('base64'), publicKey); | |
const privateKeyHash = computeSHA1Hash(privateKey); | |
const metadata = { | |
encryptedKey, | |
privateKeyHash, | |
}; | |
const metadataFilePath = filePath + '.metadata'; | |
fs.writeFileSync(metadataFilePath, JSON.stringify(metadata)); | |
console.log('Mã hoá tập tin thành công!'); | |
console.log('Khoá bí mật (đã mã hoá):', encryptedKey); | |
console.log('Khoá riêng tư (đã băm):', privateKeyHash); | |
} | |
function generateAndSaveRSAKeyPair() { | |
const {privateKey, publicKey} = generateRSAKeyPair(); | |
fs.writeFileSync('privateKey.pem', privateKey); | |
return publicKey; | |
} | |
// Giải mã một tập tin theo các bước | |
function decryptFile(filePath) { | |
const metadataFilePath = filePath + '.metadata'; | |
const metadata = JSON.parse(fs.readFileSync(metadataFilePath)); | |
const encryptedKey = metadata.encryptedKey; | |
const privateKeyHash = metadata.privateKeyHash; | |
const privateKey = fs.readFileSync('privateKey.pem'); // Đọc khoá riêng tư từ file | |
const computedHash = computeSHA1Hash(privateKey); | |
if (computedHash !== privateKeyHash) { | |
console.log('Comhash: ', computedHash, 'private', privateKey); | |
console.log('Giải mã thất bại. Không trùng khớp với khoá riêng tư đã lưu.'); | |
return; | |
} | |
const secretKey = decryptStringRSA(encryptedKey, privateKey); | |
const iv = crypto.randomBytes(16); // Đọc iv từ metadata nếu đã lưu trước đó | |
const decryptedFileContent = decryptFileAES(filePath, Buffer.from(secretKey, 'base64'), iv); | |
const decryptedFilePath = 'decrypted_' + filePath; | |
fs.writeFileSync(decryptedFilePath, decryptedFileContent); | |
console.log('Giải mã tập tin thành công!'); | |
console.log('Tập tin giải mã:', decryptedFilePath); | |
} | |
// Sử dụng ứng dụng | |
const filePath = 'file'; // Đường dẫn tới tập tin cần mã hoá hoặc giải mã | |
// Mã hoá tập tin | |
console.log('*** Mã hoá tập tin ***'); | |
const publicKey = generateAndSaveRSAKeyPair(); // Phát sinh cặp khoá RSA và lưu khoá riêng tư vào file | |
encryptFile(filePath); | |
console.log('---------------------'); | |
// Giải mã tập tin | |
console.log('*** Giải mã tập tin ***'); | |
decryptFile(filePath); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment