我有一些Perl代码(简单的XOR解密),我想将它移植到Ruby以将其添加到另一个脚本但我真的迷失在Ruby中的XOR编码/解码:
#!/usr/bin/perl
# XOR password decoder
# Greets: Joni Salonen @ stackoverflow.com
$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));
}
与perl一起使用的示例:
$ perl cf6deca.pl
Enter string to decode: )4-H5GX\:&G\!6
Decoded string value: likearock
感谢您的帮助和时间。
我想在Ruby中使用这样的东西:
key = ['3cb37efae7f4f376ebbd76cd'].pack('H*')
print "Enter string to decode: "
STDOUT.flush
a_string = gets
a_string.chomp!
a_string = a_string.gsub(/\//, "")
dec = decode(a_string)
puts "Decoded string value: "+dec
def decode(in)
cipher = in.unpack('u')
plain = cipher^key;
plain.slice(len(cipher))
return plain
end
我知道是TOTAL MESS,请帮助:)。
答案 0 :(得分:2)
只是看看stackoverflow上的一些问题,看来ruby可能没有按位字符串运算符。 One post表明这可能有助于提供这样的运营商:
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