Skip to content

Instantly share code, notes, and snippets.

@mwksl
Created September 26, 2014 17:18
Show Gist options
  • Save mwksl/730240fe9e6310870292 to your computer and use it in GitHub Desktop.
Save mwksl/730240fe9e6310870292 to your computer and use it in GitHub Desktop.
# row_encrypt.rb
# declare variables
input = ""
key = ""
cipher = ""
# get the user input
# prompt for key
print "Please Enter the key you wish to use: \n"
key = gets.chomp
no_spaces_key = key.gsub(/\s+/, "") # remove whitespace by substitution
key_chars = no_spaces_key.split(//) # split into individual characters
# prompt for message
print "Please Enter the text you wish to encrypt: \n"
input = gets.chomp
no_spaces_message = input.gsub(/\s+/, "") # remove whitespace by substitution
message_chars = no_spaces_message.split(//) # split into individual characters
# define constants
KEYLENGTH = key_chars.length
MSGLENGTH = message_chars.length
# make sure we form an n x n matrix
if message_chars.length % key_chars.length != 0
# add x's until it's square
message_chars.push("x")
end
def encrypt(input_array)
p = 0
k = 0
temp = Array.new(KEYLENGTH) { Array.new(KEYLENGTH) }
for i in 0..input_array.length do
print input_array[i]
temp[p][k].map(input_array[i])
p = 0 if p == ((KEYLENGTH) -1)
k = k + 1 if p == ((KEYLENGTH) - 1)
p++ if p != ((KEYLENGTH)-1)
end # end if block
end # end for loop
end # end encrypt method
encrypt(message_chars)
print message_chars
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment