Example usage for java.security NoSuchAlgorithmException printStackTrace

List of usage examples for java.security NoSuchAlgorithmException printStackTrace

Introduction

In this page you can find the example usage for java.security NoSuchAlgorithmException printStackTrace.

Prototype

public void printStackTrace(PrintStream s) 

Source Link

Document

Prints this throwable and its backtrace to the specified print stream.

Usage

From source file:com.invariantproperties.sandbox.springentitylistener.service.EncryptorBean.java

/**
 * Encrypt string. We should use a container class to contain the ciphertext
 * and key but an array is good enough for testing.
 *//*from w w w .  j a  va2 s  .  c  o  m*/
public String[] encryptString(String plaintext) {
    String ciphertext = null;
    String salt = null;

    if (plaintext != null) {
        try {
            // Encryptor encryptor = JavaEncryptor.getInstance();
            // CipherText ct = encryptor.encrypt(key, new
            // PlainText(plaintext));
            // ciphertext =
            // Base64.encodeBytes(ct.asPortableSerializedByteArray());
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
            cipher.init(Cipher.ENCRYPT_MODE, key);
            ciphertext = Base64.encodeBytes(cipher.doFinal(plaintext.getBytes()));
            salt = Base64.encodeBytes(cipher.getIV());
        } catch (NoSuchAlgorithmException e) {
            // handle exception. Perhaps set value to null?
            System.out.println("decryption exception: " + e.getMessage());
        } catch (NoSuchPaddingException e) {
            // handle exception. Perhaps set value to null?
            System.out.println("decryption exception: " + e.getMessage());
        } catch (InvalidKeyException e) {
            // handle exception. Perhaps set value to null?
            System.out.println("decryption exception: " + e.getMessage());
        } catch (BadPaddingException e) {
            // handle exception. Perhaps set value to null?
            System.out.println("decryption exception: " + e.getMessage());
        } catch (IllegalBlockSizeException e) {
            // handle exception. Perhaps set value to null?
            System.out.println("decryption exception: " + e.getMessage());
        } catch (Throwable e) {
            e.printStackTrace(System.out);
        }
    }

    return new String[] { ciphertext, salt };
}