11package com .example .crypto .artifacts ;
2+
3+ import java .security .*;
24import javax .crypto .Cipher ;
35import javax .crypto .KeyGenerator ;
46import javax .crypto .SecretKey ;
57import javax .crypto .spec .IvParameterSpec ;
6- import javax .crypto .spec .GCMParameterSpec ;
7- import java .security .*;
8- import java .util .Base64 ;
9- import java .util .Properties ;
10- import java .util .Random ;
11- import java .io .FileInputStream ;
12- import java .io .IOException ;
13- import java .util .Arrays ;
148
159public class Test {
1610
17- public static SecretKey generateAESKey ()throws Exception {
11+ public static SecretKey generateAESKey () throws Exception {
1812 KeyGenerator keyGen = KeyGenerator .getInstance ("AES" );
1913 keyGen .init (256 );
2014 return keyGen .generateKey ();
2115 }
2216
23-
24- private static byte [] getRandomWrapper1 ()throws Exception {
17+ private static byte [] getRandomWrapper1 () throws Exception {
2518 byte [] val = new byte [16 ];
2619 new SecureRandom ().nextBytes (val );
2720 return val ;
2821 }
2922
30- private static byte [] getRandomWrapper2A ()throws Exception {
23+ private static byte [] getRandomWrapper2A () throws Exception {
3124 byte [] val ;
32- val = getRandomWrapper1 ();
25+ val = getRandomWrapper1 ();
3326 funcA1 (val );
3427 return val ;
3528 }
3629
37- private static byte [] getRandomWrapper2b ()throws Exception {
30+ private static byte [] getRandomWrapper2b () throws Exception {
3831 byte [] val ;
39- val = getRandomWrapper1 ();
32+ val = getRandomWrapper1 ();
4033 return val ;
4134 }
4235
43- private static void funcA1 (byte [] iv )throws Exception {
36+ private static void funcA1 (byte [] iv ) throws Exception {
4437 IvParameterSpec ivSpec = new IvParameterSpec (iv );
4538 Cipher cipher = Cipher .getInstance ("AES/CBC/PKCS5Padding" );
4639 SecretKey key = generateAESKey ();
4740 cipher .init (Cipher .ENCRYPT_MODE , key , ivSpec ); // BAD: Reuse of `iv` in funcB1
4841 byte [] ciphertext = cipher .doFinal ("Simple Test Data" .getBytes ());
4942 }
5043
51- private static void funcB1 ()throws Exception {
44+ private static void funcB1 () throws Exception {
5245 byte [] iv = getRandomWrapper2A ();
5346 IvParameterSpec ivSpec = new IvParameterSpec (iv );
5447 Cipher cipher = Cipher .getInstance ("AES/CBC/PKCS5Padding" );
@@ -57,7 +50,7 @@ private static void funcB1()throws Exception {
5750 byte [] ciphertext = cipher .doFinal ("Simple Test Data" .getBytes ());
5851 }
5952
60- private static void funcA2 ()throws Exception {
53+ private static void funcA2 () throws Exception {
6154 byte [] iv = getRandomWrapper2b ();
6255 IvParameterSpec ivSpec = new IvParameterSpec (iv );
6356 Cipher cipher = Cipher .getInstance ("AES/CBC/PKCS5Padding" );
@@ -66,7 +59,7 @@ private static void funcA2()throws Exception {
6659 byte [] ciphertext = cipher .doFinal ("Simple Test Data" .getBytes ());
6760 }
6861
69- private static void funcB2 ()throws Exception {
62+ private static void funcB2 () throws Exception {
7063 byte [] iv = getRandomWrapper2b ();
7164 IvParameterSpec ivSpec = new IvParameterSpec (iv );
7265 Cipher cipher = Cipher .getInstance ("AES/CBC/PKCS5Padding" );
@@ -75,7 +68,6 @@ private static void funcB2()throws Exception {
7568 byte [] ciphertext = cipher .doFinal ("Simple Test Data" .getBytes ());
7669 }
7770
78-
7971 private static void funcA3 () throws Exception {
8072 byte [] iv = getRandomWrapper2b ();
8173 IvParameterSpec ivSpec1 = new IvParameterSpec (iv );
@@ -91,16 +83,12 @@ private static void funcA3() throws Exception {
9183 byte [] ciphertext2 = cipher2 .doFinal ("Simple Test Data" .getBytes ());
9284 }
9385
94-
95-
96-
9786 public static void main (String [] args ) {
98- try {
87+ try {
9988 funcA2 ();
10089 funcB1 ();
10190 funcB2 ();
102- }
103- catch (Exception e ) {
91+ } catch (Exception e ) {
10492 e .printStackTrace ();
10593 }
10694 }
0 commit comments