@@ -115,16 +115,17 @@ public EncryptionResult encrypt(EncryptionRequest request) throws CryptoExceptio
115115 .getJavaName ()), encryptIv );
116116 ciphertext = encryptCipher .update (request .getPlainText ());
117117 if (!useExplicitIv ) {
118- setNextIv (ciphertext );
118+ setNextEncryptIv (ciphertext );
119119 }
120+ LOGGER .debug ("encryptIv: " + ArrayConverter .bytesToHexString (encryptIv .getIV ()));
120121 return new EncryptionResult (encryptIv .getIV (), ciphertext , useExplicitIv );
121122
122123 } catch (InvalidKeyException | InvalidAlgorithmParameterException ex ) {
123124 throw new CryptoException (ex );
124125 }
125126 }
126127
127- private void setNextIv (byte [] ciphertext ) {
128+ private void setNextEncryptIv (byte [] ciphertext ) {
128129 encryptIv = new IvParameterSpec (Arrays .copyOfRange (ciphertext ,
129130 ciphertext .length - encryptCipher .getBlockSize (), ciphertext .length ));
130131 }
@@ -145,23 +146,35 @@ public byte[] decrypt(byte[] data) throws CryptoException {
145146 ConnectionEndType localConEndType = context .getConnection ().getLocalConnectionEndType ();
146147 if (useExplicitIv ) {
147148 decryptIv = new IvParameterSpec (Arrays .copyOf (data , decryptCipher .getBlockSize ()));
149+ LOGGER .debug ("decryptionIV: " + ArrayConverter .bytesToHexString (decryptIv .getIV ()));
150+
148151 decryptCipher .init (Cipher .DECRYPT_MODE , new SecretKeySpec (getKeySet ().getReadKey (localConEndType ),
149152 bulkCipherAlg .getJavaName ()), decryptIv );
150153 plaintext = decryptCipher .doFinal (Arrays .copyOfRange (data , decryptCipher .getBlockSize (), data .length ));
151154 } else {
152155 decryptIv = new IvParameterSpec (getDecryptionIV ());
156+ LOGGER .debug ("decryptionIV: " + ArrayConverter .bytesToHexString (decryptIv .getIV ()));
157+
153158 decryptCipher .init (Cipher .DECRYPT_MODE , new SecretKeySpec (getKeySet ().getReadKey (localConEndType ),
154159 bulkCipherAlg .getJavaName ()), decryptIv );
155160 plaintext = decryptCipher .doFinal (data );
161+
162+ // Set next IV
163+ setNextDecryptIv (data );
156164 }
157- LOGGER . debug ( "decryptionIV: " + ArrayConverter . bytesToHexString ( decryptIv . getIV ()));
165+
158166 return plaintext ;
159167 } catch (BadPaddingException | IllegalBlockSizeException | InvalidAlgorithmParameterException
160168 | InvalidKeyException | UnsupportedOperationException ex ) {
161169 throw new CryptoException (ex );
162170 }
163171 }
164172
173+ private void setNextDecryptIv (byte [] ciphertext ) {
174+ decryptIv = new IvParameterSpec (Arrays .copyOfRange (ciphertext ,
175+ ciphertext .length - decryptCipher .getBlockSize (), ciphertext .length ));
176+ }
177+
165178 @ Override
166179 public int getMacLength () {
167180 return readMac .getMacLength ();
0 commit comments