Skip to content
write a program to encrypt
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.



Write a program which can encrypt messages. Write a second program which can decrypt those messages.


Don't use Ruby's tr method.


The simplest two-way encryption algorithm is called ROT-13. It's a good choice to build up your solution.


Non-Object Based

If you're just getting started with Ruby and aren't familiar with creating classes and objects, create an encrypt.rb like this:

key = 13

message = "This is my secret"

# do your encryption here

puts encrypted_message

Then a decrypt.rb like:

key = 13

encrypted_message = "Guvf vf zl frperg"

# do your decryption here

puts message


If you're comfortable with creating classes, start with this:

class EncryptionEngine
  def encrypt(string)
    # Your code here
  def decrypt(string)
    # Your code here

Which gets used like this:

engine =
output = engine.encrypt("My Message")
puts output  # outputs "Zl Zrffntr"
output2 = engine.decrypt("Zl Zrffntr")
puts output2 # outputs "My Message"

And of course you'll be using TDD. Maybe you start with:

require 'minitest/autorun'

class EncryptionEngineTest < Minitest::Test
  def test_it_encrypts_using_rot13
    engine =
    output = engine.encrypt("My Message")
    assert_equal "Zl Zrffntr", output


Flexible Rotation

When you finish ROT-13, add the ability to specify the rotation number when encrypting or decrypting. So a user could, for example, choose to use ROT-6.

Shuffled Cipher

The next level up would be to create a shuffled cipher (the pattern used for encrypting/decrypting). Create your own cipher which maps each letter to an arbitrary other letter (like a mapping to x and b mapping to j). And create a reversed version for decrypting.

You can’t perform that action at this time.