Using Legacy DES Encryption
Using DES.new() from PyCryptodome which has a 56-bit effective key size.
Cryptographic weakness where code uses DES or 3DES encryption algorithms which have small key sizes (56 bits for DES) and known vulnerabilities. These algorithms are susceptible to brute-force attacks and collision attacks due to their small block size (64 bits). Modern computing power makes DES/3DES encryption easily breakable, allowing attackers to decrypt sensitive data and tamper with ciphertext integrity.
from Crypto.Cipher import DES
from Crypto.Random import get_random_bytes
# VULNERABLE: DES has only 56-bit key strength
def encrypt_data(plaintext, key):
cipher = DES.new(key, DES.MODE_ECB)
# Pad plaintext to 8-byte blocks
padded = plaintext + b'\x00' * (8 - len(plaintext) % 8)
return cipher.encrypt(padded)
# Weak: 8-byte key for DES
key = get_random_bytes(8)
encrypted = encrypt_data(b'Secret data', key)from Crypto.Cipher import AES
from Crypto.Random import get_random_bytes
# SECURE: AES-GCM with authentication
def encrypt_data(plaintext, key):
nonce = get_random_bytes(12)
cipher = AES.new(key, AES.MODE_GCM, nonce=nonce)
ciphertext, tag = cipher.encrypt_and_digest(plaintext)
return {'nonce': nonce, 'ciphertext': ciphertext, 'tag': tag}
def decrypt_data(encrypted, key):
cipher = AES.new(key, AES.MODE_GCM, nonce=encrypted['nonce'])
plaintext = cipher.decrypt_and_verify(
encrypted['ciphertext'], encrypted['tag'])
return plaintext
# Strong: 32-byte key for AES-256
key = get_random_bytes(32)
encrypted = encrypt_data(b'Secret data', key)The vulnerable code uses DES with a weak 56-bit key and ECB mode without authentication. The secure version uses AES-256 (256-bit key) with GCM mode which provides both confidentiality and authentication, preventing decryption and tampering.
Using DES.new() from PyCryptodome which has a 56-bit effective key size.
Sourcery automatically identifies cryptographic weakness from des/3des usage in pycryptodome and many other security issues in your codebase.