Search in sources :

Example 1 with CryptingException

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);
    }
}
Also used : SecretKeySpec(javax.crypto.spec.SecretKeySpec) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) Cipher(javax.crypto.Cipher) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) BadPaddingException(javax.crypto.BadPaddingException) InvalidKeyException(java.security.InvalidKeyException) CryptingException(org.sklsft.commons.crypto.exception.CryptingException)

Example 2 with CryptingException

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);
    }
}
Also used : SecretKeySpec(javax.crypto.spec.SecretKeySpec) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) Cipher(javax.crypto.Cipher) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) BadPaddingException(javax.crypto.BadPaddingException) InvalidKeyException(java.security.InvalidKeyException) CryptingException(org.sklsft.commons.crypto.exception.CryptingException)

Aggregations

InvalidKeyException (java.security.InvalidKeyException)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 BadPaddingException (javax.crypto.BadPaddingException)2 Cipher (javax.crypto.Cipher)2 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)2 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)2 SecretKeySpec (javax.crypto.spec.SecretKeySpec)2 CryptingException (org.sklsft.commons.crypto.exception.CryptingException)2