如何将 RSA 私钥转换为 pem
How to convert RSA private key to pem
我需要将使用 RsaPrivateKey::new() 生成的私钥转换为 pem,但无法通过文档弄清楚如何做到这一点
use rsa::{PublicKey, RsaPrivateKey, RsaPublicKey, PaddingScheme, pkcs8::EncodePrivateKey};
// create a private key
let mut rng = rand::thread_rng();
let bits = 2048;
let private_key = RsaPrivateKey::new(&mut rng, bits).expect("Failed to generate key");
// create a public key / address
let public_key = RsaPublicKey::from(&private_key);
创建 pem returns 一个错误,指出 LineEnding 未声明,但他们在文档中是这样做的:
let priv_pem = RsaPrivateKey::to_pkcs8_pem(&private_key, line_ending: LineEnding::default);
您需要从 pkcs8 导入 LineEnding,或将其限定为来自该板条箱:
use rsa::pkcs8::{LineEnding, EncodePrivateKey};
let priv_pem = RsaPrivateKey::to_pkcs8_pem(&private_key, LineEnding::default);
或
use rsa::pkcs8::{self, EncodePrivateKey};
let priv_pem = RsaPrivateKey::to_pkcs8_pem(&private_key, pkcs8::LineEnding::default);
我需要将使用 RsaPrivateKey::new() 生成的私钥转换为 pem,但无法通过文档弄清楚如何做到这一点
use rsa::{PublicKey, RsaPrivateKey, RsaPublicKey, PaddingScheme, pkcs8::EncodePrivateKey};
// create a private key
let mut rng = rand::thread_rng();
let bits = 2048;
let private_key = RsaPrivateKey::new(&mut rng, bits).expect("Failed to generate key");
// create a public key / address
let public_key = RsaPublicKey::from(&private_key);
创建 pem returns 一个错误,指出 LineEnding 未声明,但他们在文档中是这样做的:
let priv_pem = RsaPrivateKey::to_pkcs8_pem(&private_key, line_ending: LineEnding::default);
您需要从 pkcs8 导入 LineEnding,或将其限定为来自该板条箱:
use rsa::pkcs8::{LineEnding, EncodePrivateKey};
let priv_pem = RsaPrivateKey::to_pkcs8_pem(&private_key, LineEnding::default);
或
use rsa::pkcs8::{self, EncodePrivateKey};
let priv_pem = RsaPrivateKey::to_pkcs8_pem(&private_key, pkcs8::LineEnding::default);