public String decrypt(String encryptedString) throws Exception { if (encryptedString == null || encryptedString.trim().length() <= 0) { throw new ProgramLogicError("Invalid input text:" + encryptedString); } SecretKey key = keyFactory.generateSecret(keySpec); cipher.init(Cipher.DECRYPT_MODE, key); byte[] cleartext = Base64.decodeBuffer(encryptedString); byte[] ciphertext = cipher.doFinal(cleartext); return new String(ciphertext, UNICODE_FORMAT); }
public String encrypt(String unencryptedString) throws Exception { if (unencryptedString == null || unencryptedString.trim().length() == 0) { throw new ProgramLogicError("Invalid input text:" + unencryptedString); } SecretKey key = keyFactory.generateSecret(keySpec); cipher.init(Cipher.ENCRYPT_MODE, key); byte[] cleartext = unencryptedString.getBytes(UNICODE_FORMAT); byte[] ciphertext = cipher.doFinal(cleartext); return Base64.encode(ciphertext); }