· 8 years ago · Dec 14, 2017, 03:24 PM
1public class EncryptionUtils {
2
3 private static final Logger LOGGER = Logger.getLogger(EncryptionUtils.class);
4 private static final String BLOWFISH = "Blowfish";
5
6
7 public static String decryptString(String toDencrypt, String algorythm) {
8 String key = "DesireSecretKey"; //will be moved to env variables
9 byte[] result = doCrypto(toDencrypt.getBytes(), algorythm, key);
10 return new String(result, StandardCharsets.UTF_8);
11 }
12
13 public static String decryptBlowFishString(String toDecrypt) {
14 return decryptString(toDecrypt, BLOWFISH);
15 }
16
17 private static byte[] doCrypto(byte[] bytesToEncrypt, String algorythm, String keyString) {
18 try {
19 Key secretKey = new SecretKeySpec(keyString.getBytes(), algorythm);
20 Cipher cipher = Cipher.getInstance(algorythm);
21 cipher.init(Cipher.DECRYPT_MODE, secretKey);
22
23 return cipher.doFinal(bytesToEncrypt);
24 } catch (NoSuchAlgorithmException | NoSuchPaddingException |
25 InvalidKeyException | BadPaddingException | IllegalBlockSizeException ex) {
26 LOGGER.error("Error occurred during decryption process", ex);
27 throw new ServerException("Error occurred during decryption process", ex);
28 }
29 }
30
31}