| 49 | |
| 50 | /* Decryption Method */ |
| 51 | public static String decrypt(String strToDecrypt) |
| 52 | { |
| 53 | try |
| 54 | { |
| 55 | /* Declare a byte array. */ |
| 56 | byte[] iv = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; |
| 57 | IvParameterSpec ivspec = new IvParameterSpec(iv); |
| 58 | /* Create factory for secret keys. */ |
| 59 | SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256"); |
| 60 | /* PBEKeySpec class implements KeySpec interface. */ |
| 61 | KeySpec spec = new PBEKeySpec(SECRET_KEY.toCharArray(), SALTVALUE.getBytes(), 65536, 256); |
| 62 | SecretKey tmp = factory.generateSecret(spec); |
| 63 | SecretKeySpec secretKey = new SecretKeySpec(tmp.getEncoded(), "AES"); |
| 64 | Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING"); |
| 65 | cipher.init(Cipher.DECRYPT_MODE, secretKey, ivspec); |
| 66 | /* Retruns decrypted value. */ |
| 67 | return new String(cipher.doFinal(Base64.getDecoder().decode(strToDecrypt))); |
| 68 | } |
| 69 | catch (InvalidAlgorithmParameterException | InvalidKeyException | NoSuchAlgorithmException | InvalidKeySpecException | BadPaddingException | IllegalBlockSizeException | NoSuchPaddingException e) |
| 70 | { |
| 71 | System.out.println("Error occured during decryption: " + e.toString()); |
| 72 | } |
| 73 | return null; |
| 74 | } |
| 75 | /* Driver Code */ |
| 76 | public static void main(String[] args) |
| 77 | { |