-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathcaesar.rb
More file actions
30 lines (24 loc) · 827 Bytes
/
caesar.rb
File metadata and controls
30 lines (24 loc) · 827 Bytes
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
# Caesar Cipher
# Write a function to encrypt a message (by shifting char code up or down). Write a function to decrypt a message
#
# Useful methods:
# - http://ruby-doc.org/core-2.2.0/String.html#method-i-ord
# - http://ruby-doc.org/core-2.0.0/String.html#method-i-chr
OFFSET = 3
def encrypt(message)
encrypted = ""
message.each_char {|c| encrypted << (c.ord+OFFSET).chr }
encrypted
end
def decrypt(message)
decrypted = ""
message.each_char {|c| decrypted << (c.ord-OFFSET).chr }
decrypted
end
# http://blog.steveklabnik.com/posts/2011-08-19-matz-is-nice-so-we-are-nice
message = "Matz is Nice So We Are Nice"
puts "Original msg: #{message}"
messageEncrypted = encrypt(message)
puts "Encrypted msg: #{messageEncrypted}"
messageDecrypted = decrypt(messageEncrypted)
puts "Decrypted msg: #{messageDecrypted}"