1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.forgerock.json.jose.jwe.handlers.encryption;
18
19 import java.security.GeneralSecurityException;
20 import java.security.InvalidAlgorithmParameterException;
21 import java.security.InvalidKeyException;
22 import java.security.Key;
23 import java.security.NoSuchAlgorithmException;
24 import java.util.logging.Level;
25 import java.util.logging.Logger;
26
27 import javax.crypto.BadPaddingException;
28 import javax.crypto.Cipher;
29 import javax.crypto.IllegalBlockSizeException;
30 import javax.crypto.NoSuchPaddingException;
31 import javax.crypto.spec.IvParameterSpec;
32 import javax.crypto.spec.SecretKeySpec;
33
34 import org.forgerock.json.jose.exceptions.JweDecryptionException;
35 import org.forgerock.json.jose.exceptions.JweEncryptionException;
36
37
38
39
40
41
42
43
44 @Deprecated
45 public abstract class AbstractEncryptionHandler implements EncryptionHandler {
46 private static final Logger LOGGER = Logger.getLogger(AbstractEncryptionHandler.class.getName());
47
48
49
50
51
52
53
54
55
56 protected byte[] encrypt(String algorithm, Key key, byte[] data) {
57 try {
58 Cipher cipher = Cipher.getInstance(algorithm);
59 cipher.init(Cipher.ENCRYPT_MODE, key);
60 return cipher.doFinal(data);
61 } catch (NoSuchAlgorithmException e) {
62 throw new JweEncryptionException("Unsupported Encryption Algorithm, " + algorithm, e);
63 } catch (IllegalBlockSizeException | InvalidKeyException | NoSuchPaddingException | BadPaddingException e) {
64 throw new JweEncryptionException(e);
65 }
66 }
67
68
69
70
71
72
73
74
75
76
77
78 protected byte[] encrypt(String algorithm, Key key, byte[] initialisationVector, byte[] data) {
79
80 try {
81 Cipher cipher = Cipher.getInstance(algorithm);
82 SecretKeySpec secretKeySpec = new SecretKeySpec(key.getEncoded(), key.getAlgorithm());
83 IvParameterSpec ivParameterSpec = new IvParameterSpec(initialisationVector);
84 cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivParameterSpec);
85 return cipher.doFinal(data);
86 } catch (NoSuchAlgorithmException e) {
87 throw new JweEncryptionException("Unsupported Encryption Algorithm, " + algorithm, e);
88 } catch (IllegalBlockSizeException | InvalidKeyException | NoSuchPaddingException | BadPaddingException
89 | InvalidAlgorithmParameterException e) {
90 throw new JweEncryptionException(e);
91 }
92 }
93
94
95
96
97
98
99
100
101
102
103 public byte[] decrypt(String algorithm, Key privateKey, byte[] data) {
104
105 try {
106 Cipher cipher = Cipher.getInstance(algorithm);
107 cipher.init(Cipher.DECRYPT_MODE, privateKey);
108 return cipher.doFinal(data);
109 } catch (GeneralSecurityException e) {
110 logDecryptionFailure(e);
111 throw new JweDecryptionException();
112 }
113 }
114
115
116
117
118
119
120
121
122
123
124
125 protected byte[] decrypt(String algorithm, Key key, byte[] initialisationVector, byte[] data) {
126
127 try {
128 Cipher cipher = Cipher.getInstance(algorithm);
129 SecretKeySpec secretKeySpec = new SecretKeySpec(key.getEncoded(), key.getAlgorithm());
130 IvParameterSpec ivParameterSpec = new IvParameterSpec(initialisationVector);
131 cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, ivParameterSpec);
132 return cipher.doFinal(data);
133 } catch (GeneralSecurityException e) {
134 logDecryptionFailure(e);
135 throw new JweDecryptionException();
136 }
137 }
138
139
140
141
142 private void logDecryptionFailure(Throwable cause) {
143 if (LOGGER.isLoggable(Level.FINE)) {
144 LOGGER.log(Level.FINE, "Decryption failed: " + cause, cause);
145 }
146 }
147 }