← Java / Spring · Contents

JAVA-043 / Intermediate

Using ECB mode or DES encryption

A02:2021 Cryptographic Failures · CWE-327 Broken Crypto

Why is this a problem?

Exposes patterns or uses insufficient key length.

How to fix it

java
Cipher c = Cipher.getInstance("AES/GCM/NoPadding");
byte[] iv = new byte[12]; new SecureRandom().nextBytes(iv);  // New IV every time
c.init(Cipher.ENCRYPT_MODE, key, new GCMParameterSpec(128, iv));

Further reading