Skip to content
Firaenix edited this page Jul 24, 2021 · 4 revisions

Standard Use Cases:

Encrypting for yourself:

JS:

let message = Buffer.from("Hello, Bitcoin.");

// Sender + Recipient
let alice = PrivateKey.fromRandom();

let alice_secret_stuff = alice.encryptMessage(message);
let decrypted = alice.decryptMessage(alice_secret_stuff, alice.getPublicKey());

Rust:

let alice_private_key = PrivateKey::from_random();
let message = b"Hello, Bitcoin.";

let encrypted = alice_private_key.encrypt_message(message).unwrap();
let plaintext = alice_private_key.decrypt_message(&encrypted, &alice_private_key.get_public_key().unwrap()).unwrap();

Encrypting to send to a third party

JS:

let message = Buffer.from("Hello, Bitcoin.");

// Sender
let alice = PrivateKey.fromRandom();
// Recipient
let bob = PrivateKey.fromRandom();
let bob_pub = bob.getPublicKey();

// Alice does:
let message_for_bob = bob_pub.encryptMessage(message, alice);

// Bob does:
let decrypted = bob.decryptMessage(message_for_bob, alice.getPublicKey());

Rust:

// Sender
let alice_private_key = PrivateKey::from_random();
let alice_pub_key = alice_private_key.get_public_key().unwrap();

// Recipient
let bob_priv_key = PrivateKey::from_random();
let bob_pub_key = bob_priv_key.get_public_key().unwrap();
let message = b"Hello, Bitcoin.";

let encrypted = bob_pub_key.encrypt_message(message, &alice_private_key).unwrap();

let plaintext = bob_priv_key.decrypt_message(&encrypted, &alice_pub_key).unwrap();

Advanced Use Cases with Crypto Primitives:

Encrypting to send to a third party with a throwaway private key (Anonymity)

JS:

let message = Buffer.from("Hello, Bitcoin.");

// Recipient
let bob = PrivateKey.fromRandom();
let bob_pub = bob.getPublicKey();

// Alice does:
let message_for_bob = ECIES.encryptWithEphemeralKey(message, bob_pub);

// Bob does:
let decrypted = bob.decryptMessage(message_for_bob, message_for_bob.extractPublicKey());

Encrypting to without encoding a public key in the ciphertext

This may be desired if you have already established a connection with a third party but don't want middlemen to identify the parties that are communicating.

JS:

let message = Buffer.from("Hello, Bitcoin.");

// Sender
let alice = PrivateKey.fromRandom();
// Recipient
let bob = PrivateKey.fromRandom();
let bob_pub = bob.getPublicKey();

// Alice does:
let message_for_bob = ECIES.encrypt(message, alice, bob_pub, true);

// Bob does:
let decrypted = bob.decryptMessage(message_for_bob, alice.getPublicKey());

Rust:

// Sender
let alice_private_key = PrivateKey::from_random();

// Recipient
let bob_priv_key = PrivateKey::from_random();
let bob_pub_key = bob_priv_key.get_public_key().unwrap();
let message = b"Hello, Bitcoin.";

let encrypted = ECIES::encrypt(message, &alice_private_key, &bob_pub_key, true).unwrap();

let plaintext = ECIES::decrypt(&encrypted, &bob_priv_key, &alice_private_key.get_public_key().unwrap()).unwrap();

Encoding/Decoding a ECIESCiphertext from a Buffer

JS:

let message = Buffer.from("Hello, Bitcoin.");

// Recipient
let bob = PrivateKey.fromRandom();
let bob_pub = bob.getPublicKey();

// Alice does:
let message_for_bob = ECIES.encryptWithEphemeralKey(message, bob_pub);
let message_bytes = message_for_bob.toBytes();

// Bob does:
let received_message = ECIESCiphertext.fromBytes(message_bytes, true);
let decrypted = bob.decryptMessage(received_message, message_for_bob.extractPublicKey());

Rust:

// Recipient with Anonymous sender
let bob_priv_key = PrivateKey::from_random();
let bob_pub_key = bob_priv_key.get_public_key().unwrap();
let message = b"Hello, Bitcoin.";

let encrypted = ECIES::encrypt_with_ephemeral_private_key(message, &bob_pub_key).unwrap();

let encrypted_bytes = encrypted.to_bytes();

// === Send encrypted bytes to the Recipient === 

// Bob does: (set has_pub_key to true if you know that the buffer has the pubkey in it.)
let received_msg = ECIESCiphertext::from_bytes(&encrypted_bytes, true).unwrap();

let plaintext = ECIES::decrypt(&received_msg, &bob_priv_key, &encrypted.extract_public_key().unwrap()).unwrap();