Search code examples
rubyperlxor

Port small bitwise XOR perl function to Ruby


I have the following Perl script that does a bitwise XOR on a string with a HEX key:

#!/usr/bin/perl

$key = pack("H*","3cb37efae7f4f376ebbd76cd");

print "Enter string to decode: ";
$str=<STDIN>;chomp $str; $str =~ s/\\//g;
$dec = decode($str);
print "Decoded string value: $dec\n";

sub decode{ #Sub to decode
    @subvar=@_;
    my $sqlstr = $subvar[0];
    $cipher = unpack("u", $sqlstr);
    $plain = $cipher^$key;
    return substr($plain, 0, length($cipher));
}

Sample output of running it:

$ perl deXOR.pl
Enter string to decode: (?LM-D\=^5DB$ \n
Decoded string value: Bx3k8aaW

I tried to port it to Ruby but I'm doing something wrong, the result is not the same:

#!/usr/bin/env ruby

key = ['3cb37efae7f4f376ebbd76cd'].pack('H*')

print "Enter string to decode: "
STDOUT.flush
a_string = gets
a_string.chomp!
a_string = a_string.gsub(/\//, "")
dec = String(key)
puts "Decoded string value: "+dec

class String
  def xor(key)
    text = dup
    text.length.times {|n| text[n] = (text[n].ord ^ key[n.modulo key.size].ord).chr }
    text
  end
end

Sample output:

$ ruby deXOR.rb
Enter string to decode: (?LM-D\=^5DB$ \n
Decoded string value: <³~úçôóvë½vÍ

What am I doing wrong? Any thoughts? Thanks!

Changed, still a mess...

key = ['3cb37efae7f4f376ebbd76cd'].pack('H*')

def xor(text, key)
  text.length.times {|n| text[n] = (text[n].ord ^ key[n.modulo key.size].ord).chr}
  text
end

print "Enter string to decode: "
STDOUT.flush
a_string = gets
a_string.chomp!
a_string = a_string.gsub(/\//, "")
dec = xor(a_string, key)
puts "Decoded string value: "+dec

Output:

$ ruby deXOR.rb
Enter string to decode: (?LM-D\=^5DB$ \n
Decoded string value: 2·Ê°¯Kµ2"

Working version with Digitaka's help:

key = ['3cb37efae7f4f376ebbd76cd'].pack('H*')

def decode(str, key)
  text = str.dup
  text.length.times { |n| text[n] = (text[n].ord ^ key[n.modulo key.size].ord).chr }
  text
end

print "Enter string to decode: "
STDOUT.flush
a_string = gets
a_string.chomp!
a_string = a_string.gsub(/\\n/, "")
a_string = a_string.gsub(/\\/, "")
a_string = a_string.unpack('u')[0]
dec = decode(a_string,key)
puts "Decoded string value: "+dec

Output:

$ ruby deXOR.rb
Enter string to decode: (?LM-D=^5DB$ \n
Decoded string value: Bx3k8aaW

Solution

  • In the perl, your code is uudecodeing the entered string, and the equivalent isn't happening in ruby. This snippet uudecodes and decodes like the perl code:

    key = ['3cb37efae7f4f376ebbd76cd'].pack('H*')
    
    # took some liberties to simplify the input text code
    istr = "(?LM-D=^5DB$ ".unpack('u')[0]
    
    def decode(str, key)
      text = str.dup
      text.length.times { |n| text[n] = (text[n].ord ^ key[n.modulo key.size].ord).chr }
      text
    end
    
    puts decode(istr,key) 
    # => Bx3k8aaW