Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
250 changes: 174 additions & 76 deletions EncryptDecrypt-v2.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,51 +16,58 @@ let privateKeyPath = './keys/privateKey.key'
// Calling generateKeyPair() method
// with its parameters
exports.generateKeys = () => {
generateKeyPair('rsa', {
modulusLength: 2048, // options
publicExponent: 0x10101,
publicKeyEncoding: {
type: 'pkcs1',
format: 'pem'
},
privateKeyEncoding: {
type: 'pkcs8',
format: 'pem',
cipher: 'aes-192-cbc',
passphrase: 'sse'
}
}, (err, PublicKey, PrivateKey) => { // Callback function
if(!err)
{
fs.writeFile(publicKeyPath, PublicKey.toString('hex'), (er) => {
if (er) console.log(er);
console.log("Key Successfully Saved.");
});

fs.writeFile(privateKeyPath, PrivateKey.toString('hex'), (er) => {
if (er) console.log(er);
console.log("Key Successfully Saved.");
});

}
else
{
// Prints error if any
console.log("Errr is: ", err);
}
});

return new Promise( (resolve, reject) => {

generateKeyPair('rsa', {
modulusLength: 2048, // options
publicExponent: 0x10101,
publicKeyEncoding: {
type: 'pkcs1',
format: 'pem'
},
privateKeyEncoding: {
type: 'pkcs8',
format: 'pem',
cipher: 'aes-192-cbc',
passphrase: 'sse'
}
}, (err, PublicKey, PrivateKey) => { // Callback function
if(!err)
{
fs.writeFile(publicKeyPath, PublicKey.toString('hex'), (er) => {
if (er) console.log(er);
console.log("Key Successfully Saved.");
});

fs.writeFile(privateKeyPath, PrivateKey.toString('hex'), (er) => {
if (er) console.log(er);
console.log("Key Successfully Saved.");
});
resolve();

}
else
{
// Prints error if any
console.log("Errr is: ", err);
reject();
}
});

})

};


exports.getKeywordHash = (keyword) => {
keyword_hash = crypto.createHmac('sha256', indexKey)
let keyword_hash = crypto.createHmac('sha256', indexKey)
.update(keyword)
.digest('hex')
return keyword_hash
};

exports.getEncryptionKeyword = (pbKey, keyword) => { //send publickey as a parameter
//Store publicKey.key as key file
exports.getEncryptionKeyword = (pbKey, keyword) => {
//Also pass publicKey.key the file here as a parameter
//let pbKey = fs.readFileSync('./keys/publicKey.key');

Expand Down Expand Up @@ -96,10 +103,10 @@ exports.getDecryptionKeyword = (encryptedKeyword) => {
return keyword.toString();
};

exports.getEncryptFile = (/* pbKey, */filePath) => {
exports.getEncryptFile = (pbKey, filePath) => {
//Store publicKey.key as key file
//Also pass publicKey.key the file here as a parameter
let pbKey = fs.readFileSync('./keys/publicKey.key');
//let pbKey = fs.readFileSync('./keys/publicKey.key');

fs.readFile(filePath, "utf-8", (err, data) => {

Expand All @@ -121,56 +128,147 @@ exports.getEncryptFile = (/* pbKey, */filePath) => {
});
};

exports.getEncryptFileV2 = (pbKey, filePath) => {
//Store publicKey.key as key file
//Also pass publicKey.key the file here as a parameter
//let pbKey = fs.readFileSync('./keys/publicKey.key');

fs.readFile(filePath, "utf-8", (err, data) => {
//It is for encrypting owner's file data with requester's public key -also output is in HEX
exports.getEncryptFileV2 = (pbKey, filePath ) => {
return new Promise( (resolve) => {

const encryptedData = crypto.publicEncrypt(
{
key: pbKey,
padding: crypto.constants.RSA_PKCS1_OAEP_PADDING,
oaepHash: "sha256",
},
//Store publicKey.key as key file
//Also pass publicKey.key the file here as a parameter
//let pbKey = fs.readFileSync('./keys/publicKey.key');
let againEncryptedfileP = './temp-file/againEncryptFile.txt';
console.log("Inside encryptFileV2: ");
console.log("file data: ", fs.readFileSync(filePath).toString('hex'));
fs.readFile(filePath, "utf-8" , (err, data) => {

Buffer.from(data)
)

fs.writeFile(filePath, encryptedData, (error) => {
if (error) console.log(error);
//console.log(encryptedData);
console.log("File Successfully Encrypted.");

const encryptedData = crypto.publicEncrypt(
{
key: pbKey,
padding: crypto.constants.RSA_PKCS1_OAEP_PADDING,
oaepHash: "sha256",
},

Buffer.from(data)
)

fs.writeFile(againEncryptedfileP, encryptedData.toString('hex'), (error) => {
if (error) console.log(error);
console.log("File Successfully Encrypted.");
resolve(againEncryptedfileP);
});
//resolve(againEncryptedfileP);
});
});
};

exports.getDecryptFile = (filePath) => {
let prKey = fs.readFileSync('./keys/privateKey.key');
const plainDataFilePath = './temp-file/decryptedFile.txt';
return new Promise( (resolve) => {

const plainDataFileP = './temp-file/decryptedFile.txt';
//let prKey = fs.readFileSync('./keys/privateKey.key'); //toString??
fs.readFile('./keys/privateKey.key', 'utf8', (err, prKey)=> {

console.log('In the getDecryptFile(). ');
console.log(filePath);

const tempPath = './public/files/'+ filePath;

fs.readFile(tempPath, (err, encryptedData) => {

const decryptedData = crypto.privateDecrypt(
{
key: prKey,
passphrase: 'sse',
padding: crypto.constants.RSA_PKCS8_OAEP_PADDING,
oaepHash: "sha256",
},
encryptedData
)
fs.writeFile(plainDataFileP, decryptedData , (error) => {
console.log('In the getDecryptFile(). writeFile ');
if (error) console.log(error);
console.log(decryptedData);
console.log("Successfully decrypted.");
resolve(plainDataFileP);
});
//resolve(plainDataFileP);
});
});
});
//return plainDataFilePath;
};

exports.getDecryptFileContent = (fileContent) => {
return new Promise((resolve)=> {
console.log("File Content: "+ fileContent);
fs.readFile('./keys/privateKey.key', 'utf8', (err, prKey) => {
console.log('prKey = ', prKey, typeof(prKey));

const plainData = crypto.privateDecrypt(
{
key: prKey,
passphrase: 'sse',
padding: crypto.constants.RSA_PKCS8_OAEP_PADDING,
oaepHash: "sha256",
},
Buffer.from(fileContent,'hex')
)

//console.log("decrypted data: ", keyword.toString());
resolve(plainData.toString() );
})
});
};

console.log(filePath);
console.log(fs.readFileSync('./public/files/'+filePath)); //its wrong completely
const tempPath = './public/files/'+ filePath;

fs.readFile(tempPath, (err, encryptedData) => {
const decryptedData = crypto.privateDecrypt(
exports.generateNonce = () => {
return new Promise((resolve)=> {

crypto.randomInt(0, 1000000, (err, n) => {
if (err) throw err;

resolve(n.toString());
})
});
};

exports.getEncryptNonce = (pbKey, plainNonce) => {
return new Promise( (resolve) => {
console.log("At Nonce Encrypt: plainNonce: " + plainNonce +" tpy: "+ typeof(plainNonce));

const encryptedNonce = crypto.publicEncrypt(
{
key: prKey,
passphrase: 'sse',
padding: crypto.constants.RSA_PKCS8_OAEP_PADDING,
key: pbKey,
padding: crypto.constants.RSA_PKCS1_OAEP_PADDING,
oaepHash: "sha256",
},
encryptedData

Buffer.from(plainNonce)
)
fs.writeFile(plainDataFilePath, decryptedData , (error) => {
if (error) console.log(error);
console.log(decryptedData);
console.log("Successfully decrypted.");
});

});

return plainDataFilePath;
console.log("At Nonce Encrypt: encryptNonce: ", encryptedNonce.toString('hex') + " typ: " + typeof(encryptedNonce.toString('hex')));
resolve(encryptedNonce.toString('hex'));
});
};

exports.getDecryptNonce = (encryptedNonce) => {
return new Promise( (resolve) => {
console.log("At Nonce decrypt: encryptedNonce: " + encryptedNonce +" tpy: "+ typeof(encryptedNonce));
console.log("At Nonce decrypt: encryptedNonce: buffer convert " + Buffer.from(encryptedNonce, 'hex') +" tpy: "+ typeof(Buffer.from(encryptedNonce, 'hex')));

fs.readFile('./keys/privateKey.key', 'utf8', (err, prKey)=> {

const decryptedNonce = crypto.privateDecrypt(
{
key: prKey,
passphrase: 'sse',
padding: crypto.constants.RSA_PKCS8_OAEP_PADDING,
oaepHash: "sha256",
},
Buffer.from(encryptedNonce, 'hex') //should use buffer???
)
console.log("At Nonce decrypt: decryptedNonce: " + decryptedNonce.toString() +" tpy: "+ typeof(decryptedNonce.toString()));
resolve(decryptedNonce.toString());
});
});
}
1 change: 1 addition & 0 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const User = require('./models/user');


const encDec = require('./EncryptDecrypt-v2');
const fs = require('fs');


const fileStorage = multer.diskStorage({
Expand Down
18 changes: 16 additions & 2 deletions controllers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ exports.postLogin = (req,res, next) => {
.catch(err => console.log(err));
}


exports.getSignup = (req,res, next) => {
let message = req.flash('error');
if (message.length > 0) {
Expand All @@ -113,6 +114,16 @@ exports.getSignup = (req,res, next) => {
});
}

// Make a function in index.js controller.
function fudai(){
return new Promise((resolve, reject) => {
fs.readFile('./keys/publicKey.key', 'utf8', (err, data)=> {
console.log('Inside fudai.');
resolve(data);
});
});
}

exports.postSignup = async (req,res, next) => {
const email = req.body.email;
const password = req.body.password;
Expand All @@ -138,9 +149,12 @@ exports.postSignup = async (req,res, next) => {
// If no error, encrypt the password, and save the user into database.
const hashedPassword = await bcrypt.hash(password, 12);

encDec.generateKeys();
await encDec.generateKeys();

let pbKey = await fudai();
//let pbKey = await encDec.generateKeys();

let pbKey = await fs.readFileSync('./keys/publicKey.key');
console.log(pbKey, typeof(pbKey) ); // This is buffer Object

const user = new User({
email: email,
Expand Down
Loading