1+ import javax .crypto .Cipher ;
2+ import javax .crypto .KeyGenerator ;
3+ import javax .crypto .SecretKey ;
4+ import java .security .Key ;
5+ import java .security .NoSuchAlgorithmException ;
6+
7+ public class Test {
8+ public static void main (String [] args ) throws Exception {
9+ byte [] data = "Sensitive Data" .getBytes ();
10+
11+ // BAD: DES (unsafe)
12+ KeyGenerator desKeyGen = KeyGenerator .getInstance ("DES" ); // $Alert
13+ SecretKey desKey = desKeyGen .generateKey ();
14+ Cipher desCipher = Cipher .getInstance ("DES" ); // $Alert
15+ desCipher .init (Cipher .ENCRYPT_MODE , desKey );
16+ byte [] desEncrypted = desCipher .doFinal (data );
17+
18+ // BAD: DESede (Triple DES, considered weak)
19+ KeyGenerator desedeKeyGen = KeyGenerator .getInstance ("DESede" ); // $Alert
20+ SecretKey desedeKey = desedeKeyGen .generateKey ();
21+ Cipher desedeCipher = Cipher .getInstance ("DESede" ); // $Alert
22+ desedeCipher .init (Cipher .ENCRYPT_MODE , desedeKey );
23+ byte [] desedeEncrypted = desedeCipher .doFinal (data );
24+
25+ // BAD: Blowfish (considered weak)
26+ KeyGenerator blowfishKeyGen = KeyGenerator .getInstance ("Blowfish" ); // $Alert
27+ SecretKey blowfishKey = blowfishKeyGen .generateKey ();
28+ Cipher blowfishCipher = Cipher .getInstance ("Blowfish" ); // $Alert
29+ blowfishCipher .init (Cipher .ENCRYPT_MODE , blowfishKey );
30+ byte [] blowfishEncrypted = blowfishCipher .doFinal (data );
31+
32+ // BAD: RC2 (unsafe)
33+ KeyGenerator rc2KeyGen = KeyGenerator .getInstance ("RC2" );
34+ SecretKey rc2Key = rc2KeyGen .generateKey ();
35+ Cipher rc2Cipher = Cipher .getInstance ("RC2" ); // $Alert
36+ rc2Cipher .init (Cipher .ENCRYPT_MODE , rc2Key );
37+ byte [] rc2Encrypted = rc2Cipher .doFinal (data );
38+
39+ // BAD: RC4 (stream cipher, unsafe)
40+ KeyGenerator rc4KeyGen = KeyGenerator .getInstance ("RC4" ); // $Alert
41+ SecretKey rc4Key = rc4KeyGen .generateKey ();
42+ Cipher rc4Cipher = Cipher .getInstance ("RC4" ); // $Alert
43+ rc4Cipher .init (Cipher .ENCRYPT_MODE , rc4Key );
44+ byte [] rc4Encrypted = rc4Cipher .doFinal (data );
45+
46+ // BAD: IDEA (considered weak)
47+ KeyGenerator ideaKeyGen = KeyGenerator .getInstance ("IDEA" ); // $Alert
48+ SecretKey ideaKey = ideaKeyGen .generateKey ();
49+ Cipher ideaCipher = Cipher .getInstance ("IDEA" ); // $Alert
50+ ideaCipher .init (Cipher .ENCRYPT_MODE , ideaKey );
51+ byte [] ideaEncrypted = ideaCipher .doFinal (data );
52+
53+ // BAD: Skipjack (unsafe)
54+ KeyGenerator skipjackKeyGen = KeyGenerator .getInstance ("Skipjack" ); // $Alert
55+ SecretKey skipjackKey = skipjackKeyGen .generateKey ();
56+ Cipher skipjackCipher = Cipher .getInstance ("Skipjack" ); // $Alert
57+ skipjackCipher .init (Cipher .ENCRYPT_MODE , skipjackKey );
58+ byte [] skipjackEncrypted = skipjackCipher .doFinal (data );
59+
60+ // GOOD: AES (safe)
61+ KeyGenerator aesKeyGen = KeyGenerator .getInstance ("AES" );
62+ SecretKey aesKey = aesKeyGen .generateKey ();
63+ Cipher aesCipher = Cipher .getInstance ("AES" );
64+ aesCipher .init (Cipher .ENCRYPT_MODE , aesKey );
65+ byte [] aesEncrypted = aesCipher .doFinal (data );
66+
67+ // GOOD: AES with CBC mode and PKCS5Padding
68+ Cipher aesCbcCipher = Cipher .getInstance ("AES/CBC/PKCS5Padding" );
69+ aesCbcCipher .init (Cipher .ENCRYPT_MODE , aesKey );
70+ byte [] aesCbcEncrypted = aesCbcCipher .doFinal (data );
71+
72+ // GOOD: AES with GCM mode (authenticated encryption)
73+ Cipher aesGcmCipher = Cipher .getInstance ("AES/GCM/NoPadding" );
74+ aesGcmCipher .init (Cipher .ENCRYPT_MODE , aesKey );
75+ byte [] aesGcmEncrypted = aesGcmCipher .doFinal (data );
76+ }
77+ }
0 commit comments