Reputation: 793
i am currently using nodejs as my backend, sequelize ORM and postgres as my db.
When my user is signing up, i am trying to encrypt the data using the builtin crypto module.
everything is working but since i am generating a custom IV, the encrypted data all take the same IV since it is rendered every time node restarts. How do i give each field a different IV?
This is my first time encrypting data, can someone tell me if what i am doing is correct or not?
let key ="12345678123456781234567812345678";
let iv = crypto.randomBytes(16);
router.post(
"/register",
(req, res) => {
let cipher1 = crypto.createCipheriv('aes-256-cbc', Buffer.from(key), iv);
let cipher2 = crypto.createCipheriv('aes-256-cbc', Buffer.from(key), iv);
let cipher3 = crypto.createCipheriv('aes-256-cbc', Buffer.from(key), iv);
let cipher4 = crypto.createCipheriv('aes-256-cbc', Buffer.from(key), iv);
let cipher5 = crypto.createCipheriv('aes-256-cbc', Buffer.from(key), iv);
let mobilenumber = cipher1.update(req.body.mobilenumber);
const encrypted_mobilenumber = Buffer.concat([mobilenumber, cipher1.final()]);
let firstname = cipher2.update(req.body.firstname);
const encrypted_firstname = Buffer.concat([firstname, cipher2.final()]);
let lastname = cipher3.update(req.body.lastname);
const encrypted_lastname = Buffer.concat([lastname, cipher3.final()]);
let dob = cipher4.update(req.body.dob);
const encrypted_dob = Buffer.concat([dob, cipher4.final()]);
const fullAddress= req.body.housenumber + ', ' + req.body.address1 +
(req.body.address2===''?'': ', ' + req.body.address2 ) +
', ' + req.body.city + ', ' + req.body.postcode + ', ' + req.body.country
let address = cipher5.update(fullAddress);
const encrypted_address = Buffer.concat([address, cipher5.final()]);
User.create({
email: req.body.email,
mobilenumber:iv.toString('hex') + ':' + encrypted_mobilenumber.toString('hex'),
passcode: req.body.passcode,
firstname:iv.toString('hex') + ':' + encrypted_firstname.toString('hex'),
lastname:iv.toString('hex') + ':' + encrypted_lastname.toString('hex'),
dob:iv.toString('hex') + ':' + encrypted_dob.toString('hex'),
address:iv.toString('hex') + ':' + encrypted_address.toString('hex')
})
Upvotes: 0
Views: 2262
Reputation: 30685
I think we can simplify this code by introducing a new function, encryptField(), that will encrypt a given field with the key provided and prepend the iv to it before returning.
I would also suggest creating a getFullAddress function to turn address components into the full address.
All of this should significantly reduce code length and duplication:
const key = "12345678123456781234567812345678";
function encryptField(data, key) {
const iv = crypto.randomBytes(16);
const cipher = crypto.createCipheriv('aes-256-cbc', Buffer.from(key), iv);
return iv.toString('base64') + ':' + Buffer.concat([cipher.update(data),cipher.final()]).toString("base64");
}
function getFullAddress({housenumber, address1, address2, city, postcode, country}) {
return [housenumber, address1, ...(address2 ? [address2]: []), city, postcode, country].join(", ");
}
router.post(
"/register",
(req, res) => {
User.create({
email: encryptField(req.body.email, key),
mobilenumber: encryptField(req.body.mobilenumber, key),
passcode: req.body.passcode,
firstname: encryptField(req.body.firstname, key),
lastname: encryptField(req.body.lastname, key),
dob: encryptField(req.body.dob, key),
address: encryptField(getFullAddress(req.body), key)
})
}
)
Upvotes: 1