[英]Perl XOR escape string that contains “^”
How can I escape a "^" character when doing a bitwise XOR in Perl? 在Perl中进行按位异或时,如何转义“ ^”字符? My script is ok but when I input a string like
.1M80P]/)S@*>RQF^RM< \\n
then the output gets messed up: 我的脚本还可以,但是当我输入
.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));
}
Output: 输出:
Enter string to decode: .1M80P]/)S@*>RQF^RM< \n
Decoded string value: zen94==tuvosÊ×
What is weird, the following string ,\\=_\\\\^M;+ D1F$U@7L\\n
works ok and decodes as KL@OU4books!
奇怪的是,以下字符串
,\\=_\\\\^M;+ D1F$U@7L\\n
正常工作并解码为KL@OU4books!
but again .;H ^F8B8EQ">SA^BDL8 \\n
doesn't work as expected and gives back R3@coldfusioÆ
但是再次
.;H ^F8B8EQ">SA^BDL8 \\n
无法正常工作,并退回R3@coldfusioÆ
Here is the cleaned code by ikegami (same results though): 这是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");
So this is the solution, thanks to ikegami help: 因此,这是解决方案,这要感谢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");
The problem was with the length of the KEY. 问题出在密钥的长度上。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.