JSON无法在Perl中编码Object类型数据

时间:2014-12-05 06:07:55

标签: json perl encryption cryptography

我正在尝试用JSON编码一些数据,并希望通过浏览器发送JSON编码数据。 但是数据包含Crypt :: RSA :: Key :: Private的OBJECT,并且不能编码与下面给出的相同。

CODE:

use Crypt::RSA;
use Data::Dumper;
use JSON::XS;

my $rsa = Crypt::RSA->new();
my ($public_key, $private_key) = $rsa->keygen( Size => 128 );

print "private_key - " . Dumper($private_key) . "\n" . ref($private_key) . "\n";

my $info = {
    'flag'      => 1,
    'private_key' => $private_key
};

my $json_info = JSON::XS->new->allow_blessed->convert_blessed->encode($info);

print "json_info - " . Dumper($json_info) . "\n";

my $content_hash = JSON::XS->new->allow_blessed->convert_blessed->decode($json_info);
print "content_hash " . Dumper($content_hash) . "\n";

输出:

private_key - $VAR1 = bless( {
                 'Cipher' => 'Blowfish',
                 'Checked' => 0,
                 'Version' => '1.99',
                 'private' => {
                                '_e' => bless( do{\(my $o = '140486154324192')},         'Math::Pari' ),
                                '_dp' => bless( do{\(my $o = 40289976)}, 'Math::Pari' ),
                                '_d' => bless( do{\(my $o = 40151976)}, 'Math::Pari' ),
                                '_dq' => bless( do{\(my $o = 40290328)}, 'Math::Pari' ),
                                '_phi' => bless( do{\(my $o = 39423832)}, 'Math::Pari' ),
                                '_q' => bless( do{\(my $o = 29831976)}, 'Math::Pari' ),
                                '_p' => bless( do{\(my $o = 29832440)}, 'Math::Pari' ),
                                '_n' => bless( do{\(my $o = 40309624)}, 'Math::Pari' ),
                                '_u' => bless( do{\(my $o = 40290392)}, 'Math::Pari' )
                              }
               }, 'Crypt::RSA::Key::Private' );

Crypt::RSA::Key::Private
json_info - $VAR1 = '{"private_key":null,"flag":1}';

content_hash $VAR1 = {
          'flag' => 1,
          'private_key' => undef
        };

private_key不应该为null,所以我在代码中遗漏了上述行为的原因吗?

1 个答案:

答案 0 :(得分:1)

如果您正在寻找RSA私钥的字符串表示形式,可以使用

序列化它(到二进制数据)
Crypt::RSA::Key::Private::SSH->serialize( $private_key )

并使用此模块

Convert::ASCII::Armour

生成字符串表示。