A comprehensive guide to hash, symmetric, and asymmetric encryption algorithms, including principles, use cases, and practical Python code examples to help you build secure solutions quickly.
Complete Guide to Encryption Algorithms 2025 | Hash, Symmetric, Asymmetric (with Python Code)
Deep Dive into Common Encryption Algorithms: Principles, Code, and Scenarios
In this highly digitized era, data security is the "invisible guardian" of infrastructure. This article will guide you from hash to symmetric to asymmetric encryption, using concise language and clear structure to help you understand the essence and applications of these algorithms, along with practical Python code examples.
1. Algorithm Classification Overview
"Encryption algorithms" can be divided into three categories:
| Type | Core Characteristics | Common Uses |
|---|---|---|
| Hash | One-way irreversible (plaintext → hash value) | Verification, password storage, signature foundation |
| Symmetric | Same key for encryption/decryption (fast) | File encryption, data transfer, local storage |
| Asymmetric | Public key encryption, private key decryption (secure key distribution, slower) | Secure communication, key exchange, digital signatures |
2. Hash Algorithms: Irreversible Data Fingerprints
Principle Overview
Hash algorithms map arbitrary-length inputs to fixed-length outputs ("hash values"), commonly used for verifying data integrity and password storage.
Python Example (SHA-256)
import hashlib
def hash_sha256(text: str) -> str:
return hashlib.sha256(text.encode('utf-8')).hexdigest()
print("SHA-256:", hash_sha256("hello"))Note: Avoid using MD5 and SHA-1 as they have been proven vulnerable to collision attacks.
Use Cases
- File download verification
- User password storage (combined with "salt" for better security)
- Blockchain data integrity verification
3. Symmetric Encryption: High-Speed Protection with Shared Keys
Algorithm Essence
Symmetric encryption (e.g., AES, DES) uses the same key for encryption and decryption, offering high processing efficiency but requiring secure key exchange.
Python Example (AES-128 + EAX Mode)
from Crypto.Cipher import AES
from Crypto.Random import get_random_bytes
import base64
def aes_encrypt_decrypt_demo():
key = get_random_bytes(16)
cipher = AES.new(key, AES.MODE_EAX)
pt = b"Secret message"
ct, tag = cipher.encrypt_and_digest(pt)
print("Ciphertext:", base64.b64encode(ct))
cipher2 = AES.new(key, AES.MODE_EAX, nonce=cipher.nonce)
print("Decrypted:", cipher2.decrypt(ct).decode())
aes_encrypt_decrypt_demo()Scenario Examples
- Local disk or file encryption
- Database field encryption (e.g., ID numbers, bank card numbers)
- High-speed communication encryption (e.g., VPN, internal network transfer)
4. Asymmetric Encryption: Secure Collaboration of Public and Private Keys
Why Do We Need It?
Asymmetric encryption (RSA, ECC) solves the key distribution problem in symmetric encryption through public key encryption and private key decryption. Although slower, it's more suitable for critical data exchange.
Python Example (RSA 2048-bit + OAEP Padding)
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_OAEP
def rsa_encrypt_decrypt_demo():
key = RSA.generate(2048)
pub = key.publickey()
cipher = PKCS1_OAEP.new(pub)
pt = b"Hello RSA"
ct = cipher.encrypt(pt)
print("Ciphertext:", ct)
cipher2 = PKCS1_OAEP.new(key)
print("Decrypted:", cipher2.decrypt(ct).decode())
rsa_encrypt_decrypt_demo()Typical Applications
- HTTPS/TLS key negotiation during handshake
- Digital signatures and identity verification
- Certificate issuance and Public Key Infrastructure (PKI)
5. Collaborative Security Solution: HTTPS/TLS Example
In practical applications, a single algorithm often cannot meet all security needs. Take HTTPS/TLS as an example:
- Use asymmetric encryption (e.g., RSA) to securely exchange symmetric keys.
- Use symmetric encryption (e.g., AES) to encrypt all subsequent data communication.
- Use hash algorithms (e.g., SHA-256) to ensure data integrity and prevent tampering.
This combination creates an efficient and secure communication mechanism.
6. Summary
- Hash: Generates unique digests for verification and storage, but is irreversible.
- Symmetric Encryption: Efficient but requires secure key distribution.
- Asymmetric Encryption: Secure key management and identity verification mechanism.
- Combined Use: The core concept of practical encryption systems (e.g., TLS).
7. Practical Recommendations
- For password storage, use strong hashing with "salt" (e.g., bcrypt, Argon2).
- When encrypting large amounts of data, prioritize symmetric algorithms.
- When communicating keys across networks, introduce asymmetric algorithms for security.
- Combine different encryption methods to build a complete security chain.