$slow = false $input = '49276d206b696c6c696e6720796f757220627261696e206c696b65206120706f69736f6e6f7573206d757368726f6f6d' $b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".scan(/./) puts "----------------------------------------------------------------------------------" puts ">> crypto challenge :: Set: 1 :: Challenge: 1 " puts "----------------------------------------------------------------------------------\n" def msg_to_hex(msg) #return msg.scan(/./).map { |x| x.unpack('H*').join }.join msg.unpack('H*')[0] end def str_to_hex(s) return s.scan(/../).map { |x| '0x' + x }.join end def str_to_intArray(s) return s.scan(/../).map { |i| i.hex.to_i }.to_a end def hex_to_bin(h) return h.scan(/..../).map { |x| "%08b" % x.hex.to_i }.join end def bin_to_base64(bi) return bi.scan(/....../).map { |x| $b64[x.to_i(2)] }.join end def str_to_base64(s) return [[s].pack('H*')].pack('m0') #return bin_to_base64(hex_to_bin(str_to_hex(s))) end puts "hex: \n" + str_to_hex($input) puts "\n" puts "bin: \n" + hex_to_bin(str_to_hex($input)) puts "\n" puts "base64: \n" + bin_to_base64(hex_to_bin(str_to_hex($input))) # hex string to ascii str = [$input].pack('H*') puts "String: " + str puts "\n TEST: " + [str].pack('m0') puts "\n----------------------------------------------------------------------------------" puts ">> crypto challenge :: Set: 1 :: Challenge: 2 " puts "----------------------------------------------------------------------------------\n" $inputA = 'm3OW m30wmEoWMeoW meowm30Wm30WMEoW wMEOw m30WmEoWmeoWm30w MeOwMeowm30wm30Wm3ow MEOwm3oW' $inputB = '686974207468652062756c6c277320657965' def do_xor(a,b) binA = hex_to_bin(str_to_hex(a)).to_i(2) binB = hex_to_bin(str_to_hex(b)).to_i(2) return (binA ^ binB).to_s(16) end puts "xor: \n" + do_xor($inputA, $inputB) puts "xor_str: \n" + [do_xor($inputA, $inputB)].pack('H*') def do_xor_byte(a,b) aa = str_to_intArray(a) ab = str_to_intArray(b) i = 0 xor = '' while i < aa.size do xor << ( aa[i] ^ ab[i] ).to_s(16) i += 1 end return xor end puts "XOR-Byte-String: " + [do_xor_byte($inputA, $inputB)].pack('H*') puts "\n----------------------------------------------------------------------------------" puts ">> crypto challenge :: Set: 1 :: Challenge: 3 " puts "----------------------------------------------------------------------------------\n" $input3 = 'wMEOw m30WmEoWmeoWm30w MeOwMeowm30wm30Wm3ow MEOwm3oW' $keywords = ['the', 'be', 'to', 'of', 'and', 'a', 'in', 'that', 'have', 'I', 'it', 'for', 'not', 'on', 'with', 'he', 'as', 'you', 'do', 'at', 'this', 'but', 'his', 'by', 'from', 'they', 'we', 'say', 'her', 'she', 'or', 'an', 'will', 'my', 'one', 'all', 'would', 'there', 'their', 'what', 'so', 'up', 'out', 'if', 'about', 'who', 'get', 'which', 'go', 'me', 'when', 'make', 'can', 'like', 'time', 'no', 'just', 'him', 'know', 'take', 'people', 'into', 'year', 'your', 'good', 'some', 'could', 'them', 'see', 'other', 'than', 'then', 'now', 'look', 'only', 'come', 'its', 'over', 'think', 'also', 'back', 'after', 'use', 'two', 'how', 'our', 'work', 'first', 'well', 'way', 'even', 'new', 'want', 'because', 'any', 'these', 'give', 'day', 'most', 'us'] def do_xor_crack(a,key) aa = str_to_intArray(a) i = 0 xor = '' while i < aa.size do xor << ( aa[i] ^ key ).to_s(16) i += 1 end return xor end def decrypt_xor(h) bf = Array.new result = Hash.new for i in 0..255 bf[i] = [do_xor_crack(h, i )].pack('H*') result[i] = freq_to_int(bf[i]) # puts "#{freq_to_int(freq_anal(bf[i]))}: " + bf[i] # puts "#{i}: " + bf[i] # puts "xor #{i}: " + [do_xor_crack($input3, i )].pack('H*') end puts "-= Results for XOR decipher analysis =-" result.sort_by {|k,v| v}.reverse[0..4].each do |k,v| puts "Key:#{k.chr} | Score: #{v} | Text: #{bf[k]}" end end def freq_to_int(msg) hash = freq_anal(msg) score = 0 hash.each do |k,v| score = case k when ('e' or 't' or 'a' or 'o' or 'i') then score += 3 * v else score += v end end score += 10 * (msg.split(' ') & $keywords).length # score += 10 if $keywords.any? {|w| msg[w]} return score end def freq_anal(msg) h = Hash.new 0 # s = msg.gsub(/[\W_]+/, '') s = msg.gsub(/[^A-Za-z]+/, '').downcase unless s.length == 0 s.split("").each do |c| h[c.chr] += 1 end end return h end decrypt_xor($input3) puts "\n----------------------------------------------------------------------------------" puts ">> crypto challenge :: Set: 1 :: Challenge: 4 " puts "----------------------------------------------------------------------------------\n" $eval_hash = Hash.new def xor_analyze(msg) bf = Array.new ret = Array.new result = Hash.new for i in 0..255 bf[i] = [do_xor_crack(msg, i )].pack('H*') result[i] = freq_to_int(bf[i]) end ret = result.sort_by {|k,v| v}.reverse[0] return ret[0], ret[1], bf[ret[0]] end def find_xor_string result = Hash.new values = Array.new line_num = 0 File.open('./4.txt').each do |line| score = 0 key = 0 msg = '' key, score, msg = xor_analyze(line) $eval_hash[score] = [line_num, key, msg] line_num += 1 end puts "-= Results for XOR-File decipher analysis =-" $eval_hash.sort_by {|k,v| k}.reverse[0..4].each do |k,v| puts "Score: #{k} Line: #{v[0]} Key: #{v[1]} | Text: #{v[2]}" end =begin result.sort_by {|k,v| v}.reverse[0..19].each do |k,v| puts "#{k}: Score: #{v[0]} | Text: #{bf[v[0]]} " end =end end puts "no output because slow == false" if $slow == false find_xor_string() if $slow == true puts "\n----------------------------------------------------------------------------------" puts ">> crypto challenge :: Set: 1 :: Challenge: 5 " puts "----------------------------------------------------------------------------------\n" $input5 = "Burning 'em, if you ain't quick and nimble\n" + "I go crazy when I hear a cymbal" $input51 = "0b3637272a2b2e63622c2e69692a23693a2a3c6324202d623d63343c2a26226324272765272" + "a282b2f20430a652e2c652a3124333a653e2b2027630c692b20283165286326302e27282f" def decrypt_xor_string(msg,key) aa = str_to_intArray(msg_to_hex(msg)) # aa[42] = 10 hex_key = msg_to_hex(key.ljust(aa.length, key)) full_key = str_to_intArray(hex_key) i = 0 xor = '' while i < aa.size do xor << ( aa[i] ^ full_key[i] ).to_s(16).rjust(2, '0') # puts "#{i} aa= #{aa[i]} val= #{( aa[i] ^ full_key[i] ).to_s(16).rjust(2, '0')} str= #{msg[i..i]} xor= #{full_key[i]} " i += 1 end return xor end string = decrypt_xor_string($input5,"ICE") puts string puts "Identical: #{ string.eql? $input51}" puts "\n----------------------------------------------------------------------------------" puts ">> crypto challenge :: Set: 1 :: Challenge: 6 " puts "----------------------------------------------------------------------------------\n"
Write, Run & Share Ruby code online using OneCompiler's Ruby online compiler for free. It's one of the robust, feature-rich online compilers for Ruby language, running on the latest version 2.3.1. Getting started with the OneCompiler's Ruby compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as Ruby
and start coding.
OneCompiler's Ruby online editor supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample Ruby program which takes name as input and prints hello message with your name.
name = gets.chomp
print "Hello #{name}.\n"
Ruby is a general purpose object oriented programming language developed by Yukihiro Matsumoto.
Data type | Description | Usage |
---|---|---|
Fixnum | Represents normal numbers | x = 10 |
Bignum | Represents big numbers | x =9999999999 |
Float | Represents decimal numbers | x = 3.14 |
Complex | Represents imaginary numbers | x = 1 + 2i |
Rational | Represents fractional numbers | x = 1/4 |
BigDecimal | Represents precision decimal numbers | x=1.0 |
Hash | Represents key value pairs | {"Website"=>"onecompiler","message" => "Happy learning"} |
In Ruby, there is no need to explicitly declare variables to reserve memory space. When you assign a value to a variable, declaration happens automatically and a prefix is needed to indicate the following variable types.
Variable type | Description |
---|---|
Local variables | Scope is limited to the block of the variable initialization. Variable name must start with either _ or lowercase letter. |
Class variables | Class variables belongs to whole class and can be accessible anywhere inside the class. Variable name must start with @@ . They must be initialized before use. |
Instance variables | Instance variables belongs to a instance of the class. They can be accessed from any instance of the class within a method. Variable name must start @ |
Global variables | Scope is global and hence they can be accessible anywhere in the program. Variable name must start with $ |
If, If-else, Nested-Ifs are used when you want to perform a certain set of operations based on conditional expressions.
if(conditional-expression)
#code
end
if(conditional-expression)
#code if condition is true
else
#code if condition is false
end
if(condition-expression1)
#code if above condition is true
elsif(condition-expression2)
#code if above condition is true
elsif(condition-expression3)
#code if above condition is true
...
else
#code if all the conditions are false
end
Case is similar to Switch statement, where it is used to execute one set of a statement from multiple conditions.
case expression
[when expression [, expression ...] [then]
# code ]...
[else
# code ]
end
For loop is used to iterate a set of statements based on a condition.
for variable in expression do
# code
end
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while condition do
# code
end
Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.
loop do
#code
break if conditional-expression
end