public static String decrypt(String str) { init(); try { byte[] dec = BASE64DecoderStream.decode(str.getBytes()); byte[] utf8 = dcipher.doFinal(dec); return new String(utf8, "UTF8"); } catch (IllegalBlockSizeException | BadPaddingException | UnsupportedEncodingException e) { System.out.println("Decrypt exception:" + e.getMessage()); } return null; }
public static String encryptString(String message, SecretKey key) throws EncryptionException { String encryptedMessage = ""; try { Cipher cipher = Cipher.getInstance(ALGO); cipher.init(Cipher.ENCRYPT_MODE, key); byte[] encryptedBytes = cipher.doFinal(message.getBytes()); encryptedMessage = Base64.encodeBase64String(encryptedBytes); } catch (IllegalBlockSizeException | BadPaddingException | InvalidKeyException | NoSuchAlgorithmException | NoSuchPaddingException | NullPointerException e) { throw new EncryptionException("Cannot encrypt this message" + '\n' + e.getMessage()); } return encryptedMessage; }