Symmetric vs Asymmetric Encryption
The Memo SDK uses a hybrid encryption approach that combines both symmetric and asymmetric encryption. Understanding the difference between these two types of encryption helps explain why this approach is both secure and efficient.
Symmetric Encryption
Section titled “Symmetric Encryption”Symmetric encryption uses the same key for both encryption and decryption.
Characteristics
Section titled “Characteristics”- ✅ Same key used for both encryption and decryption
- ✅ Fast, suitable for encrypting large amounts of data
- ⚠️ Key needs secure transmission (this is why we use RSA to encrypt the AES key)
Example: AES
Section titled “Example: AES”AES (Advanced Encryption Standard) is the most common symmetric encryption algorithm:
// AES symmetric encryptionaesKey := generateKey() // Same keycipherData := aes.Encrypt(data, aesKey) // Encrypt with keyplainData := aes.Decrypt(cipherData, aesKey) // Decrypt with same keyCommon Symmetric Algorithms
Section titled “Common Symmetric Algorithms”- AES (Advanced Encryption Standard) - Used by Memo SDK
- DES, 3DES
- ChaCha20
Asymmetric Encryption
Section titled “Asymmetric Encryption”Asymmetric encryption uses a pair of keys: a public key and a private key.
Characteristics
Section titled “Characteristics”- ✅ Public and private keys are a pair, different purposes
- ✅ Public key encrypts, private key decrypts
- ✅ Private key signs, public key verifies
- ⚠️ Slow, not suitable for encrypting large amounts of data
Example: RSA
Section titled “Example: RSA”RSA is the most common asymmetric encryption algorithm:
// RSA asymmetric encryptionpublicKey, privateKey := generateKeyPair() // A key paircipherData := rsa.Encrypt(data, publicKey) // Encrypt with public keyplainData := rsa.Decrypt(cipherData, privateKey) // Decrypt with private keyCommon Asymmetric Algorithms
Section titled “Common Asymmetric Algorithms”- RSA - Used by Memo SDK
- ECC (Elliptic Curve Cryptography)
- ElGamal
Comparison
Section titled “Comparison”| Feature | Symmetric (AES) | Asymmetric (RSA) |
|---|---|---|
| Speed | ✅ Very fast | ⚠️ Slow (100-1000x slower than AES) |
| Key Management | ⚠️ Need secure key transmission | ✅ Public key can be public |
| Data Size Limit | ✅ Unlimited | ⚠️ Limited (2048-bit key can only encrypt 245 bytes) |
| Use Case | Encrypt large data | Encrypt small data, key exchange, signing |
| Key Distribution | ⚠️ Challenging | ✅ Easy (public key can be shared) |
Why Use Hybrid Encryption?
Section titled “Why Use Hybrid Encryption?”The Memo SDK uses hybrid encryption to combine the best of both worlds:
Hybrid Encryption Advantages
Section titled “Hybrid Encryption Advantages”- Use AES (symmetric) to encrypt large data → Fast
- Use RSA (asymmetric) to encrypt AES key → Solves key transmission problem
- Combines both advantages: secure and efficient
How It Works in Memo SDK
Section titled “How It Works in Memo SDK”// 1. Generate AES key (symmetric encryption)aesKey := make([]byte, 32) // Same key for encryption and decryption
// 2. Encrypt data with AES (symmetric encryption)cipherData := aes.Encrypt(data, aesKey)
// 3. Encrypt AES key with RSA public key (asymmetric encryption)encryptedKey := rsa.Encrypt(aesKey, publicKey)
// Decryption:// 1. Decrypt AES key with RSA private key (asymmetric decryption)aesKey := rsa.Decrypt(encryptedKey, privateKey)
// 2. Decrypt data with AES key (symmetric decryption)data := aes.Decrypt(cipherData, aesKey)Summary
Section titled “Summary”- Symmetric encryption (AES): Fast, efficient for large data, but requires secure key distribution
- Asymmetric encryption (RSA): Solves key distribution problem, but slow for large data
- Hybrid encryption: Best of both worlds - fast data encryption with secure key exchange
Related Topics
Section titled “Related Topics”- Learn about the complete encryption flow in the Memo SDK
- Understand GCM mode used for symmetric encryption
- Explore RSA OAEP padding used for asymmetric encryption