0

这不是我写的;它是由传给我的人写的。我与代码的作者失去了联系。我已经使用这个代码几年了,现在才意识到这个错误。似乎字母序列rkey1弄乱了输出。

例如turkey1输出为解密为tur79y1. 这个 Perl 代码应该输出turkey1而不是tur79y1

$String = "turkey1";
$e = &encode_escaped(&palace_encrypt($String));
$d = &palace_decrypt(&decode_escaped("'\"".$e."\"'"));
print $d."<br>\n";

KEY REMOVED BY OWNER


sub palace_decrypt
{
    local $lastchar = 0;
    local $rc = 0;
    local @bs;
    for($i=length($_[0])-1; $i>=0; $i--) {
        local $tmp = ord(substr($_[0], $i, 1));
        $bs[$i] = $tmp ^ $palace_key[$rc++] ^ $lastchar;
        $lastchar = $tmp ^ $palace_key[$rc++];
        }

    return join("", map { chr($_) } @bs);
}

sub decode_escaped
{
    $_[0] =~ m/\"(.*)\"/;
    local $str = $1;
    $str =~ s/\\\\/\0/g;
    $str =~ s/\\"/"/g;
    $str =~ s/\\(..)/pack("c",hex($1))/ge;
    $str =~ s/\0/\\/g;
    return $str;
}


sub palace_encrypt
{
    local $lastchar = 0;
    local $rc = 0;
    local @bs;
    for($i=length($_[0])-1; $i>=0; $i--) {
        local $b = ord(substr($_[0], $i, 1));
        $bs[$i] = $b ^ $palace_key[$rc++] ^ $lastchar;
        $lastchar = $bs[$i] ^ $palace_key[$rc++];
        }
    return join("", map { chr($_) } @bs);
}

sub encode_escaped
{
    local $str = $_[0];
    $str =~ s/\\/\\\\/g;
    $str =~ s/([^A-Za-z0-9\.\\])/sprintf("\\%2.2X", ord($1))/ge;

    return $str;
}
4

1 回答 1

10

您的问题是您的 decode_escaped 并没有完全撤消 encode_escaped 所做的事情。将其替换为以下内容,这应该可以解决您的问题。

sub decode_escaped
{
    $_[0] =~ m/\"(.*)\"/;
    local @str = split /(\\\\)/, $1;
    foreach (@str) {
        s/\\"/"/g;
        s/\\(..)/chr(hex($1))/ge;
        s/\\\\/\\/;
    }
    return join '', @str;
}
于 2009-07-15T03:24:05.447 回答