在Perl中执行按位XOR时如何转义“^”字符?我的脚本没问题但是当我输入一个像.1M80P]/)S@*>RQF^RM< \n
这样的字符串时,输出就搞砸了:
#!/usr/bin/perl
$key = pack("H*","3cb37efae7f4f376ebbd76cd");
print "Enter string to decode: ";
$str=<STDIN>;chomp $str; $str =~s/\\(.)/$1/g;
$dec = decode($str);
print "Decoded string value: $dec\n";
sub decode{
@subvar=@_;
my $sqlstr = $subvar[0];
$cipher = unpack("u", $sqlstr);
$plain = $cipher^$key;
return substr($plain, 0, length($cipher));
}
输出:
Enter string to decode: .1M80P]/)S@*>RQF^RM< \n
Decoded string value: zen94==tuvosÊ×
奇怪的是,以下字符串,\=_\\^M;+ D1F$U@7L\n
正常工作并解码为KL@OU4books!
,但.;H ^F8B8EQ">SA^BDL8 \n
再次无法正常工作并返回R3@coldfusioÆ
以下是ikegami的清洁代码(虽然结果相同):
#!/usr/bin/perl
use strict;
use warnings;
sub deliteral {
my ($s) = @_;
$s =~ s/\\n/\n/g;
die "Unrecognised escape \\$1\n" if $s =~ /\\[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*', '3cb37efae7f4f376ebbd76cd');
print "Enter string to decode: ";
chomp( my $coded = <STDIN> );
my $cipher = uudecode(deliteral($coded));
my $plain = decode($key, $cipher);
print("Plain text: $plain\n");
答案 0 :(得分:0)
所以这是解决方案,感谢ikegami的帮助:
#!/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");
问题在于KEY的长度。