使用HEX密钥对Perl进行Ruby XOR解码

时间:2013-12-29 13:01:38

标签: ruby perl

我编写了以下简单的Perl脚本,用一个HEX键对一个字符串进行异或,我尝试(到目前为止成功99%)将它移植到Ruby但是成功不是100%,因为在某些情况下它失败了(有些特殊字符):

Perl脚本:

#!/usr/bin/perl
use strict;
use warnings;

sub deliteral {
   my ($s) = @_;
   $s =~ s/\\n/\n/g;
   die "Unrecognised escape \\$1\n" if $s =~ /(?<!\\)(?:\\{2})*\\([a-zA-Z0-9])/;
   $s =~ s/\\(.)/$1/sg;
   return $s;
}

sub uudecode {
   return unpack 'u', $_[0];
}

sub decode {
   my ($key, $cipher) = @_;
   return substr($cipher^$key, 0, length($cipher)); # XXX
}

my $key = pack('H*', '3cb37efae7f4f376ebbd76cdfce7391e9ed9cee4cfceb4b33332fc96ff7b');

print "Enter string to decode: ";
chomp( my $coded = <STDIN> );

my $cipher = uudecode(deliteral($coded));
my $plain = decode($key, $cipher);
print("Plain text: $plain\n");

Ruby脚本:

#!/usr/bin/env ruby

key = ['3cb37efae7f4f376ebbd76cdfce7391e9ed9cee4cfceb4b33332fc96ff7b'].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

ruby​​脚本失败的示例:

root@server:~/coding/ruby$ perl dec6admin.pl
Enter string to decode: ,6]\\3B8Z9@QJ.Q4_T\n
Plain text: glmsimplex99
root@server:~/coding/ruby$ ruby dec6admin.rb
Enter string to decode: ,6]\\3B8Z9@QJ.Q4_T\n
Decoded string value: ggA5ÕZîÍ

root@server:~/coding/ruby$ perl dec6admin.pl
Enter string to decode: *4\\L'GM?,EQ?9B0  \n
Plain text: oxyd08da24
root@server:~/coding/ruby$ ruby dec6admin.rb
Enter string to decode: *4\\L'GM?,EQ?9B0  \n
Decoded string value: nrOÑ6ý

root@server:~/coding/ruby$ perl dec6admin.pl
Enter string to decode: 33\\8.GY67DAJ"VP2LFXY5\=\\'N^@  \n
Plain text: supercalifragili_74
root@server:~/coding/ruby$ ruby dec6admin.rb
Enter string to decode: 33\\8.GY67DAJ"VP2LFXY5\=\\'N^@  \n
Decoded string value: q0ÙuÖ]|]ërdqyÎ

root@server:~/coding/ruby$ perl dec6admin.pl
Enter string to decode: +7\=\\;EI*&D@+8C40 \n
Plain text: alelurat302
root@server:~/coding/ruby$ ruby dec6admin.rb
Enter string to decode: +7\=\\;EI*&D@+8C40 \n
Decoded string value: aeFPsÀÈìv

root@server:~/coding/ruby$ perl dec6admin.pl
Enter string to decode: '"\\ 1C(*&T@  \n
Plain text: 7sover!
root@server:~/coding/ruby$ ruby dec6admin.rb
Enter string to decode: '"\\ 1C(*&T@  \n
Decoded string value: 4·ÚF@s

root@server:~/coding/ruby$ perl dec6admin.pl
Enter string to decode: *4\\,*RY>&G$?9C0  \n
Plain text: opt1pro120
root@server:~/coding/ruby$ ruby dec6admin.rb
Enter string to decode: *4\\,*RY>&G$?9C0  \n
Decoded string value: lqÌSâý

任何想法我在Ruby脚本中哪里错了?

1 个答案:

答案 0 :(得分:3)

比较这一行:

$s =~ s/\\(.)/$1/sg;
在您的Perl代码中使用相应的行

a_string = a_string.gsub(/\\/, "")
在您的Ruby代码中

。你能看出差异吗?

这是一个提示:Ruby代码从字符串中剥离所有反斜杠,而Perl代码用该字符替换反斜杠后跟任何其他字符(包括另一个反斜杠)。这意味着您在Perl代码处理时输入,6]\\3B8Z9@QJ.Q4_T变为,6]\3B8Z9@QJ.Q4_T,而在Ruby代码处理时输入,6]3B8Z9@QJ.Q4_T

使用等效的Perl代码替换Ruby代码中的那一行,即:

a_string = a_string.gsub(/\\(.)/s, '\1')

让它适合我。

聚苯乙烯。为什么你甚至需要在uudecoding之前处理字符串?在没有任何加倍的反斜杠或尾随换行符的情况下存储输入会更加简单。