Skip to content

Instantly share code, notes, and snippets.

/ruby.rb Secret

Created September 21, 2015 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/fec2209650c81175c69a to your computer and use it in GitHub Desktop.
Save anonymous/fec2209650c81175c69a to your computer and use it in GitHub Desktop.
class Caesar
def initialize(shift, alphabet = %{ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/_\\=.%: })
i = shift % alphabet.size
@decrypt = alphabet
@encrypt = alphabet[i..-1] + alphabet[0...i]
end
def encrypt(string)
string.tr(@decrypt, @encrypt)
end
def decrypt(string)
string.tr(@encrypt, @decrypt)
end
end
alpha = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/', '_', '\\', '=', '.', '%', ':', ' '].join
#x = Caesar.new(5, %{ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/_\\=.%: })
x = Caesar.new(5, alpha)
puts alpha
puts x.encrypt(alpha)
###################################################################
$ ruby encode.rb
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/_\=.%:
FGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/_=.%: \ABCDE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment