在Perl中将RSA密钥转换为JSON [英] Converting RSA keys to JSON in Perl

查看:377
本文介绍了在Perl中将RSA密钥转换为JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要找到一种将RSA公钥传输到我的网络通信程序的服务器的方法。我做了一些研究,似乎最简单的方法是将公共密钥(存储为某种哈希引用)转换为JSON进行传输。但是,在我的测试代码中,我不能得到转换为JSON的关键。这是我的测试程序:

I need to find a way of transferring an RSA public key to a server for my network communication program. I have done some research, and it seems that the easiest way to do this is to convert the public key (which is stored as some kind of hash reference) to a JSON for transmission. However, in my test code I cannot get the key to convert to a JSON. Here is my test program:

use strict;
use warnings;

use Crypt::RSA;
use JSON;

my %hash = ( name => "bob",
             age  => 123,
             hates=> "Perl"
        );

    my $hash_ref = \%hash;
    my $hash_as_json = to_json($hash_ref);

    print $hash_as_json, "\n"; # Works fine for a normal hash

    my $rsa = new Crypt::RSA;

    my ($public, $private) = $rsa->keygen ( 
            Identity  => 'client',
            Size      => 512,  
            Password  => 'password', 
            Verbosity => 1,
        ) or die $rsa->errstr();

    my $key_hash_as_json = to_json($public, {allow_blessed => 1, convert_blessed => 1});
    print $key_hash_as_json, "\n";



在我找到 {allow_blessed => 1,convert_blessed => 1} 我收到一条错误消息,说:

Before I found the line {allow_blessed => 1, convert_blessed => 1} I got an error message saying


遇到的对象'Crypt :: RSA :: Key :: Public = hash(0x3117128)',但是
既不是allow_blessed,convert_blessed也不是allow_tags设置是
启用(或缺少TO_JSON / FREEZE方法)在
/ home / alex / perl5 / lib / perl5 / JSON .pm line 154。

encountered object 'Crypt::RSA::Key::Public=HASH(0x3117128)', but neither allow_blessed, convert_blessed nor allow_tags settings are enabled (or TO_JSON/FREEZE method missing) at /home/alex/perl5/lib/perl5/JSON.pm line 154.

这是什么意思,为什么这一行会修复?

What does this mean and why did that line fix it?

添加代码后,当我尝试并打印JSON时,它只会给出 null 。为什么会发生这种情况,我该如何解决?

After adding the code, it just gives null when I try and print the JSON. Why is this happening and how do I fix it?

或者,有更好的方式来做我在这里做的事吗?

Alternatively, is there a better way of doing what I am trying here?

推荐答案

将RSA公钥表示为文本的最常见方法是PEM编码。不幸的是, Crypt :: RSA 不提供任何方式来转换或从这种格式,或确实任何其他标准格式。不要使用它!

The most common way of representing an RSA public key as text is the PEM encoding. Unfortunately, Crypt::RSA does not provide any way to convert to or from this format, or indeed any other standard format. Don't use it!

相反,我建议您使用 Crypt :: OpenSSL :: RSA 。生成私钥并使用此模块打印其公共表单很简单:

Instead, I'd recommend that you use Crypt::OpenSSL::RSA. Generating a private key and printing its public form with this module is simple:

use Crypt::OpenSSL::RSA;

my $key = Crypt::OpenSSL::RSA->generate_key(512);
print $key->get_public_key_string;

这将输出如下的PEM编码:

This will output a PEM encoding like the following:

-----BEGIN RSA PUBLIC KEY-----
MEgCQQDd/5F9Rc5vsNuKBrd4gfI4BDgre/sTBKu3yXpk+8NjByKpClsi3IQEGYeG
wmv/q/1ZjflFby1MPxMhXZo/82CbAgMBAAE=
-----END RSA PUBLIC KEY-----

这篇关于在Perl中将RSA密钥转换为JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆