Skip to content

Instantly share code, notes, and snippets.

@everblut
Created November 4, 2013 02:35
Show Gist options
  • Save everblut/7297283 to your computer and use it in GitHub Desktop.
Save everblut/7297283 to your computer and use it in GitHub Desktop.
Cifrado por transposicón
#!/usr/ruby
# *****------->>>>>Usage: cesar.rb 'texto'
def transposicion(msg)
cif = C(msg).map(&:reverse).transpose.map { |fila| fila.join('') }.join(' ')
return "****---->>>> Mensaje cifrado: #{cif} \n****---->>>> Mensaje descifrado: #{C(cif,cif.split[0].size).reverse.transpose.map { |fila| fila.join('') }.join(' ')}"
end
def C(msg,s=-1)
arr,ran, matrix = [msg.split.join.split('')] + [s == -1 ? Random.rand(3...6) : s] + [[[]]]
ran.times {|n| arr.each_slice(ran).with_index { |bloq,i| matrix[i] = Array.new(ran).each_with_index.map { |val,idx| val = bloq[idx].nil? ? (10...36).map { |i| i.to_s 36 }.sample : bloq[idx] } } }
return matrix
end
def main
return if ARGV[0].nil?
puts "\n**-->Mensaje a cifrar [msg]: #{ARGV[0]}\n#{transposicion(ARGV[0].downcase)}"
end
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment