Crypto 100 challenge requires decryption of a given ciphertext.
There is also an input field that takes in a plain text and responds with a ciphertext. By encrypting 'a' and 'A' we see that it provides the same output. Hence the cipher is case insensitive. The following code solves the challenge by generating a table of cipher text for a-z and then decrypts the given ciphertext using the mapping.
There is also an input field that takes in a plain text and responds with a ciphertext. By encrypting 'a' and 'A' we see that it provides the same output. Hence the cipher is case insensitive. The following code solves the challenge by generating a table of cipher text for a-z and then decrypts the given ciphertext using the mapping.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'uri' | |
require 'net/http' | |
def odd_positions_in_string( str ) | |
str.scan(/(.).?/).join | |
end | |
def even_positions_in_string ( str ) | |
str.scan(/.?(.)/).join | |
end | |
uri = URI.parse('http://176.9.193.13/CrYpt0ch4l12554222426.php') | |
http = Net::HTTP.new(uri.hostname, uri.port) | |
res = http.request_get(uri.path) | |
enc_ver = res.body.delete('^01').scan(/.{54}/) | |
# vertical to horiztal conversion | |
enc_hor_temp = '' | |
54.times {|i| enc_ver.collect{|x| enc_hor_temp << x[i]} } | |
enc_hor = enc_hor_temp.scan(/....../) | |
# create a mapping of 'a'..'z' with its respective encoding | |
char_to_hex_map = Hash.new | |
("a".."z").each do |alpha| | |
uri = URI("http://176.9.193.13/CrYpt0ch4l12554222426.php?key=#{alpha}") | |
html = Net::HTTP.get(uri) | |
enc = html.split(/\r?\n/).last.delete('^01') | |
char_to_hex_map[alpha] = odd_positions_in_string(enc) + even_positions_in_string(enc) | |
end | |
# find the decrypted text | |
decrypted_text = '' | |
enc_hor.each do |i| | |
decrypted_text << char_to_hex_map.key(i) | |
end | |
puts decrypted_text.upcase |
$ ruby cscamp_crypto100.rb
JUSTASMALLBLINDYCRYPTOGAMEZ
0 comments:
Post a Comment