Encrypt a message with ruby

Data security refer to the methodology for protecting data from an unauthorized access, and how can we do that? There are many ways to protect our data, and encryption is the most common way for protect our data. So, what is encryption?

What is encryption?

Encryption is the process of converting data into other format, then only people who have a secret key (formally called a decryption key) or password can decode the encrypted data into original information.

The encryption methods has long been used by military and governments for their secret communication. And till now, it commonly used for protecting information on many fields especially when we transfer data through the internet.

In this article I am going to pick two encryption methods as an example of message encryption ROT13, and XOR encryption.


  1. Definition

    Developed in ancient Rome, ROT13 ("rotate by 13 places", sometimes hyphenated ROT-13) convert original message into chipertext by replacing a letter with the 13 letters after it in the alphabet.


    Diagram of using ROT13 download.png

  2. Example:

    • Input: Why did the chicken cross the road?
    • Result Jul qvq gur puvpxra pebff gur ebnq?
  3. Implementation

   def rot13(secret_message)
      secret_message.bytes.map do|ch|
        if ch.chr >= 'a'  && ch.chr < 'n'
          ch += 13
        elsif ch.chr >= 'A' && ch.chr < 'N'
          ch += 13
        elsif ch.chr >= 'n' && ch.chr <= 'z'
          ch -= 13
        elsif ch.chr >= 'N' && ch.chr <= 'Z'
          ch -= 13

Let's run our method it in the terminal:

Screen Shot 2016-12-25 at 4.41.44 PM.png


  1. Definition

    XOR(Exclusive or or Exclusive disjunction) is a logical operation that outputs true only when inputs differ (one is true, the other is false). And we use symbol for represent XOR operation.


    Base on the true table, what is the value of A ⊕ B ⊕ A = ? As you can in the true table 0 ⊕ 0 = 0, 1 ⊕ 1 = 0

    => for any A: A ⊕ A = 0

    So: A ⊕ B ⊕ A = A ⊕ A ⊕ B = B

    Now let change our variable name to make it easy to understand.

    Message ⊕ Key = EncryptMessage

    EncryptMessage ⊕ key = Message

    Daigram of using XOR encryption download (1).png

  2. Implementation

    In ruby we can use ^ for XOR operation on number. So we need to open String class and implement our own XOR for string which convert the given message string and key to an Integer represent for those letters.(you can read more about method unpack from here), then we operate XOR on converted message with key.

    class String
      def ^(other)
        if other.empty?
          str1 = self.unpack("c*")
          str2 = other.unpack("c*")
          str2 *= str1.length/str2.length + 1
          str1.zip(str2).collect{|c1,c2| c1^c2}.pack("c*")

Let's run our code it in the terminal: Screen Shot 2016-12-25 at 9.26.02 PM.png

Last words

Encryption is a good way for protecting our data from unauthorized access. There are many methods for encrypting the data and many languages for implementation the encryption. However, even your data is encrypted, it doen't mean that your data is safe since the technology nowaday always go forward.

All Rights Reserved

Let's register a Viblo Account to get more interesting posts.