Simple Ruby Encryption & Decryption

Occasionally I run across the need to efficiently encrypt and decrypt small messages that get sent over public media. Sure, I could use SSL, but for simple situations, I don’t need such a big hammer. What I need is a way to take a message like, “I’m leaving the key under the doormat” and tuck it away in a message that otherwise does not need security.

Below is a class called Shencrypt, with two simple methods. To encrypt a message, just put it into the argument for self.encrypt, and it will provide an output hash that contains the encrypted message as well as the IV (Initialization Vector) that helps protect against analysis by the bad guys. Since this uses symmetric encryption, the receiver has to have access to the same key. This can be solved in various ways; perhaps the most obvious would be to use an MD5 hash of the login password as the key.
Continue reading