Data Encryption In Ruby On Rails

Data security is the main concern in any application. Because we are storing the user’s personal data such as date of birth, mobile number, address, bank details and etc.

alok rawat
ITNEXT
2 min readNov 20, 2017

Click here to share this article on LinkedIn »

So you are at the right place to implement the data security functionality.
Here we are using the Ruby(4.2.5) and Postgresql(to store the user’s data).

There are multiple gems available in Ruby On Rails. But we are going to implement the “attr_encrypted” Gem.

Step1. Add data encryption gem
add a gem in GemFile.

Step2. Perform the bundle install

Step3. Generate a model

Step4. Set encrypted columns in migration file.

In the migration file, we need to encrypt birth_date and mobile_no. So, we have to add 「encrypted_」string before column name. For example, birth_date.

So, our migration file looks like,

iv column is used for more data security.

Step5. Setting in Model
In your model,

for environment variables setting, please check the following article.
http://qiita.com/alokrawat050/items/0d7791b3915579f95791

Step6. How to access encrypted fields in view or rails console
If you need to access encrypted fields in views or rails console then you just need to write the column name(no need to write the encrypted_).

In views,

In your controller, set the permit params,

In rails console,
rails c

1.data insert in user_details table.

In above example, you can see that when we are going to save data then it encrypted both the column’s data(birth_date and mobile_no).

2. data fetch from user_details table.

When we are fetching data then we need to call only column name(no need to write the encrypted_).

And you will get the data in decrypted form.

Enjoy Coding.

Thanks & Best Regards,
Alok Rawat

Originally published at qiita.com.

Sign up to discover human stories that deepen your understanding of the world.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Published in ITNEXT

ITNEXT is a platform for IT developers & software engineers to share knowledge, connect, collaborate, learn and experience next-gen technologies.

Written by alok rawat

Chief Engineer | Project Manager at NTT DATA INTELLILINK Corporation,Tech Enthusiast, Learner https://alokrawat050.github.io/alokrawat.github.io/

No responses yet

What are your thoughts?