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<br/>text = "Hello, World!"<br/>md5_hash = hashlib.md5(text.encode()).hexdigest()<br/>print(md5_hash) # output: MD5 hash value<br/># Use case: quick message digest, not suitable for high‑security scenarios.2. SHA‑256 Hashing
sha256_hash = hashlib.sha256(text.encode()).hexdigest()<br/>print(sha256_hash) # output: SHA‑256 hash value<br/># Use case: provides stronger security than MD5.3. Base64 Encoding
import base64<br/>encoded_text = base64.b64encode(text.encode('utf-8')).decode('utf-8')<br/>print(encoded_text) # output: Base64‑encoded text<br/># Use case: convert binary data to ASCII for transmission.4. Base64 Decoding
decoded_text = base64.b64decode(encoded_text).decode('utf-8')<br/>print(decoded_text) # output: original text<br/># Use case: reverse of Base64 encoding.5. AES Symmetric Encryption (CBC mode)
from Crypto.Cipher import AES<br/>from Crypto.Util.Padding import pad<br/>key = b'Sixteen byte key'<br/>cipher = AES.new(key, AES.MODE_CBC)<br/>ct_bytes = cipher.encrypt(pad(text.encode('utf-8'), AES.block_size))<br/>iv = cipher.iv<br/>print(base64.b64encode(iv + ct_bytes).decode('utf-8')) # output: encrypted text<br/># Use case: efficient encryption of large data with a single shared key.6. AES Decryption
from Crypto.Util.Padding import unpad<br/>ct = base64.b64decode(ct_encoded)<br/>iv = ct[:AES.block_size]<br/>cipher = AES.new(key, AES.MODE_CBC, iv)<br/>pt = unpad(cipher.decrypt(ct[AES.block_size:]), AES.block_size).decode('utf-8')<br/>print(pt) # output: original text<br/># Use case: paired with AES encryption.7. RSA Asymmetric Encryption
from Crypto.PublicKey import RSA<br/>from Crypto.Cipher import PKCS1_OAEP<br/>public_key = RSA.import_key(open('public.pem').read())<br/>cipher_rsa = PKCS1_OAEP.new(public_key)<br/>encrypted_data = cipher_rsa.encrypt(text.encode('utf-8'))<br/>print(base64.b64encode(encrypted_data).decode('utf-8')) # output: encrypted text<br/># Use case: encrypt small data or exchange symmetric keys.8. RSA Decryption
private_key = RSA.import_key(open('private.pem').read())<br/>cipher_rsa = PKCS1_OAEP.new(private_key)<br/>decrypted_data = cipher_rsa.decrypt(base64.b64decode(encrypted_data)).decode('utf-8')<br/>print(decrypted_data) # output: original text<br/># Use case: paired with RSA encryption.9. HMAC‑SHA256
hmac_sha256 = hmac.new(key, text.encode(), hashlib.sha256).hexdigest()<br/>print(hmac_sha256) # output: HMAC‑SHA256 hash value<br/># Use case: data integrity and authentication.10. Fernet Symmetric Encryption
from cryptography.fernet import Fernet<br/>key = Fernet.generate_key()<br/>cipher_suite = Fernet(key)<br/>cipher_text = cipher_suite.encrypt(text.encode('utf-8'))<br/>print(cipher_text.decode()) # output: encrypted text<br/># Use case: simple, high‑level symmetric encryption for config files, etc.11. Fernet Decryption
plain_text = cipher_suite.decrypt(cipher_text).decode('utf-8')<br/>print(plain_text) # output: original text<br/># Use case: paired with Fernet encryption.12. PBKDF2 Password Hashing
salt = os.urandom(16)<br/>hashed_password = hashlib.pbkdf2_hmac('sha256', b'password', salt, 100000)<br/>print(salt + hashed_password) # output: salt + hash<br/># Use case: securely store user passwords.13. Blowfish Encryption
from Crypto.Cipher import Blowfish<br/>bs = Blowfish.block_size<br/>cipher = Blowfish.new(key, Blowfish.MODE_CBC)<br/>plaintext = b'*' * bs # pad to block size<br/>msg = cipher.encrypt(plaintext)<br/>print(msg) # output: encrypted text<br/># Use case: older but still effective symmetric algorithm.14. Blowfish Decryption
decrypted_msg = cipher.decrypt(msg)<br/>print(decrypted_msg) # output: original text<br/># 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<br/># Use case: simple demonstration, not for production.16. XOR Decryption
decrypted = xor_encrypt(encrypted, 'secret') # same key decrypts
print(decrypted) # output: original text<br/># 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.Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.
