· 9 years ago · Sep 10, 2016, 09:32 AM
1public String decrypt(String encryptedString) throws EncryptionException {
2 if (encryptedString == null || encryptedString.trim().length() <= 0)
3 throw new IllegalArgumentException("encrypted string was null or empty");
4 try {
5 byte[] cleartext = Base64.decodeBase64(encryptedString);
6return decrypt(cleartext);
7 } catch (Exception e) {
8 throw new EncryptionException(e);
9 }
10 }
11
12 public String decrypt(byte[] cleartext) throws EncryptionException {
13 try {
14 SecretKey key = keyFactory.generateSecret(keySpec);
15
16 cipher.init(Cipher.DECRYPT_MODE, key);
17 byte[] ciphertext = cipher.doFinal(cleartext);
18
19 return bytes2String(ciphertext);
20 } catch (Exception e) {
21 throw new EncryptionException(e);
22 }
23 }