|
19 | 19 | */ |
20 | 20 | package ro.kuberam.libs.java.crypto.encrypt; |
21 | 21 |
|
| 22 | +import static java.nio.charset.StandardCharsets.UTF_8; |
| 23 | + |
22 | 24 | import java.io.ByteArrayInputStream; |
23 | 25 | import java.io.ByteArrayOutputStream; |
24 | 26 | import java.io.IOException; |
25 | 27 | import java.io.InputStream; |
26 | | -import java.nio.charset.StandardCharsets; |
27 | 28 | import java.security.InvalidAlgorithmParameterException; |
28 | 29 | import java.security.InvalidKeyException; |
29 | 30 | import java.security.NoSuchAlgorithmException; |
|
42 | 43 | import ro.kuberam.libs.java.crypto.CryptoException; |
43 | 44 | import ro.kuberam.libs.java.crypto.utils.Buffer; |
44 | 45 |
|
45 | | -import static java.nio.charset.StandardCharsets.UTF_8; |
46 | | - |
47 | 46 | /** |
48 | | - * @author <a href="mailto:[email protected]">Claudius Teodorescu</a> |
| 47 | + * @author <a href="mailto:[email protected]">Claudius |
| 48 | + * Teodorescu</a> |
49 | 49 | */ |
50 | 50 | public class SymmetricEncryption { |
51 | 51 |
|
52 | | - public static String encryptString(final String input, final String plainKey, final String transformationName, |
53 | | - final String iv, final String provider) throws CryptoException, IOException { |
54 | | - try (final InputStream bais = new ByteArrayInputStream(input.getBytes(UTF_8))) { |
55 | | - return encrypt(bais, plainKey, transformationName, iv, provider); |
56 | | - } |
57 | | - } |
58 | | - |
59 | | - public static String encrypt(final InputStream input, final String plainKey, final String transformationName, |
60 | | - final String iv, final String provider) throws CryptoException, IOException { |
61 | | - final String algorithm = (transformationName.contains("/")) ? transformationName.substring(0, transformationName.indexOf("/")) : transformationName; |
62 | | - final String actualProvider = Optional.ofNullable(provider) |
63 | | - .filter(str -> !str.isEmpty()) |
64 | | - .orElse("SunJCE"); // default to SunJCE |
65 | | - |
66 | | - final Cipher cipher; |
67 | | - try { |
68 | | - cipher = Cipher.getInstance(transformationName, actualProvider); |
69 | | - } catch (final NoSuchProviderException e) { |
70 | | - throw new CryptoException(CryptoError.NO_PROVIDER, e); |
71 | | - } catch (final NoSuchAlgorithmException e) { |
72 | | - throw new CryptoException(CryptoError.UNKNOWN_ALGORITH, e); |
73 | | - } catch (final NoSuchPaddingException e) { |
74 | | - throw new CryptoException(CryptoError.INEXISTENT_PADDING, e); |
75 | | - } |
76 | | - |
77 | | - final SecretKeySpec skeySpec = new SecretKeySpec(plainKey.getBytes(UTF_8), algorithm); |
78 | | - if (transformationName.contains("/")) { |
79 | | - final IvParameterSpec ivSpec = new IvParameterSpec(iv.getBytes(UTF_8), 0, 16); |
80 | | - try { |
81 | | - cipher.init(Cipher.ENCRYPT_MODE, skeySpec, ivSpec); |
82 | | - } catch (final InvalidAlgorithmParameterException e) { |
83 | | - throw new CryptoException(CryptoError.UNKNOWN_ALGORITH, e); |
84 | | - } catch (final InvalidKeyException e) { |
85 | | - throw new CryptoException(CryptoError.INVALID_CRYPTO_KEY, e); |
86 | | - } |
87 | | - } else { |
88 | | - try { |
89 | | - cipher.init(Cipher.ENCRYPT_MODE, skeySpec); |
90 | | - } catch (final InvalidKeyException e) { |
91 | | - throw new CryptoException(CryptoError.INVALID_CRYPTO_KEY, e); |
92 | | - } |
93 | | - } |
94 | | - |
95 | | - try { |
96 | | - final byte[] buf = new byte[Buffer.TRANSFER_SIZE]; |
97 | | - int read = -1; |
98 | | - while((read = input.read(buf)) > -1) { |
99 | | - cipher.update(buf, 0, read); |
100 | | - } |
101 | | - |
102 | | - final byte[] resultBytes = cipher.doFinal(); |
103 | | - return getString(resultBytes); |
104 | | - } catch (final IllegalBlockSizeException e) { |
105 | | - throw new CryptoException(CryptoError.BLOCK_SIZE, e); |
106 | | - } catch (final BadPaddingException e) { |
107 | | - throw new CryptoException(CryptoError.INCORRECT_PADDING, e); |
108 | | - } |
109 | | - } |
110 | | - |
111 | | - public static String decryptString(final String encryptedInput, final String plainKey, |
112 | | - final String transformationName, final String iv, final String provider) throws CryptoException, IOException { |
113 | | - try (final InputStream bais = new ByteArrayInputStream(getBytes(encryptedInput))) { |
114 | | - return decrypt(bais, plainKey, transformationName, iv, provider); |
115 | | - } |
116 | | - } |
117 | | - |
118 | | - public static String decrypt(final InputStream encryptedInput, final String plainKey, |
119 | | - final String transformationName, final String iv, final String provider) throws CryptoException, IOException { |
120 | | - final String algorithm = (transformationName.contains("/")) ? transformationName.substring(0, transformationName.indexOf("/")) : transformationName; |
121 | | - final String actualProvider = Optional.ofNullable(provider) |
122 | | - .filter(str -> !str.isEmpty()) |
123 | | - .orElse("SunJCE"); // default to SunJCE |
124 | | - |
125 | | - final Cipher cipher; |
126 | | - try { |
127 | | - cipher = Cipher.getInstance(transformationName, actualProvider); |
128 | | - } catch (final NoSuchProviderException e) { |
129 | | - throw new CryptoException(CryptoError.NO_PROVIDER, e); |
130 | | - } catch (final NoSuchAlgorithmException e) { |
131 | | - throw new CryptoException(CryptoError.UNKNOWN_ALGORITH, e); |
132 | | - } catch (final NoSuchPaddingException e) { |
133 | | - throw new CryptoException(CryptoError.INEXISTENT_PADDING, e); |
134 | | - } |
135 | | - |
136 | | - final SecretKeySpec skeySpec = new SecretKeySpec(plainKey.getBytes(StandardCharsets.UTF_8), algorithm); |
137 | | - if (transformationName.contains("/")) { |
138 | | - final IvParameterSpec ivSpec = new IvParameterSpec(iv.getBytes(StandardCharsets.UTF_8), 0, 16); |
139 | | - try { |
140 | | - cipher.init(Cipher.DECRYPT_MODE, skeySpec, ivSpec); |
141 | | - } catch (final InvalidAlgorithmParameterException e) { |
142 | | - throw new CryptoException(CryptoError.UNKNOWN_ALGORITH, e); |
143 | | - } catch (final InvalidKeyException e) { |
144 | | - throw new CryptoException(CryptoError.INVALID_CRYPTO_KEY, e); |
145 | | - } |
146 | | - } else { |
147 | | - try { |
148 | | - cipher.init(Cipher.DECRYPT_MODE, skeySpec); |
149 | | - } catch (final InvalidKeyException e) { |
150 | | - throw new CryptoException(CryptoError.INVALID_CRYPTO_KEY, e); |
151 | | - } |
152 | | - } |
153 | | - try { |
154 | | - |
155 | | - final byte[] buf = new byte[Buffer.TRANSFER_SIZE]; |
156 | | - int read = -1; |
157 | | - while((read = encryptedInput.read(buf)) > -1) { |
158 | | - cipher.update(buf, 0, read); |
159 | | - } |
160 | | - |
161 | | - final byte[] resultBytes = cipher.doFinal(); |
162 | | - return new String(resultBytes, UTF_8); |
163 | | - } catch (final IllegalBlockSizeException e) { |
164 | | - throw new CryptoException(CryptoError.BLOCK_SIZE, e); |
165 | | - } catch (final BadPaddingException e) { |
166 | | - throw new CryptoException(CryptoError.INCORRECT_PADDING, e); |
167 | | - } |
168 | | - } |
169 | | - |
170 | | - public static String getString(final byte[] bytes) { |
171 | | - final StringBuilder sb = new StringBuilder(); |
172 | | - for (int i = 0; i < bytes.length; i++) { |
173 | | - final byte b = bytes[i]; |
174 | | - sb.append((int) (0x00FF & b)); |
175 | | - if (i + 1 < bytes.length) { |
176 | | - sb.append("-"); |
177 | | - } |
178 | | - } |
179 | | - return sb.toString(); |
180 | | - } |
181 | | - |
182 | | - public static byte[] getBytes(final String str) throws IOException { |
183 | | - final StringTokenizer st = new StringTokenizer(str, "-", false); |
184 | | - try (final ByteArrayOutputStream bos = new ByteArrayOutputStream()) { |
185 | | - while (st.hasMoreTokens()) { |
186 | | - final int i = Integer.parseInt(st.nextToken()); |
187 | | - bos.write((byte) i); |
188 | | - } |
189 | | - return bos.toByteArray(); |
190 | | - } |
191 | | - } |
| 52 | + public static String encryptString(final String input, final String plainKey, final String transformationName, |
| 53 | + final String iv, final String provider) throws CryptoException, IOException { |
| 54 | + try (final InputStream bais = new ByteArrayInputStream(input.getBytes(UTF_8))) { |
| 55 | + return operation(Cipher.ENCRYPT_MODE, bais, plainKey, transformationName, iv, provider); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + public static String encryptBinary(final InputStream input, final String plainKey, final String transformationName, |
| 60 | + final String iv, final String provider) throws CryptoException, IOException { |
| 61 | + return operation(Cipher.ENCRYPT_MODE, input, plainKey, transformationName, iv, provider); |
| 62 | + } |
| 63 | + |
| 64 | + public static String decryptString(final String encryptedInput, final String plainKey, |
| 65 | + final String transformationName, final String iv, final String provider) |
| 66 | + throws CryptoException, IOException { |
| 67 | + try (final InputStream bais = new ByteArrayInputStream(getBytes(encryptedInput))) { |
| 68 | + return operation(Cipher.DECRYPT_MODE, bais, plainKey, transformationName, iv, provider); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + public static String decryptBinary(final InputStream encryptedInput, final String plainKey, |
| 73 | + final String transformationName, final String iv, final String provider) |
| 74 | + throws CryptoException, IOException { |
| 75 | + return operation(Cipher.DECRYPT_MODE, encryptedInput, plainKey, transformationName, iv, provider); |
| 76 | + } |
| 77 | + |
| 78 | + public static String operation(final int operationType, final InputStream input, final String plainKey, |
| 79 | + final String transformationName, final String iv, final String provider) |
| 80 | + throws CryptoException, IOException { |
| 81 | + final String algorithm = (transformationName.contains("/")) |
| 82 | + ? transformationName.substring(0, transformationName.indexOf("/")) |
| 83 | + : transformationName; |
| 84 | + final String actualProvider = Optional.ofNullable(provider).filter(str -> !str.isEmpty()).orElse("SunJCE"); |
| 85 | + final Cipher cipher; |
| 86 | + ByteArrayOutputStream resultBaos = new ByteArrayOutputStream(); |
| 87 | + |
| 88 | + try { |
| 89 | + cipher = Cipher.getInstance(transformationName, actualProvider); |
| 90 | + } catch (NoSuchProviderException e) { |
| 91 | + throw new CryptoException(CryptoError.NO_PROVIDER, e); |
| 92 | + } catch (NoSuchAlgorithmException e) { |
| 93 | + throw new CryptoException(CryptoError.UNKNOWN_ALGORITH, e); |
| 94 | + } catch (NoSuchPaddingException e) { |
| 95 | + throw new CryptoException(CryptoError.INEXISTENT_PADDING, e); |
| 96 | + } |
| 97 | + |
| 98 | + final SecretKeySpec skeySpec = new SecretKeySpec(plainKey.getBytes(UTF_8), algorithm); |
| 99 | + if (transformationName.contains("/")) { |
| 100 | + final IvParameterSpec ivSpec = new IvParameterSpec(iv.getBytes(UTF_8), 0, 16); |
| 101 | + try { |
| 102 | + cipher.init(operationType, skeySpec, ivSpec); |
| 103 | + } catch (InvalidAlgorithmParameterException e) { |
| 104 | + throw new CryptoException(CryptoError.UNKNOWN_ALGORITH, e); |
| 105 | + } catch (InvalidKeyException e) { |
| 106 | + throw new CryptoException(CryptoError.INVALID_CRYPTO_KEY, e); |
| 107 | + } |
| 108 | + } else { |
| 109 | + try { |
| 110 | + cipher.init(operationType, skeySpec); |
| 111 | + } catch (InvalidKeyException e) { |
| 112 | + throw new CryptoException(CryptoError.INVALID_CRYPTO_KEY, e); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + try { |
| 117 | + final byte[] buf = new byte[Buffer.TRANSFER_SIZE]; |
| 118 | + int read = -1; |
| 119 | + while ((read = input.read(buf)) > -1) { |
| 120 | + byte[] tmpBuffer = cipher.update(buf, 0, read); |
| 121 | + resultBaos.write(tmpBuffer); |
| 122 | + } |
| 123 | + |
| 124 | + byte[] finalBuffer = cipher.doFinal(); |
| 125 | + resultBaos.write(finalBuffer); |
| 126 | + |
| 127 | + return getString(resultBaos.toByteArray()); |
| 128 | + } catch (IllegalBlockSizeException e) { |
| 129 | + throw new CryptoException(CryptoError.BLOCK_SIZE, e); |
| 130 | + } catch (BadPaddingException e) { |
| 131 | + throw new CryptoException(CryptoError.INCORRECT_PADDING, e); |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + public static String getString(final byte[] bytes) { |
| 136 | + final StringBuilder sb = new StringBuilder(); |
| 137 | + for (int i = 0; i < bytes.length; i++) { |
| 138 | + final byte b = bytes[i]; |
| 139 | + sb.append((int) (0x00FF & b)); |
| 140 | + if (i + 1 < bytes.length) { |
| 141 | + sb.append("-"); |
| 142 | + } |
| 143 | + } |
| 144 | + return sb.toString(); |
| 145 | + } |
| 146 | + |
| 147 | + public static byte[] getBytes(final String str) throws IOException { |
| 148 | + final StringTokenizer st = new StringTokenizer(str, "-", false); |
| 149 | + try (final ByteArrayOutputStream bos = new ByteArrayOutputStream()) { |
| 150 | + while (st.hasMoreTokens()) { |
| 151 | + final int i = Integer.parseInt(st.nextToken()); |
| 152 | + bos.write((byte) i); |
| 153 | + } |
| 154 | + return bos.toByteArray(); |
| 155 | + } |
| 156 | + } |
192 | 157 |
|
193 | 158 | } |
194 | 159 |
|
|
0 commit comments