Skip to content

Instantly share code, notes, and snippets.

@lcfr-eth
Created September 17, 2023 20:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lcfr-eth/105e7a9346071def30fba7cdc6856a3e to your computer and use it in GitHub Desktop.
Save lcfr-eth/105e7a9346071def30fba7cdc6856a3e to your computer and use it in GitHub Desktop.
rust ecies stuff
/*
[package]
name = "rust"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
secp256k1 = "0.20"
hex = "0.4"
ecies = {version = "0.2", features = ["std"]}
*/
use ecies::{decrypt, encrypt};
use secp256k1::{PublicKey, SecretKey, Secp256k1};
use hex;
fn main() {
const MSG: &str = "helloworldAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAB";
let private_key_str = "0000000000000000000000000000000000000000000000000000000000000001";
let secret_bytes = hex::decode(private_key_str).expect("Failed to decode hex string");
let secp = Secp256k1::new();
let sk = SecretKey::from_slice(&secret_bytes).expect("Failed to create SecretKey");
let pk = PublicKey::from_secret_key(&secp, &sk);
let sk_bytes = &sk[..];
let pk_bytes = &pk.serialize_uncompressed();
let msg = MSG.as_bytes();
let encrypted = encrypt(pk_bytes, msg).unwrap();
println!("PublicKey: {}\n", hex::encode(pk_bytes));
println!("encrypted: {}\n", hex::encode(encrypted.clone()));
let binding = decrypt(sk_bytes, &encrypted).unwrap();
let dec_str = std::str::from_utf8(&binding).expect("Failed to convert decrypted data to UTF-8 string");
println!("decrypted: {}\n", dec_str);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment