Created
January 10, 2015 00:15
-
-
Save awhit012/445e9e0991646645ffed to your computer and use it in GitHub Desktop.
Ruby implementation of the old school Rail Fence Cypher, encrypts and decrypts using 2, 3 or 4 rows.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Rail_Fence_Cipher | |
def initialize instruction, number, text | |
@letter_array = text.split("") | |
@text_length = text.length | |
@number = number | |
case instruction | |
when "encrypt" | |
encrypt | |
when "decrypt" | |
decrypt | |
else raise "Invalid instruction" | |
end | |
end | |
def encrypt | |
case @number | |
when 2 | |
encrypt_2 | |
when 3 | |
encrypt_3 | |
when 4 | |
encrypt_4 | |
else | |
raise "Not a valid encryption code" | |
end | |
end | |
def encrypt_2 | |
first, second = [], [] | |
(0..@text_length).each do |index| | |
if index % 2 == 0 | |
first << @letter_array[index] | |
else | |
second << @letter_array[index] | |
end | |
end | |
puts (first + second).join("") | |
end | |
def encrypt_3 | |
first, second, third = [], [], [] | |
(0..@text_length).each do |index| | |
if index % 4 == 0 | |
first << @letter_array[index] | |
elsif index % 2 == 1 | |
second << @letter_array[index] | |
else | |
third << @letter_array[index] | |
end | |
end | |
puts (first + second + third).join("") | |
end | |
def encrypt_4 | |
first, second, third, fourth = [], [], [], [] | |
(0..@text_length).each do |index| | |
if index % 6 == 0 | |
first << @letter_array[index] | |
elsif index % 2 == 1 && index % 3 != 0 | |
second << @letter_array[index] | |
elsif index % 2 == 0 && index % 3 !=0 | |
third << @letter_array[index] | |
else | |
fourth << @letter_array[index] | |
end | |
end | |
puts (first + second + third + fourth).join("") | |
end | |
def decrypt | |
case @number | |
when 2 | |
decrypt_2 | |
when 3 | |
decrypt_3 | |
when 4 | |
decrypt_4 | |
# when 5 | |
# decrypt_5 | |
else | |
raise "Not a valid encryption code" | |
end | |
end | |
def decrypt_2 | |
zipper = @letter_array.each_slice(@text_length/2).to_a | |
puts zipper[0].zip(zipper[1]).flatten.join("") | |
end | |
def decrypt_3 | |
zipper = @letter_array.each_slice(@text_length/4).to_a | |
puts zipper[0].zip(zipper[3]).flatten.zip(zipper[1] + zipper[2]).flatten.join("") | |
end | |
def decrypt_4 | |
zipper = @letter_array.each_slice(@text_length/6).to_a | |
first_and_last = zipper[0].zip(zipper[5]).flatten | |
space_holders = [] | |
first_and_last.length.times do | |
space_holders << 0 | |
end | |
new_zipper = first_and_last.zip(space_holders).flatten | |
middles = (zipper[1] + zipper[2]).zip(zipper[3] + zipper[4]) | |
middles.each_with_index{|small_array, index| small_array.reverse! if index % 2 == 1} | |
middles.flatten! | |
solution = new_zipper.zip(middles).flatten | |
solution.delete(0) | |
puts solution.join("") | |
end | |
end | |
Rail_Fence_Cipher.new "decrypt", 4, "rclretoiygardimapomeddrm" | |
Rail_Fence_Cipher.new "encrypt", 4, "redditcomdailyprogrammer" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment