Common Encryption Techniques in Python: MD5, SHA‑256, Base64, AES, RSA, HMAC‑SHA256, Fernet, PBKDF2, Blowfish, XOR
This article presents concise Python examples for a wide range of encryption and encoding methods—including MD5, SHA‑256, Base64, AES, RSA, HMAC‑SHA256, Fernet, PBKDF2, Blowfish, and XOR—explaining their usage scenarios and highlighting the importance of selecting appropriate cryptographic techniques for security.
1. MD5 Hashing
import hashlib
text = "Hello, World!"
md5_hash = hashlib.md5(text.encode()).hexdigest()
print(md5_hash) # output: MD5 hash value
# Use case: quick message digest, not suitable for high‑security scenarios.2. SHA‑256 Hashing
sha256_hash = hashlib.sha256(text.encode()).hexdigest()
print(sha256_hash) # output: SHA‑256 hash value
# Use case: provides stronger security than MD5.3. Base64 Encoding
import base64
encoded_text = base64.b64encode(text.encode('utf-8')).decode('utf-8')
print(encoded_text) # output: Base64‑encoded text
# Use case: convert binary data to ASCII for transmission.4. Base64 Decoding
decoded_text = base64.b64decode(encoded_text).decode('utf-8')
print(decoded_text) # output: original text
# Use case: reverse of Base64 encoding.5. AES Symmetric Encryption (CBC mode)
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad
key = b'Sixteen byte key'
cipher = AES.new(key, AES.MODE_CBC)
ct_bytes = cipher.encrypt(pad(text.encode('utf-8'), AES.block_size))
iv = cipher.iv
print(base64.b64encode(iv + ct_bytes).decode('utf-8')) # output: encrypted text
# Use case: efficient encryption of large data with a single shared key.6. AES Decryption
from Crypto.Util.Padding import unpad
ct = base64.b64decode(ct_encoded)
iv = ct[:AES.block_size]
cipher = AES.new(key, AES.MODE_CBC, iv)
pt = unpad(cipher.decrypt(ct[AES.block_size:]), AES.block_size).decode('utf-8')
print(pt) # output: original text
# Use case: paired with AES encryption.7. RSA Asymmetric Encryption
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_OAEP
public_key = RSA.import_key(open('public.pem').read())
cipher_rsa = PKCS1_OAEP.new(public_key)
encrypted_data = cipher_rsa.encrypt(text.encode('utf-8'))
print(base64.b64encode(encrypted_data).decode('utf-8')) # output: encrypted text
# Use case: encrypt small data or exchange symmetric keys.8. RSA Decryption
private_key = RSA.import_key(open('private.pem').read())
cipher_rsa = PKCS1_OAEP.new(private_key)
decrypted_data = cipher_rsa.decrypt(base64.b64decode(encrypted_data)).decode('utf-8')
print(decrypted_data) # output: original text
# Use case: paired with RSA encryption.9. HMAC‑SHA256
hmac_sha256 = hmac.new(key, text.encode(), hashlib.sha256).hexdigest()
print(hmac_sha256) # output: HMAC‑SHA256 hash value
# Use case: data integrity and authentication.10. Fernet Symmetric Encryption
from cryptography.fernet import Fernet
key = Fernet.generate_key()
cipher_suite = Fernet(key)
cipher_text = cipher_suite.encrypt(text.encode('utf-8'))
print(cipher_text.decode()) # output: encrypted text
# Use case: simple, high‑level symmetric encryption for config files, etc.11. Fernet Decryption
plain_text = cipher_suite.decrypt(cipher_text).decode('utf-8')
print(plain_text) # output: original text
# Use case: paired with Fernet encryption.12. PBKDF2 Password Hashing
salt = os.urandom(16)
hashed_password = hashlib.pbkdf2_hmac('sha256', b'password', salt, 100000)
print(salt + hashed_password) # output: salt + hash
# Use case: securely store user passwords.13. Blowfish Encryption
from Crypto.Cipher import Blowfish
bs = Blowfish.block_size
cipher = Blowfish.new(key, Blowfish.MODE_CBC)
plaintext = b'*' * bs # pad to block size
msg = cipher.encrypt(plaintext)
print(msg) # output: encrypted text
# Use case: older but still effective symmetric algorithm.14. Blowfish Decryption
decrypted_msg = cipher.decrypt(msg)
print(decrypted_msg) # output: original text
# Use case: paired with Blowfish encryption.15. XOR Encryption
def xor_encrypt(input_str, key):
return ''.join(chr(ord(x) ^ ord(y)) for (x, y) in zip(input_str, itertools.cycle(key)))
encrypted = xor_encrypt(text, 'secret')
print(encrypted) # output: encrypted text
# Use case: simple demonstration, not for production.16. XOR Decryption
decrypted = xor_encrypt(encrypted, 'secret') # same key decrypts
print(decrypted) # output: original text
# Use case: paired with XOR encryption.Summary
These scripts demonstrate a variety of encryption techniques; choose the appropriate method based on security requirements. Some advanced algorithms require external libraries such as pycryptodome or cryptography, which can be installed via:
pip install pycryptodome cryptography
Always understand each algorithm's security properties and follow best practices to protect your data.Test Development Learning Exchange
Test Development Learning Exchange
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.