Practical Cryptography for Developers
master-zh
master-zh
  • Welcome
  • 前言
  • 密码学——概述
  • 哈希函数
    • 加密哈希和碰撞
    • 哈希函数:应用场景
    • 安全哈希算法
    • 哈希函数——示例
    • 练习:计算哈希值
    • 工作量证明(Proof-of-Work)哈希函数
  • MAC 和密钥派生
    • HMAC 与密钥派生
    • HMAC 计算——示例
    • 练习:计算 HMAC
    • KDF: Deriving Key from Password
    • PBKDF2
    • Modern Key Derivation Functions
    • Scrypt
    • Bcrypt
    • Linux crypt()
    • Argon2
    • Secure Password Storage
    • Exercises: Password Encryption
  • Secure Random Generators
    • Pseudo-Random Numbers - Examples
    • Secure Random Generators (CSPRNG)
    • Exercises: Pseudo-Random Generator
  • Key Exchange and DHKE
    • Diffie–Hellman Key Exchange
    • DHKE - Examples
    • Exercises: DHKE Key Exchange
  • Encryption: Symmetric and Asymmetric
  • Symmetric Key Ciphers
    • Cipher Block Modes
    • Popular Symmetric Algorithms
    • The AES Cipher - Concepts
    • AES Encrypt / Decrypt - Examples
    • Ethereum Wallet Encryption
    • Exercises: AES Encrypt / Decrypt
    • ChaCha20-Poly1305
    • Exercises: ChaCha20-Poly1305
  • Asymmetric Key Ciphers
    • The RSA Cryptosystem - Concepts
    • RSA Encrypt / Decrypt - Examples
    • Exercises: RSA Encrypt / Decrypt
    • Elliptic Curve Cryptography (ECC)
    • ECDH Key Exchange
    • ECDH Key Exchange - Examples
    • Exercises: ECDH Key Exchange
    • ECC Encryption / Decryption
    • ECIES Hybrid Encryption Scheme
    • ECIES Encryption - Example
    • Exercises: ECIES Encrypt / Decrypt
  • Digital Signatures
    • RSA Signatures
    • RSA: Sign / Verify - Examples
    • Exercises: RSA Sign and Verify
    • ECDSA: Elliptic Curve Signatures
    • ECDSA: Sign / Verify - Examples
    • Exercises: ECDSA Sign and Verify
    • EdDSA and Ed25519
    • EdDSA: Sign / Verify - Examples
    • Exercises: EdDSA Sign and Verify
  • Quantum-Safe Cryptography
    • Quantum-Safe Signatures - Example
    • Quantum-Safe Key Exchange - Example
    • Quantum-Safe Asymmetric Encryption - Example
  • More Cryptographic Concepts
    • Digital Certificates - Example
    • TLS - Example
    • One-Time Passwords (OTP) - Example
  • Crypto Libraries for Developers
    • JavaScript Crypto Libraries
    • Python Crypto Libraries
    • C# Crypto Libraries
    • Java Crypto Libraries
  • Conclusion
Powered by GitBook
On this page
  • ECIES Encryption
  • ECIES Decryption

Was this helpful?

  1. Asymmetric Key Ciphers

Exercises: ECIES Encrypt / Decrypt

PreviousECIES Encryption - ExampleNextDigital Signatures

Last updated 5 years ago

Was this helpful?

Write a program to encrypt / decrypt a message by public / private key using (Elliptic Curve Integrated Encryption Scheme). The encryption will require an EC public key and decryption will require the corresponding EC private key. Internally, use ECC cryptography based on a 256-bit elliptic curve by choice (e.g. brainpoolP256t1) and symmetric encryption by choice (e.g. AES-256-CTR + MAC, AES-128-GCM or ChaCha20-Poly1305), along with key-derivation function by choice (e.g. PBKDF2).

You are free to choose between writing your own ECIES implementation, following the standard or use a standard ECIES library for your language, e.g.

  • Python:

  • JavaScript:

  • C#:

  • Java:

  • C, C++, PHP, Perl:

ECIES Encryption

Write a program to encrypt a message using the ECIES hybrid encryption scheme and a 256-bit ECC public key (2 * 256 bits).

  • The input consists of the public key in hex (at the first line, uncompressed, 128 hex digits) + plaintext message for encryption (at the second line).

  • The output is the hex-encoded encrypted message. It may hold the ECC ciphertext public key + the ciphertext + MAC code + the symmetric key algorithm parameters, but this depends very much on the underlying algorithms and implementation.

Sample input:

552e2b308514b38e4989d71ed263e0af6376f65ba81a94ebb74f6fadc223ee80aa8fb710cfb445e0871cd1c1a0c1f2adb2b6eedc2a0470b04244548c5be518c8
Sample text for ECIES encryption.

Sample output:

It will be different for each program execution due to the randomness in the encryption scheme:

0442e2fba3fddba1ba9207f3276e141809782dc72529523aa1fcf35b15c4c22a9333ddacd7d64de4abd0a36138d430c50be7a98d5512cb8c2fe36ca45a0bbd7927c150ae3637c45093207531ce75e3841d4808ced85e82305d8da891708c20479388f6d4a7cde213bb36bf860c5df0077358a942eeb9a4c23e89bcc11f11

ECIES Decryption

Write a program to decrypt an encrypted message created by the program from the previous example, using the ECIES hybrid encryption scheme and a 256-bit ECC private key.

  • The input consists of the private key in hex (at the first line, 64 hex digits) + encrypted message for decryption (at the second line).

  • The output is the decrypted plaintext message. In case or decryption problem (e.g. incorrect decryption key or broken encrypted message), display Error: cannot decrypt the message.

Sample input:

27f07d3251dee39ec2c5ff800641f4d839e6f8065033e9a710ea2e519473bdd7
0442e2fba3fddba1ba9207f3276e141809782dc72529523aa1fcf35b15c4c22a9333ddacd7d64de4abd0a36138d430c50be7a98d5512cb8c2fe36ca45a0bbd7927c150ae3637c45093207531ce75e3841d4808ced85e82305d8da891708c20479388f6d4a7cde213bb36bf860c5df0077358a942eeb9a4c23e89bcc11f11

Sample output:

Sample text for ECIES encryption.

Sample input:

This example holds an incorrect decryption private key:

9ab686c269b2c58f0fca699dde09cf24e23353e56bd60095d681b23709cb0dc3
0442e2fba3fddba1ba9207f3276e141809782dc72529523aa1fcf35b15c4c22a9333ddacd7d64de4abd0a36138d430c50be7a98d5512cb8c2fe36ca45a0bbd7927c150ae3637c45093207531ce75e3841d4808ced85e82305d8da891708c20479388f6d4a7cde213bb36bf860c5df0077358a942eeb9a4c23e89bcc11f11

Sample output:

Error: cannot decrypt the message
ECIES
SECG-SEC-1
https://pypi.org/project/eciespy
https://github.com/bitchan/eccrypto
https://github.com/VirgilSecurity/virgil-sdk-crypto-net
https://github.com/Arryboom/smartbox-ecies-java
https://github.com/jedisct1/libsodium