1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
def cesaer_shift(message, shift = 1)
  #set up our A to Z alphabet
  alpha = ("A".."Z").to_a
  #get our secert message and split into an array
  plain_text = message.upcase.split('')
  #create our array for the encrypted text
  encrypted_text = []
  
  plain_text.each do |a|
    index = alpha.index(a) #get the index number for this letter, a = 1, etc
    
    #strip out any non alphanumeric
    if index.nil?
      next
    end
    
    new_index = index + shift
    if new_index > alpha.length - 1
      #subtract length to account for large shifts or shifts at the end of the alphabet
      while new_index > alpha.length - 1
        new_index -= alpha.length
      end
    elsif new_index < 0
      #add length to account for backwards shifts
      while new_index > alpha.length
        new_index += alpha.length
      end
    end
    
    encrypted_text << alpha[new_index]
  end
  
  puts "ENCRYPTED  : " + encrypted_text.join
  puts "UNENCRYPTED: " + plain_text.reject {|a| !alpha.include? a }.join
end

puts cesaer_shift("MEET ME UNDER THE BRIDGE AT NOON", 8)
ruby 2.5.0p0 (2017-12-25 revision 61468) [x86_64-linux]