jon colins
jon colins

Reputation: 453

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?

Upvotes: 13

Views: 10795

Answers (4)

alex.zherdev
alex.zherdev

Reputation: 24174

If you're not concerned about security you can just base64-encode your string:

encoded = Base64::encode(string)
decoded = Base64::decode(encoded)

By the way it's also suitable for encoding binary data.

This isn't really encrypting as any developer may even guess that its Base64 encoded data.

Upvotes: -25

Sean Huber
Sean Huber

Reputation: 640

gem install encryptor

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

http://github.com/shuber/encryptor

Upvotes: 16

Alex Mcp
Alex Mcp

Reputation: 19315

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.

Upvotes: 15

John Topley
John Topley

Reputation: 115412

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

Upvotes: 20

Related Questions