为什么我的解密功能不起作用?

时间:2022-08-23 12:19:48

This was not written by me; it was written by someone who passed it down to me. I lost contact with the author of the code. I have been using this code for a few years and just now realized this error. It seems that the letter sequence rkey1 messes up the output.

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

For example turkey1 outputs as decryption as tur79y1. This Perl code should output turkey1 and not tur79y1:

例如,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;
}

1 个解决方案

#1


Your problem is that your decode_escaped does not exactly undo what encode_escaped did. Replace it with the following and that should fix your problem.

您的问题是您的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;
}

#1


Your problem is that your decode_escaped does not exactly undo what encode_escaped did. Replace it with the following and that should fix your problem.

您的问题是您的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;
}