Elgamal public key cryptography implementation in Rust.
This library has been built as an exercise in implementing algorithms in Rust, and is not recommended for any production use requiring real security
- generate_keys - Generate Elgamal keypair
fn generate_keys(bit_size: uint) -> (PublicKey, PrivateKey)
-
PublicKey is an object representing the Elgamal public key with the following methods:
- encrypt - Encryption
fn encrypt(&self, m: &BigUint) -> (BigUint, BigUint)
- encrypt_string - Encrypt a plaintext string
fn encrypt_string(&self, m: &str) -> String
- verify - Verify signature
fn verify(&self, r: &BigUint, s: &BigUint, m: &BigUint) -> bool
- verify_string - Verify signature for a string
fn verify_string(&self, sig: &str, m: &str) -> bool
-
PrivateKey is an object representing the Elgamal private key with the following methods:
-
decrypt - Decryption
fn decrypt(&self, c1: &BigUint, c2: &BigUint) -> BigUint
-
decrypt_string - Decrypt a ciphertext string
fn decrypt_string(&self, m: &str) -> String
-
sign - Signature generation
fn sign(&self, m: &BigUint) -> (BigUint, BigUint)
-
sign_string - Sign a string
fn sign_string(&self, sig: &str, m: &str) -> bool
-
extern crate elgamal;
fn main() {
let (public_key, private_key) = elgamal::generate_keys(1024);
let plaintext = "Secret";
let ciphertext = public_key.encrypt_string(plaintext);
let signature = private_key.sign_string(ciphertext.as_slice());
let decrytped_plaintext = private_key.decrypt_string(ciphertext.as_slice());
let verified = public_key.verify_string(signature.as_slice(), ciphertext.as_slice());
println!("Decrypted message: {}, Valid signature : {}", decrytped_plaintext, verified);
}
-
Compile library:
cargo build
-
Run tests:
cargo test