Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ruby - how to encrypt text

I need to encrypt a string (from a text area) that will also be decrypted later on when it is displayed.

I'm not to concerned about it being majorly secure, but just don’t want to store the data in plain text format.

Does anyone have any suggestions on how to do this easily in Rails?

like image 986
jon colins Avatar asked Feb 03 '10 15:02

jon colins


3 Answers

There is a RubyGem named Crypt that provides a pure Ruby implementation of a number of encryption algorithms.

like image 180
John Topley Avatar answered Oct 17 '22 19:10

John Topley


gem install encryptor

It wraps the standard Ruby OpenSSL library and allows you to use any of its algorithms.

http://github.com/shuber/encryptor

like image 35
Sean Huber Avatar answered Oct 17 '22 21:10

Sean Huber


Is there a ROT13 implementation in Ruby/Rails (there must be...) that's totally insecure except to human readers (and idiot savants) so seems to fit your use case.

EDIT - This is a good start for swapping out characters:

$_.tr! "A-Za-z", "N-ZA-Mn-za-m";

It asks for user input then swaps the characters.

EDIT If you're not familiar, ROT13 assigns each letter its natural number. A=1, B=2, etc. Then it adds 13 to each number, effectively spinning it half way around the alphabet. The halfway bit is great, because unlike, say, ROT12, you can just run ROT13 again to decode. One function for both. OR you could run ROT12 13 times I guess (12 * 13 = 156. 156/26 = 6.) ROT 13 is better for this though.

like image 30
Alex Mcp Avatar answered Oct 17 '22 19:10

Alex Mcp