brix / crypto-js

JavaScript library of crypto standards.
Other
15.88k stars 2.39k forks source link

How to encrypt decrypt file #363

Open rupamking1 opened 3 years ago

rupamking1 commented 3 years ago

How to encrypt decrypt file pls write documentation encrypt decrypt file

polypixeldev commented 3 years ago

Can you clarify, please? If the file is text formatted (ex. utf8) you can get a string of the file with fs.readFileSync, and then pass that into your preferred cipher (ex. AES).

For example (assuming test.txt is utf-8 encoded):

const passphrase = 'secret key';
const string = fs.readFileSync('test.txt', {encoding: 'utf-8'});
const encryptedString = cryptoJS.AES.encrypt(string, passphrase);

You could then decrypt it like this:

let decryptedString = cryptoJS.AES.decrypt(encryptedString, passphrase).toString(cryptoJS.enc.Utf8);