使用uri_escape_utf8后解码字符串出现问题



在Perl中,我加密字符串,然后使用uri_escape_utf8,这样我就可以在URL中发送它。然后,我收到的似乎是完全相同的编码字符串返回到Perl,但无法找出解码它。

我用Perl创建了一个简单的测试程序:
$encodedUsername = uri_escape_utf8(encrypter($u));
$enc = Encode::decode('utf8', uri_unescape($encodedUsername));
$final = unencrypter($enc);

不起作用,$final是一个空字符串。

如果我做一个常规的uri_escape而不是uri_escaps_utf8,它都工作得很好。但我需要做uri_escape_utf8与javascript的encodeURIcomponent兼容。

我错过了什么?

谢谢!——乔恩·

背景:我在字符串上使用河豚加密,然后使用uri_escape_utf8,这样我就可以在URL中发送字符串。接收方是一个javascript程序,它使用encodeURIComponent将编码后的字符串发送回Perl程序。encodeURIComponent应该等同于uri_eacape_utf8。

my $mykeyS = <secret key>; 
my $len = 8;
my $mS = Crypt::CBC->new( -literal_key  => 1,
-key       => $mykeyS,
-iv        => $iv8,
-keysize   => $len,
-cipher    => 'Blowfish',
-header    => 'none'
);
sub encrypter{
my ($val) = @_;
if (length $val < 8) {
$val = pack('A8', $val);
}
return $mS->encrypt($val);
}
sub unencrypter {
my ($valEncrypted) = @_;
eval {
$val = $mS->decrypt($valEncrypted);
if (length $val == 8) {
$val = unpack('A8', $val);
}
};
if ($@) {
$val = '';
}
return $val;
}

明白了。我没有使用uri_escape_utf8,而是使用

use MIME::Base64 qw(encode_base64url decode_base64url);
encode_base64url(encrypter($u));  # to encode
unencrypter(decode_base64url($p1)); # to decode

到目前为止,它工作得很好!

最新更新