Files
libsm/examples/encrypt_and_decrypt.rs

18 lines
626 B
Rust

use libsm::sm2::encrypt::{DecryptCtx, EncryptCtx, Sm2EncryptionMode};
use libsm::sm2::signature::SigCtx;
fn main() {
let msg = "hello world".as_bytes();
let klen = msg.len();
let ctx = SigCtx::new();
let (pk_b, sk_b) = ctx.new_keypair().unwrap();
let encrypt_ctx = EncryptCtx::new(klen, pk_b);
let cipher = encrypt_ctx.encrypt(msg, Sm2EncryptionMode::C1C3C2).unwrap();
let decrypt_ctx = DecryptCtx::new(klen, sk_b);
let plain = decrypt_ctx.decrypt(&cipher, Sm2EncryptionMode::C1C3C2).unwrap();
println!("{}", hex::encode(&cipher));
println!("{}", String::from_utf8_lossy(&plain));
}