Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby/Rails: How to get same encrypted value every time we encrypt a particular string

Does ActiveSupport::MessageEncryptor support deterministic encryption so that we get the same encrypted value every time we encrypt a particular string? If not, are there any other Ruby libs that support deterministic encryption?

My goal is to get same encrypted value every time I encrypt a string and I should be able to decrypt it to original value as well.

Thanks.

like image 643
Saim Avatar asked Jan 29 '23 14:01

Saim


1 Answers

You get different crypts because ActiveSupport::MessageEncryptor uses OpenSSL for encryption which requires an iv by default to prevent attackers from inferring relationships between segments of the encrypted message. I would highly recommend you to not mess around with that because you open ways for attackers to infer the encryption key.

However if you still want to do that take a look into the OpenSSL documentation of ruby. There should be a way to encrypt without vector.

Because it's a high security risk I don't add code to the answer to protect others from unnecessary loop holes.

like image 189
Tobias Avatar answered Feb 02 '23 10:02

Tobias