feat: add sm2 encryption mode

This commit is contained in:
2023-10-22 14:24:25 +08:00
parent bcd06779ca
commit 8657e5ffce
3 changed files with 44 additions and 9 deletions

View File

@@ -0,0 +1,18 @@
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));
}