Skip to content

Instantly share code, notes, and snippets.

@everblut
Created November 4, 2013 01:48
Show Gist options
  • Save everblut/7296946 to your computer and use it in GitHub Desktop.
Save everblut/7296946 to your computer and use it in GitHub Desktop.
Cifrado cesar
#!/usr/ruby
# *****------->>>>>Usage: cesar.rb 'texto' #Desplazamiento
ALFABETO = ((0...36).map { |i| i.to_s 36 } + " ,/;'[]\=-!@#.$%^&*<>?:{}|".chars) * 10
def cesar(msg,n)
viejo = [nuevo = msg.chars.map { |c| C(TRUE,c,n.to_i) }.join('')][0].chars.map { |c| C(FALSE,c,n.to_i) }.join('')
puts "*****----> El mensaje despues de E(msg,#{n}): #{nuevo} \n*****----> El mensaje despues de D(msg,#{n}): #{viejo}"
end
def C(s,x,n)
return s ? ALFABETO[ (ALFABETO.index(x) + n) % ALFABETO.count] : ALFABETO[ (ALFABETO.index(x) - n) % ALFABETO.count]
end
def main
return if (ARGV[0].nil? || ARGV[1].nil?)
puts "**-->Mensaje a cifrar [msg]: #{ARGV[0]}" unless ARGV[0].nil?
puts "**-->Desplazamiento [n]: #{ARGV[1]}" unless ARGV[1].nil?
cesar(ARGV[0].downcase,ARGV[1])
end
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment