feat: add rsa
This commit is contained in:
18
__crypto/rsa/src/main.rs
Normal file
18
__crypto/rsa/src/main.rs
Normal file
@@ -0,0 +1,18 @@
|
||||
use rsa::{PublicKey, RSAPublicKey, RSAPrivateKey, PaddingScheme};
|
||||
use rand::rngs::OsRng;
|
||||
|
||||
fn main() {
|
||||
let mut rng = OsRng;
|
||||
let bits = 2048;
|
||||
let priv_key = RSAPrivateKey::new(&mut rng, bits).expect("failed to generate a key");
|
||||
let pub_key = RSAPublicKey::from(&priv_key);
|
||||
|
||||
// Encrypt
|
||||
let data = b"hello world";
|
||||
let enc_data = pub_key.encrypt(&mut rng, PaddingScheme::new_pkcs1v15_encrypt(), &data[..]).expect("failed to encrypt");
|
||||
assert_ne!(&data[..], &enc_data[..]);
|
||||
|
||||
// Decrypt
|
||||
let dec_data = priv_key.decrypt(PaddingScheme::new_pkcs1v15_encrypt(), &enc_data).expect("failed to decrypt");
|
||||
assert_eq!(&data[..], &dec_data[..]);
|
||||
}
|
||||
Reference in New Issue
Block a user