@@ -49,28 +49,28 @@ public class AES {
4949 private static final Logger LOGGER = LoggerFactory .getLogger (AES .class );
5050 private static final String ALGORITHM = "AES/CBC/PKCS5Padding" ;
5151 private static final String KEYSTORE_PATH = "/samples.ks" ;
52- /**
53- * Non-secret initialization vector with 16 bytes (publicly exchanged between participants), may be a random
54- * number changed every time or a counter.
55- */
56- private static final byte [] INITIALIZATION_VECTOR = {3 , 1 , 4 , 1 , 5 , 9 , 2 , 6 , 5 , 3 , 5 , 8 , 9 , 7 , 9 , 3 };
52+ private Cipher cipher ;
5753
5854 public static void main (String [] args ) {
59- AES ses = new AES ();
55+ AES aes = new AES ();
56+ aes .encrypt ();
57+ }
58+
59+ private void encrypt () {
6060 final String initialText = "AES encryption sample text" ;
6161 final char [] keystorePassword = "samples" .toCharArray ();
6262 final String keyAlias = "symmetric-sample" ;
6363 final char [] keyPassword = "symmetric-sample" .toCharArray ();
64- final IvParameterSpec iv = new IvParameterSpec (INITIALIZATION_VECTOR );
6564
6665 try {
67- KeyStore ks = ses .loadKeystore (KEYSTORE_PATH , keystorePassword );
68- Key key = ses .loadKey (ks , keyAlias , keyPassword );
66+ cipher = Cipher .getInstance (ALGORITHM );
67+ KeyStore ks = loadKeystore (KEYSTORE_PATH , keystorePassword );
68+ Key key = loadKey (ks , keyAlias , keyPassword );
6969 SecretKeySpec secretKeySpec = new SecretKeySpec (key .getEncoded (), "AES" );
70- byte [] ciphertext = ses . encrypt (secretKeySpec , iv , initialText );
71- byte [] plaintext = ses . decrypt (secretKeySpec , iv , ciphertext );
70+ byte [] ciphertext = encrypt (secretKeySpec , initialText );
71+ byte [] plaintext = decrypt (secretKeySpec , ciphertext );
7272
73- ses . printReadableMessages (initialText , ciphertext , plaintext );
73+ printReadableMessages (initialText , ciphertext , plaintext );
7474 } catch (NoSuchPaddingException | NoSuchAlgorithmException | IllegalBlockSizeException | BadPaddingException |
7575 KeyStoreException | CertificateException | UnrecoverableKeyException |
7676 InvalidAlgorithmParameterException |
@@ -98,19 +98,17 @@ private Key loadKey(KeyStore ks, String keyAlias, char[] keyPassword) throws Key
9898 return ks .getKey (keyAlias , keyPassword );
9999 }
100100
101- private byte [] encrypt (SecretKeySpec secretKeySpec , IvParameterSpec initialVector , String initialText )
102- throws NoSuchPaddingException , NoSuchAlgorithmException , UnsupportedEncodingException , BadPaddingException ,
101+ private byte [] encrypt (SecretKeySpec secretKeySpec , String initialText ) throws NoSuchPaddingException ,
102+ NoSuchAlgorithmException , UnsupportedEncodingException , BadPaddingException ,
103103 IllegalBlockSizeException , InvalidAlgorithmParameterException , InvalidKeyException {
104- Cipher cipher = Cipher .getInstance (ALGORITHM );
105- cipher .init (Cipher .ENCRYPT_MODE , secretKeySpec , initialVector );
104+ cipher .init (Cipher .ENCRYPT_MODE , secretKeySpec );
106105 return cipher .doFinal (initialText .getBytes ("UTF-8" ));
107106 }
108107
109- private byte [] decrypt (SecretKeySpec secretKeySpec , IvParameterSpec initialVector , byte [] ciphertext )
110- throws NoSuchPaddingException , NoSuchAlgorithmException , BadPaddingException , IllegalBlockSizeException ,
108+ private byte [] decrypt (SecretKeySpec secretKeySpec , byte [] ciphertext ) throws NoSuchPaddingException ,
109+ NoSuchAlgorithmException , BadPaddingException , IllegalBlockSizeException ,
111110 InvalidAlgorithmParameterException , InvalidKeyException {
112- Cipher cipher = Cipher .getInstance (ALGORITHM );
113- cipher .init (Cipher .DECRYPT_MODE , secretKeySpec , initialVector );
111+ cipher .init (Cipher .DECRYPT_MODE , secretKeySpec , new IvParameterSpec (cipher .getIV ()));
114112 return cipher .doFinal (ciphertext );
115113 }
116114
0 commit comments