Reputation: 684
I'm following this tutorial and I want it to generate and use RSA private and public keys and encrypt/decrypt a text and print it, now I want to see these key in the program , this is as far as I got:
fn print_type_of<T>(_: &T) {
println!("{}", std::any::type_name::<T>())
}
fn main() {
use rsa::{PublicKey, RsaPrivateKey, RsaPublicKey, PaddingScheme};
use rand::rngs::OsRng;
let mut rng = OsRng{};
let bits = 2048;
let private_key = RsaPrivateKey::new(&mut rng, bits).expect("failed to generate a key");
let public_key = RsaPublicKey::from(&private_key);
print_type_of(&public_key);
println!("{:?}",public_key);
println!("Public key: {}", String::from_utf8(public_key).unwrap());
}
I get :
error[E0308]: mismatched types
--> src/main.rs:21:47
|
21 | println!("Public key: {}", String::from_utf8(public_key).unwrap());
| ^^^^^^^^^^ expected struct `Vec`, found struct `rsa::RsaPublicKey`
|
= note: expected struct `Vec<u8>`
found struct `rsa::RsaPublicKey`
If I remove the unwrap line :
rsa::key::RsaPublicKey
RsaPublicKey { n: BigUint { data: [9176583044655610753, 8052921767573809665, 7322363310924215903, 10677358141470723739, 12379608428747184411, 6263963070332523975, 5963846880211652521, 16864047741974290629, 213896623420857268, 4937645776431077684, 15969812743294577757, 13867032477882121383, 5383248258773390155, 14444169918078759013, 9044709521184374311, 9755296615001295698, 18343743949489547843, 12343850134323169490, 16855493837721939000, 14970248798399036434, 16075416073667271075, 9024185634616256000, 14707399524398597897, 11804989747190776592, 11692677034154985673, 10043641354879364290, 2996813664869425496, 3933982337995406743, 15967428092655221732, 12491687912286251425, 2181688508710390266, 14829946030052214341] }, e: BigUint { data: [65537] } }
How could I print this Big integer public key to PEM like characters in the program?
PS : Playgound shows another error!
rand = "=0.8"
rsa= "=0.5.0"
Upvotes: 0
Views: 2273
Reputation: 684
I should gave the credit to the comment by yolenoyer, but the line:
println!("Public key: {:?}", pkcs1::ToRsaPublicKey::to_pkcs1_pem(&public_key));
and pkcs1= "=0.2.4" ,Is the solution! It returns :
Public key: Ok("-----BEGIN RSA PUBLIC KEY-----\nMIIBCgKCAQEAoqQqiUB+Cha260Xa0ldy8gEfiI9GlL9xBEvhRHgHZ91Kq8V22utF\nI/+wng2Uo+Pw4IcopZGuo5MZVwh8THTlLO1i+DJu+/2l2uWY1nS6fkNVJ38AsP0i\ntuyphx1SYJsEY2oO30BqRQWVZ6tGrCtPueqdXWJH12+3sYE3h+kzAa6SVs8jin4u\nAhI0hhRLv2FhHDZM4JDdzhVxQBKg9pNPqUuLCnDPIMSkJyS8QxSzWVcneY8YuJRx\n2HSzPWtB8BZRypvpSS8eYsb9ltfN3d/5OT5g74L/+5NTVIHKo+Dl6gZpAjdeJEM1\nx+DlNe8GENMXakjWBRdL5Kqa6lhZk7dI2QIDAQAB\n-----END RSA PUBLIC KEY-----\n")
If it cloud understand next line(\n) it would be nice but it is PEM and I can't complain more!
But if you want to print it prettier :
let pem1 = pkcs1::ToRsaPublicKey::to_pkcs1_pem(&public_key).unwrap();
print_type_of(&pem1);
eprintln!("Public key: {}",pem1 );
It will :
alloc::string::String
Public key: -----BEGIN RSA PUBLIC KEY-----
MIIBCgKCAQEAmL/BrxO574dYy8rN257CihwRONXz0949kRBJq/RtTG+1P8zDRD8y
IfJhFH83IaibNGxZFCrjcCL5my9bpfo6mena5B59FLghDn8UKZjx/ZqeVF5DCrfG
eRAe0LZqcpgvnaC+IKtAO3fsGn/gMYj7v45U5WAh+DadnjU5UqID9iyMON1w0T1K
hWYQNLeabppCNYMYFv1FfxG2aIA+zsTVl8JzvhptAZ39S7fMCuQugxGwAEp9+hZ3
o3OXk5Gkb3nm2maDb4TgAMdAF5upZcjjJm5ydQoB0IRbh6/JfYwogIpmudvT8+Mj
CPkYoN7WPu2LJ8tevPIQ4M3E73sFIW6c7wIDAQAB
-----END RSA PUBLIC KEY-----
Upvotes: 0