use of org.sklsft.commons.crypto.exception.CryptingException in project skeleton-commons by skeleton-software-community.
the class StringEncoder method encode.
public static String encode(String plainText, String symmetricAlgorithm, byte[] key) {
SecretKeySpec secretKey = new SecretKeySpec(key, CryptoUtils.getKeyAlgorithm(symmetricAlgorithm));
try {
Cipher cipher = Cipher.getInstance(symmetricAlgorithm);
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
return Base64.encodeBase64URLSafeString(cipher.doFinal(plainText.getBytes()));
} catch (NoSuchAlgorithmException | NoSuchPaddingException | IllegalBlockSizeException | BadPaddingException | InvalidKeyException e) {
throw new CryptingException("Failed to encode " + plainText, e);
}
}
use of org.sklsft.commons.crypto.exception.CryptingException in project skeleton-commons by skeleton-software-community.
the class StringEncoder method decode.
public static String decode(String cryptedText, String symmetricAlgorithm, byte[] key) {
SecretKeySpec secretKey = new SecretKeySpec(key, CryptoUtils.getKeyAlgorithm(symmetricAlgorithm));
try {
Cipher cipher = Cipher.getInstance(symmetricAlgorithm);
cipher.init(Cipher.DECRYPT_MODE, secretKey);
return new String((cipher.doFinal(Base64.decodeBase64(cryptedText))));
} catch (NoSuchAlgorithmException | NoSuchPaddingException | IllegalBlockSizeException | BadPaddingException | InvalidKeyException e) {
throw new CryptingException("Failed to decode " + cryptedText, e);
}
}
Aggregations