· 4 years ago · Oct 15, 2020, 07:46 AM
1 private String decrypt(String encrypted) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeySpecException, InvalidAlgorithmParameterException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
2 final byte[] salt = {
3 (byte) 0x1F, (byte) 0x13, (byte) 0xE5, (byte) 0xB2,
4 (byte) 0x49, (byte) 0x2C, (byte) 0xC3, (byte) 0x3C
5 };
6 final int iterationCount = 65536;
7 final int keyLength = 128;
8 final String passphare = ">>Default passphrase to encrypt passwords!<<";
9
10 KeySpec pbeKeySpec = new PBEKeySpec(passphare.toCharArray(), salt, iterationCount, keyLength);
11 SecretKeyFactory keyFac = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
12 SecretKey tmpKey = keyFac.generateSecret(pbeKeySpec);
13 SecretKey pbeKey = new SecretKeySpec(tmpKey.getEncoded(), "AES");
14
15 Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
16 int blockSize = cipher.getBlockSize();
17 byte[] iv = new byte[blockSize];
18 for (int i = 0; i < iv.length; i++) {
19 iv[i] = (byte) i;
20 }
21 AlgorithmParameterSpec ivSpec = new IvParameterSpec(iv);
22
23 Cipher pbeDcipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
24 pbeDcipher.init(Cipher.DECRYPT_MODE, pbeKey, ivSpec);
25
26 return new String(pbeDcipher.doFinal(Base64.getDecoder().decode(encrypted)));
27 }
28