121 throw new InvalidJweException(
"The block encryption algorithm is null");
123 if (contentMasterKey == null) {
124 throw new InvalidJweException(
"The content master key (CMK) is null");
126 if (initializationVector == null) {
127 throw new InvalidJweException(
"The initialization vector is null");
129 if (additionalAuthenticatedData == null) {
130 throw new InvalidJweException(
"The additional authentication data is null");
132 if (plainText == null) {
133 throw new InvalidJweException(
"The plain text to encrypt is null");
139 SecretKey secretKey =
new SecretKeySpec(contentMasterKey,
"AES");
140 KeyParameter key =
new KeyParameter(contentMasterKey);
141 final int MAC_SIZE_BITS = 128;
142 AEADParameters aeadParameters =
new AEADParameters(key, MAC_SIZE_BITS, initializationVector,
143 additionalAuthenticatedData);
145 final int macSize = aeadParameters.getMacSize() / 8;
146 BlockCipher blockCipher =
new AESEngine();
147 CipherParameters params =
new KeyParameter(secretKey.getEncoded());
148 blockCipher.init(
true, params);
149 GCMBlockCipher aGCMBlockCipher =
new GCMBlockCipher(blockCipher);
150 aGCMBlockCipher.init(
true, aeadParameters);
151 int len = aGCMBlockCipher.getOutputSize(plainText.length);
152 byte[] out =
new byte[len];
153 int outOff = aGCMBlockCipher.processBytes(plainText, 0, plainText.length, out, 0);
154 outOff += aGCMBlockCipher.doFinal(out, outOff);
155 byte[] cipherText =
new byte[outOff - macSize];
156 System.arraycopy(out, 0, cipherText, 0, cipherText.length);
157 byte[] authenticationTag =
new byte[macSize];
158 System.arraycopy(out, outOff - macSize, authenticationTag, 0, authenticationTag.length);
160 String encodedCipherText = Base64Util.base64urlencode(cipherText);
161 String encodedAuthenticationTag = Base64Util.base64urlencode(authenticationTag);
163 return new Pair<String, String>(encodedCipherText, encodedAuthenticationTag);
167 IvParameterSpec parameters =
new IvParameterSpec(initializationVector);
170 SecretKeySpec secretKeySpec =
new SecretKeySpec(cek,
"AES");
171 cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, parameters);
172 byte[] cipherText = cipher.doFinal(plainText);
174 String encodedCipherText = Base64Util.base64urlencode(cipherText);
176 String securedInputValue =
new String(additionalAuthenticatedData, Charset.forName(Util.UTF8_STRING_ENCODING))
177 +
"." + encodedCipherText;
183 byte[] integrityValue = mac.doFinal(securedInputValue.getBytes(Util.UTF8_STRING_ENCODING));
185 String encodedIntegrityValue = Base64Util.base64urlencode(integrityValue);
187 return new Pair<String, String>(encodedCipherText, encodedIntegrityValue);
189 throw new InvalidJweException(
"The block encryption algorithm is not supported");
191 }
catch (InvalidCipherTextException e) {
192 throw new InvalidJweException(e);
193 }
catch (NoSuchAlgorithmException e) {
194 throw new InvalidJweException(e);
195 }
catch (UnsupportedEncodingException e) {
196 throw new InvalidJweException(e);
197 }
catch (NoSuchProviderException e) {
198 throw new InvalidJweException(e);
199 }
catch (IllegalBlockSizeException e) {
200 throw new InvalidJweException(e);
201 }
catch (InvalidKeyException e) {
202 throw new InvalidJweException(e);
203 }
catch (BadPaddingException e) {
204 throw new InvalidJweException(e);
205 }
catch (InvalidAlgorithmParameterException e) {
206 throw new InvalidJweException(e);
207 }
catch (NoSuchPaddingException e) {
208 throw new InvalidJweException(e);
209 }
catch (InvalidParameterException e) {
210 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: AbstractJweEncrypter.java:38
A128GCM
Definition: BlockEncryptionAlgorithm.java:16