Skip to content

Encryption

Ecies encrypts data so that only the holder of a given Private Key can read it. It uses the Electrum ECIES format (BIE1). A shared secret is derived from the sender’s Private Key and the recipient’s Public Key, the data is encrypted with AES-128-CBC, and an HMAC-SHA256 checksum is added. The output carries the sender’s Public Key, so the recipient needs nothing more than their own Private Key.

final alice = SVPrivateKey.fromWIF('L1Ejc5dAigm5XrM3mNptMEsNnHzS7s51YxU7J61ewGshZTKkbmzJ');
final bob = SVPrivateKey.fromWIF('KxfxrUXSMjJQcb3JgnaaA6MqsrKQ1nBSxvhuigdKRyFiEm6BZDgG');
// Bob encrypts for Alice, using his Private Key and her Public Key.
// Both Ecies methods expect a Uint8List; utf8.encode already returns one.
final cipherText = Uint8List.fromList(
Ecies().AESEncrypt(utf8.encode('attack at dawn'), bob, alice.publicKey));
// Alice decrypts with her Private Key. Bob's Public Key travels inside the BIE1 envelope.
final List<int> plainText = Ecies().AESDecrypt(cipherText, alice);
utf8.decode(plainText); // attack at dawn

Decrypting with any other key fails the checksum, and throws:

try {
Ecies().AESDecrypt(cipherText, bob); // not the recipient
} on Exception {
// "HMAC checksum failed to validate"
}

The format interoperates with other BIE1 implementations. The tests decrypt a ciphertext produced by one of them.