Weak or risky cryptographic algorithms

Weak cryptographic algorithms and unsafe cipher modes

Description

DES has a short key, and the 64-bit blocks used by DES and 3DES risk collisions at high data volumes. Retire older ciphers such as RC2, RC4 and IDEA. ECB exposes repeated plaintext blocks, while MD5 and SHA-1 lack collision resistance.

Potential impact

Weak encryption can expose data. Hash collisions can undermine signatures, and fast hashing makes stored passwords easier to guess. These choices may also fall short of applicable NIST or PCI DSS requirements.

Remediation

  • Encrypt with AES-128 or stronger in GCM or CCM mode. If CBC is required, authenticate the IV and ciphertext together with a separate key, and verify the tag before decryption. Generate IVs, nonces and salts with SecureRandom and never reuse an IV or nonce with the same key.
  • Use SHA-256 or stronger where collision resistance matters. Replace SHA1withRSA and MD5withRSA signatures with SHA256withRSA using at least a 2048-bit key, or ECDSA using P-256 or stronger.
  • Store passwords using Argon2id, scrypt, bcrypt or PBKDF2 with a per-password salt and adequate cost. For PBKDF2-HMAC-SHA256, start at 600,000 iterations or more and measure performance on your server.
  • Restrict algorithms and key sizes in code and configuration. Protect and rotate keys, using a KMS or HSM where appropriate, and keep cryptographic libraries and providers up to date.

Examples

Before

java
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import java.security.MessageDigest;
import java.security.PrivateKey;
import java.security.Signature;

public class LegacyCrypto {
    // Insecure: ECB exposes plaintext patterns
    public byte[] encryptEcb(byte[] plaintext, SecretKey key) throws Exception {
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, key);
        return cipher.doFinal(plaintext);
    }

    // Insecure: MD5 is collision-prone and fast to brute-force
    public byte[] weakDigest(byte[] data) throws Exception {
        MessageDigest md = MessageDigest.getInstance("MD5");
        return md.digest(data);
    }

    // Insecure: SHA1-based signature is deprecated
    public byte[] signSha1(byte[] data, PrivateKey privKey) throws Exception {
        Signature sig = Signature.getInstance("SHA1withRSA");
        sig.initSign(privKey);
        sig.update(data);
        return sig.sign();
    }
}

After

java
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.GCMParameterSpec;
import javax.crypto.spec.PBEKeySpec;
import java.nio.ByteBuffer;
import java.security.PrivateKey;
import java.security.SecureRandom;
import java.security.Signature;
import java.util.Base64;

public class ModernCrypto {
    // Secure: AES-GCM (AEAD) with random 96-bit nonce, 128-bit tag
    public byte[] encryptGcm(byte[] plaintext, SecretKey key) throws Exception {
        byte[] nonce = new byte[12];
        SecureRandom.getInstanceStrong().nextBytes(nonce);
        Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
        GCMParameterSpec spec = new GCMParameterSpec(128, nonce);
        cipher.init(Cipher.ENCRYPT_MODE, key, spec);
        byte[] ct = cipher.doFinal(plaintext);
        return ByteBuffer.allocate(nonce.length + ct.length).put(nonce).put(ct).array();
    }

    // Secure password hashing with PBKDF2 (salt + high iteration)
    public String hashPassword(char[] password) throws Exception {
        byte[] salt = new byte[16];
        SecureRandom.getInstanceStrong().nextBytes(salt);
        PBEKeySpec spec = new PBEKeySpec(password, salt, 600_000, 256);
        SecretKeyFactory skf = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");
        byte[] hash = skf.generateSecret(spec).getEncoded();
        return Base64.getEncoder().encodeToString(salt) + ":" + Base64.getEncoder().encodeToString(hash);
    }

    // Strong signature with SHA-256
    public byte[] signStrong(byte[] data, PrivateKey privKey) throws Exception {
        Signature sig = Signature.getInstance("SHA256withRSA");
        sig.initSign(privKey);
        sig.update(data);
        return sig.sign();
    }
}

The example uses getInstanceStrong to generate a 12-byte nonce and prefixes it to the GCM ciphertext and tag. The 128-bit tag detects tampering. Handle replay separately in the protocol. The other methods return the 16-byte salt with the password hash and sign data with SHA256withRSA.

References