134 throw new InvalidJweException(
"The block encryption algorithm is null");
136 if (contentMasterKey == null) {
137 throw new InvalidJweException(
"The content master key (CMK) is null");
139 if (initializationVector == null) {
140 throw new InvalidJweException(
"The initialization vector is null");
142 if (authenticationTag == null) {
143 throw new InvalidJweException(
"The authentication tag is null");
145 if (additionalAuthenticatedData == null) {
146 throw new InvalidJweException(
"The additional authentication data is null");
152 final int MAC_SIZE_BITS = 128;
153 byte[] cipherText = Base64Util.base64urldecode(encodedCipherText);
155 KeyParameter key =
new KeyParameter(contentMasterKey);
156 AEADParameters aeadParameters =
new AEADParameters(key, MAC_SIZE_BITS, initializationVector, additionalAuthenticatedData);
157 SecretKeySpec sks =
new SecretKeySpec(contentMasterKey,
"AES");
159 BlockCipher blockCipher =
new AESEngine();
160 CipherParameters params =
new KeyParameter(sks.getEncoded());
161 blockCipher.init(
false, params);
162 GCMBlockCipher aGCMBlockCipher =
new GCMBlockCipher(blockCipher);
163 aGCMBlockCipher.init(
false, aeadParameters);
164 byte[] input =
new byte[cipherText.length + authenticationTag.length];
165 System.arraycopy(cipherText, 0, input, 0, cipherText.length);
166 System.arraycopy(authenticationTag, 0, input, cipherText.length, authenticationTag.length);
167 int len = aGCMBlockCipher.getOutputSize(input.length);
168 byte[] out =
new byte[len];
169 int outOff = aGCMBlockCipher.processBytes(input, 0, input.length, out, 0);
170 aGCMBlockCipher.doFinal(out, outOff);
172 String plaintext =
new String(out, Charset.forName(Util.UTF8_STRING_ENCODING));
177 byte[] cipherText = Base64Util.base64urldecode(encodedCipherText);
181 IvParameterSpec ivParameter =
new IvParameterSpec(initializationVector);
182 cipher.init(Cipher.DECRYPT_MODE,
new SecretKeySpec(cek,
"AES"), ivParameter);
183 byte[] decodedPlainTextBytes = cipher.doFinal(cipherText);
184 String decodedPlainText =
new String(decodedPlainTextBytes, Charset.forName(Util.UTF8_STRING_ENCODING));
187 String securedInputValue =
new String(additionalAuthenticatedData, Charset.forName(Util.UTF8_STRING_ENCODING))
188 +
"." + encodedCipherText;
193 byte[] integrityValue = mac.doFinal(securedInputValue.getBytes(Util.UTF8_STRING_ENCODING));
194 if (!Arrays.equals(integrityValue, authenticationTag)) {
195 throw new InvalidJweException(
"The authentication tag is not valid");
198 return decodedPlainText;
200 throw new InvalidJweException(
"The block encryption algorithm is not supported");
202 }
catch (InvalidCipherTextException e) {
203 throw new InvalidJweException(e);
204 }
catch (NoSuchPaddingException e) {
205 throw new InvalidJweException(e);
206 }
catch (BadPaddingException e) {
207 throw new InvalidJweException(e);
208 }
catch (InvalidAlgorithmParameterException e) {
209 throw new InvalidJweException(e);
210 }
catch (NoSuchAlgorithmException e) {
211 throw new InvalidJweException(e);
212 }
catch (IllegalBlockSizeException e) {
213 throw new InvalidJweException(e);
214 }
catch (UnsupportedEncodingException e) {
215 throw new InvalidJweException(e);
216 }
catch (NoSuchProviderException e) {
217 throw new InvalidJweException(e);
218 }
catch (InvalidKeyException e) {
219 throw new InvalidJweException(e);
220 }
catch (InvalidParameterException e) {
221 throw new InvalidJweException(e);
A128CBC_PLUS_HS256
Definition: BlockEncryptionAlgorithm.java:14
A256GCM
Definition: BlockEncryptionAlgorithm.java:17
A256CBC_PLUS_HS512
Definition: BlockEncryptionAlgorithm.java:15
BlockEncryptionAlgorithm getBlockEncryptionAlgorithm()
Definition: AbstractJweDecrypter.java:39
A128GCM
Definition: BlockEncryptionAlgorithm.java:16