I love Encryption/Decryption a Lot in Javascript ! And I have Already written article on JavaScript Crypto.JS Library and got many Responses/Doubts.And So recently I have Been working on a Small Node.JS project for File Management System in Cloud , I have Been using this File-Encryptor module for my Use.And i didn't see Any Successful Blog post guiding for this Encryption/Decryption File Module in Node.js.
Reference : Download Demo
var encryptor = require('file-encryptor');
var options = { algorithm : 'aes256' };
{
"name": "node-encryption",
"main": "server.js",
"version" : "0.0.1",
"dependencies" : {
"express" : "~3.4.4",
"file-encyptor" : "*",
}
}
Server.js:
Try out this simple node application by creating private directory in your project folder and create filename.txt and try encryption first and then followed by decryption.
var output = path.resolve(__dirname+'/../private/tmp/file.dat');
res.download(output); });
var key = 'My Secret Key';
var output = path.resolve(__dirname+'/../private/filename.txt');
var input = path.resolve(__dirname+'/../private/tmp/file.dat');
res.download(output);
}); //end of decryptor function
Working With File-Encryptor Node.js Module - Encrypt and Decrypt your Files OpenSSL |
Installation:
>npm install file-encryptor
Requirements:
- OpenSSL is required for doing our Encryption/Decryption Efficiently and Test It out in Localhost.And you can test it on Server if you have OpenSSL installed in it.Openshift Free Node.js Hosting Allows us to test it out.Click here to Learn how to Deploy Node.js in Openshift Redhat Hybrid Cloud.
- path module to resolve and access the files from your directory.
- Using Express Js framework create Routes to your Application .Learn here to create basic routes for application node.js
Procedure :
Require File encryptor and path for our Application.
var encryptor = require('file-encryptor');
var path = require("path");
Encrypting File :
Encrypting the File is easy,Just invoking the function lets you to Encrypt as DAT file in directory.Using the FileSystem Module we could normalize our directory
Encrypting the File is easy,Just invoking the function lets you to Encrypt as DAT file in directory.Using the FileSystem Module we could normalize our directory
var key = 'My KEY For Encryption';
var input = __dirname+'/../private/file.txt';
var output = __dirname+'/../private/filename.dat';
encryptor.encryptFile(input, output, key,options, function(err) {
if (err)
throw err;
console.log("Successfully Encrypted!");
});
var input = __dirname+'/../private/file.txt';
var output = __dirname+'/../private/filename.dat';
encryptor.encryptFile(input, output, key,options, function(err) {
if (err)
throw err;
console.log("Successfully Encrypted!");
});
Decrypting File :
var key = 'My KEY For Encryption';
var input = __dirname+'/../private/filename.dat';
var output = __dirname+'/../private/file.txt';
encryptor.decryptFile(input, output, key,options, function(err) {
if (err)
throw err;
console.log("Successfully Decrypted!");
});
var input = __dirname+'/../private/filename.dat';
var output = __dirname+'/../private/file.txt';
encryptor.decryptFile(input, output, key,options, function(err) {
if (err)
throw err;
console.log("Successfully Decrypted!");
});
Options :
Before trying Just Install OpenSSL in your System to and Make it Available via Commandline [Set Path]
Now Try openssl list it in your command Prompt and verify your Preferred Algorithm is installed.by default this module does aes192 if your not passing option as argument in function.
var options = { algorithm: 'Your Openssl Encryption Algorithm' };
var options = { algorithm : 'aes256' };
Basic Application :
Package.json
{
"name": "node-encryption",
"main": "server.js",
"version" : "0.0.1",
"dependencies" : {
"express" : "~3.4.4",
"file-encyptor" : "*",
}
}
Server.js:
Try out this simple node application by creating private directory in your project folder and create filename.txt and try encryption first and then followed by decryption.
var http = require("http");
var express = require("express");
var app = express();
var encryptor = require('file-encryptor');
var path = require("path");
app.get("/encrypt",function(req,res){
var key = 'My Secret Key';
var input = path.resolve(__dirname+'/../private/filename.txt');var output = path.resolve(__dirname+'/../private/tmp/file.dat');
var option = { algorithm : 'aes256' };
encryptor.encryptFile(input, output, key,options, function(err) {
res.download(output); });
});
app.get("/decrypt",function(req,res){
var output = path.resolve(__dirname+'/../private/filename.txt');
var input = path.resolve(__dirname+'/../private/tmp/file.dat');
var option = { algorithm : 'aes256' };
encryptor.decryptFile(input, output, key,options, function(err) {res.download(output);
}); //end of decryptor function
});
http.createServer(app).listen(3000);
console.log('Application Started now ');
For dynamic advanced application you can temporarily create filename unique manner and assign to create encrypted file in your directory.you can make use of tmp npm module.
try out with different algorithm for encryption/Decryption.I have used aes128/aes256/aes192 for my project.I am leaving in your hand for filename generations and applying it.
If you can find any errors/bugs/issues/suggections in my Demo/Post just comment below!lets resolve it.
For Errors/Comments and more details lets discuss below or mail me s.shivasurya@gmail.com or connect with me in Facebook/Twitter.Share is care.
0 comments:
Post a Comment