a small library to encrypt/decrypt data with RSA public/private key pair and base64/85 encoding/decoding capabilities.
you can generate your own key pair as:
openssl genrsa -out private.pem 2048
openssl rsa -in private.pem -out public.pem -pubout -outform PEM
const TEST_PRIVATE_KEY: &str = "-----BEGIN RSA PRIVATE KEY-----
MIIEowIBAAKCAQEAxGDcSAjiHKP9v2ITR+BjQmt9Tx2zW08ZyrjOxPew+Gxl2m5z
JyoP8sicZV81BeMNFkMg6q7sMtRXHhX1nFiTql5HBIqhZohYlN3LIXK2bdPWpDtt
rOFXfsSbZ4Wqy3XhXBhiPNn3kkkRv1N5L/IYcdrxwqaqvTlJzOeQnDsd3+AmkYst
uD4rgElOFkcUawtF7lKIYYFi42cYkJo51UD460mYieBezP6dZhFZB56pZ2rV8cQU
NrUQy2llpj+PxX/yhGnYI88ij0FST0gI2l4UsjtwXVB1Y2SxqrhNMdBU7W6ZA8WU
QQidr4MBxEFoujsLjaCl8LMsbEpAAilKezwubQIDAQABAoIBAFgkwbrzgcopMXP9
qXnRlbvyU0R3qFGLp5/+Y5C1PJHE1dK9UKJ7lrz6nnhBy6Lgzrb3Wob8DLij5pZy
dNPATkdiGa5IKznCaUAobUyOGKQjOWxt4ESAwKz9wmMs9ARu3MBhkXaOvzjB411l
Mjf7Ck3QYENmW6yjUiTOq3H0duxM/rn1Y88a9z2+aoWXQTltWvu0qKfb8SsqKzzx
HQFSalgNUxIqs+NoHRAT4ygzGGgipdP2/gXA966UonYuFAkpkutCeKVd7/6dMbm8
bgnr/x6ivGeLkbIaVkHNPRU+P4SYX1/XZohYIkTbggIih2aeH6+lEka8yZURANI6
HSUwLAECgYEA4bfavKu12NiIUO75/ZcqF8ojXq5+7HXP59t5X5MrURj2jizWs8YH
vPdrvYqxQNMZ0U0ZQBdAUWCn0Z11OXEak7YpKP78yoLIw2YnhgLVFvp3xZ+pIjjN
yidWbIvoq8SLMiUYHrMy3lMwVyFjM/AuA6bqNffCbHXqs9Ut+WnDN6ECgYEA3rlZ
S1gnE0sJrAJ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4U
sjtwXVB1Y2SxqrhNMdBU7W6ZA8WUQQidr4MBxEFoujsLjaCl8LMsbEpAAilKezwu
bQIDAQAB
-----END PUBLIC KEY-----";
use crate::cryptolib::decoder::Decoder;
use crate::cryptolib::encoder::Encoder;
fn main() {
let text = "some text data";
let encoder = Encoder::new(TEST_PUBLIC_KEY);
let encrypted_text = encoder.encrypt(text);
let decoder = Decoder::new(TEST_PRIVATE_KEY);
let decrypted_text = decoder.decrypt(encrypted_text);
assert_eq!(text, decrypted_text);
}
use crate::cryptolib::decoder::Decoder;
use crate::cryptolib::encoder::Encoder;
fn main() {
let text = "some text data";
let encoder = Encoder::new(TEST_PUBLIC_KEY);
let encrypted_text = encoder.encrypt(text);
let base_85_encoded_text = Encoder::to_base85(encrypted_text);
let base85_decoded_text = Decoder::from_base85(base_85_encoded_text);
let decoder = Decoder::new(TEST_PRIVATE_KEY);
let decrypted_text = decoder.decrypt(base85_decoded_text);
assert_eq!(text, decrypted_text);
println!("decrypted text: {}", decrypted_text);
}
use crate::cryptolib::decoder::Decoder;
use crate::cryptolib::encoder::Encoder;
fn main() {
let text = Encoder::to_base85(b"foobar".to_vec());
let decoded_text = Decoder::from_base85(text);
assert_eq!(b"foobar".to_vec(), decoded_text);
}
use crate::cryptolib::decoder::Decoder;
use crate::cryptolib::encoder::Encoder;
fn main() {
let text = Encoder::to_base64(b"foobar".to_vec());
let decoded_text = Decoder::from_base64(text);
assert_eq!(b"foobar".to_vec(), decoded_text);
}
NOTE: please refer to:
- rsa-encrypt-decrypt-golang for the Go lib
- rsa-encrypt-decrypt-python for the Python lib