我有一个函数可以将我想要的超时信息字符串化为base64 url encode
,但是当我尝试解码编码的字符串时,它会返回看起来与最初创建的字符串完全不同的垃圾。
输出
[i] original string > 05cee990c62ca2ce5dfe6cd77115a96d|1331012040|1331004840|7200
[i] encoded string > MDVjZWU5OTBjNjJjYTJjZTVkZmU2Y2Q3NzExNWE5NmR8MTMzMTAxMjA0MHwxMzMxMDA0ODQwfDcyMDA=
[i] decoded string > Ó{ßtskgå×ÞéÇ{ï]ykÞ
令人遗憾的是,相同的功能能够解析从Facebook检索到的base64url编码信息。
功能
sub timeoutStringGen{
my ($name,$seconds) = @_;
my $uuid = uniqueID(); #generates random string
my $timeStr = time();
my $timeEnd = $timeStr + $seconds;
my $timeoutString = "$uuid|$timeEnd|$timeStr|$seconds";
my $encodedString = encode_base64url( $timeoutString );
my $decodedString = decode_base64url( $timeoutString );
_info "original string > $timeoutString"; #interal log function outputs to STDERR
_info "encoded string > $encodedString";
_info "decoded string > $decodedString";
return $timeoutString;
}
sub timeoutStringParse{
my ($timeoutString) = @_;
return 0 unless $timeoutString;
my ($uuid,$end,$start,$secs) = split /\Q|/,$timeoutString;
my $curr = time();
my $left = $end - $curr;
my $isExpired = ($left > 0) ? 1 : 0;
my $timeHash = {
uuid => $uuid,
end => $end,
start => $start,
secs => $secs,
exp => $isExpired,
left => $left,
curr => $curr
};
return $timeHash;
}
but maybe you can help me understand why these dont work to encode and decode the string properly
#--------
sub encode64{
my($data) = @_;
return MIME::Base64::encode_base64($data);
}
#--------
sub decode64{
my($data) = @_;
return MIME::Base64::decode_base64($data);
}
#--------
sub encode_base64url{
my($data) = @_;
return 0 unless $data;
$data = encode64($data);
$data =~ tr#\-_#+/#;
return($data);
}
#--------
sub decode_base64url{
my($data) = @_;
return 0 unless $data;
$data =~ tr#+/#\-_#;
$data = decode64($data);
return($data);
}
答案 0 :(得分:4)
这:
my $decodedString = decode_base64url( $timeoutString );
应该是:
my $decodedString = decode_base64url( $encodedString );
^^^^^^^^^^^^^^
如上所述,您正在解码原始值,不是编码的原始值。