0
如何在Perl中執行按位異或時轉義「^」字符?我的劇本是好的,但是當我輸入像.1M80P]/)[email protected]*>RQF^RM< \n
一個字符串,然後輸出得到弄糟:包含「^」的Perl XOR轉義字符串
#!/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{
@[email protected]_;
my $sqlstr = $subvar[0];
$cipher = unpack("u", $sqlstr);
$plain = $cipher^$key;
return substr($plain, 0, length($cipher));
}
輸出:
Enter string to decode: .1M80P]/)[email protected]*>RQF^RM< \n
Decoded string value: zen94==tuvosÊ×
有什麼奇怪的,下面的字符串,\=_\\^M;+ [email protected]\n
工程確定並解碼爲[email protected]!
但再次如預期還給[email protected]Æ
這裏.;H ^F8B8EQ">SA^BDL8 \n
不起作用由池上清潔代碼(雖然相同的結果):
#!/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");
您期待的輸出是什麼? – 2013-02-20 10:58:36
所以你說它在純文本比關鍵字長時不起作用。 – ikegami 2013-02-20 12:15:13
不是,這是有效的:',6 \\\ = =/S,G \!PQF?SQF5 \ n'解碼爲'gt16.50otroX' – bsteo 2013-02-20 12:19:20