Example usage for javax.crypto IllegalBlockSizeException printStackTrace

List of usage examples for javax.crypto IllegalBlockSizeException printStackTrace

Introduction

In this page you can find the example usage for javax.crypto IllegalBlockSizeException printStackTrace.

Prototype

public void printStackTrace() 

Source Link

Document

Prints this throwable and its backtrace to the standard error stream.

Usage

From source file:Main.java

public static byte[] encrypt(String clearText) {
    SecretKeySpec keySpec = new SecretKeySpec(keyBytes, "AES");
    Cipher cipher = null;//  w w  w .ja v  a 2s . c  o m
    byte[] cipherText = null;

    try {
        // init cipher
        cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.ENCRYPT_MODE, keySpec);
        cipherText = cipher.doFinal(clearText.getBytes());
    } catch (IllegalBlockSizeException e) {
        e.printStackTrace();
    } catch (BadPaddingException e) {
        e.printStackTrace();
    } catch (InvalidKeyException e) {
        e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (NoSuchPaddingException e) {
        e.printStackTrace();
    }
    return Base64.encode(cipherText, 10);
}

From source file:Main.java

public static String decrypt(byte[] cipherText) {
    SecretKeySpec keySpec = new SecretKeySpec(keyBytes, "AES");

    Cipher cipher = null;/*from   www . j  a  va  2s.  c o m*/
    byte[] clearText = null;

    try {
        // init cipher
        cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.DECRYPT_MODE, keySpec);
        clearText = cipher.doFinal(Base64.decode(new String(cipherText), 10));
    } catch (IllegalBlockSizeException e) {
        e.printStackTrace();
    } catch (BadPaddingException e) {
        e.printStackTrace();
    } catch (NoSuchPaddingException e) {
        e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (InvalidKeyException e) {// TODO Auto-generated catch block
        e.printStackTrace();
    }

    return new String(clearText);
}

From source file:adminpassword.Decryption.java

@SuppressWarnings("static-access")
public String decrypt(String encryptedText, String idKey) throws Exception {
    String password = idKey;/*from   w ww  .  j a va2  s .c  o  m*/

    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");

    //strip off the salt and iv
    ByteBuffer buffer = ByteBuffer.wrap(new Base64().decode(encryptedText));
    byte[] saltBytes = new byte[20];
    buffer.get(saltBytes, 0, saltBytes.length);
    byte[] ivBytes1 = new byte[cipher.getBlockSize()];
    buffer.get(ivBytes1, 0, ivBytes1.length);
    byte[] encryptedTextBytes = new byte[buffer.capacity() - saltBytes.length - ivBytes1.length];
    buffer.get(encryptedTextBytes);

    // Deriving the key
    SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
    PBEKeySpec spec = new PBEKeySpec(password.toCharArray(), saltBytes, 65556, 256);

    SecretKey secretKey = factory.generateSecret(spec);
    SecretKeySpec secret = new SecretKeySpec(secretKey.getEncoded(), "AES");
    cipher.init(Cipher.DECRYPT_MODE, secret, new IvParameterSpec(ivBytes1));

    byte[] decryptedTextBytes = null;
    try {
        decryptedTextBytes = cipher.doFinal(encryptedTextBytes);

    } catch (IllegalBlockSizeException e) {
        e.printStackTrace();
    } catch (BadPaddingException e) {
        e.printStackTrace();
    }

    return new String(decryptedTextBytes);
}

From source file:adminpassword.AESDemo.java

@SuppressWarnings("static-access")
public String decrypt(String encryptedText) throws Exception {

    byte[] saltBytes = salt.getBytes("UTF-8");
    byte[] encryptedTextBytes = new Base64().decodeBase64(encryptedText);

    // Derive the key
    SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
    PBEKeySpec spec = new PBEKeySpec(password.toCharArray(), saltBytes, pswdIterations, keySize);

    SecretKey secretKey = factory.generateSecret(spec);
    SecretKeySpec secret = new SecretKeySpec(secretKey.getEncoded(), "AES");

    // Decrypt the message
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    cipher.init(Cipher.DECRYPT_MODE, secret, new IvParameterSpec(ivBytes));

    byte[] decryptedTextBytes = null;
    try {//from  w ww .jav a  2  s .co m
        decryptedTextBytes = cipher.doFinal(encryptedTextBytes);
    } catch (IllegalBlockSizeException e) {
        e.printStackTrace();
    } catch (BadPaddingException e) {
        e.printStackTrace();
    }

    return new String(decryptedTextBytes);
}

From source file:gov.nih.nci.security.util.AESEncryption.java

@Override
public String decrypt(String encryptedString) throws EncryptionException {
    if (encryptedString == null || encryptedString.trim().length() <= 0)
        throw new IllegalArgumentException("encrypted string was null or empty");

    BASE64Decoder base64decoder = new BASE64Decoder();
    byte[] ciphertext;
    try {//from   w w w  .j a va  2 s  .c  o m
        ciphertext = base64decoder.decodeBuffer(encryptedString);
    } catch (IOException e1) {

        e1.printStackTrace();
        throw new EncryptionException(e1);
    }
    byte[] cleartext = null;
    try {

        cleartext = decryptCipher.doFinal(ciphertext);
    } catch (IllegalBlockSizeException e) {
        e.printStackTrace();
        throw new EncryptionException(e);
    } catch (BadPaddingException e) {
        e.printStackTrace();
        throw new EncryptionException(e);
    }
    return StringUtilities.bytes2String(cleartext);
}

From source file:com.fegor.alfresco.security.crypto.Crypto.java

/**
 * Cipher input/*from  ww w. j a  va  2 s .co  m*/
 * 
 * @param input
 *            - the cleartext file to be encrypted
 * @param output
 *            - the encrypted data file
 * @throws IOException
 * @throws IllegalBlockSizeException
 * @throws BadPaddingException
 * @throws ShortBufferException
 */
public void Cipher() throws IOException, IllegalBlockSizeException, BadPaddingException, ShortBufferException {
    try {
        this.output = eCipher.doFinal(this.input);
    } catch (IllegalBlockSizeException e) {
        e.printStackTrace();
    } catch (BadPaddingException e) {
        e.printStackTrace();
    }
}

From source file:com.fegor.alfresco.security.crypto.Crypto.java

/**
 * Decipher input//from  w  ww  .  j  a v a  2s.co  m
 * 
 * @param input
 *            - the cleartext file to be encrypted
 * @param output
 *            - the encrypted data file
 * @throws IOException
 * @throws IllegalBlockSizeException
 * @throws BadPaddingException
 * @throws ShortBufferException
 */
public void Decipher()
        throws IOException, IllegalBlockSizeException, BadPaddingException, ShortBufferException {
    try {
        this.output = deCipher.doFinal(this.input);
    } catch (IllegalBlockSizeException e) {
        e.printStackTrace();
    } catch (BadPaddingException e) {
        e.printStackTrace();
    }
}

From source file:org.mozilla.android.sync.Cryptographer.java

private static byte[] commonCrypto(Cipher cipher, byte[] inputMessage) {
    byte[] outputMessage = null;
    try {//  w  w  w .  j  a  va 2s . com
        outputMessage = cipher.doFinal(inputMessage);
    } catch (IllegalBlockSizeException e) {
        e.printStackTrace();
    } catch (BadPaddingException e) {
        e.printStackTrace();
    }
    return outputMessage;
}

From source file:org.toobsframework.util.Crypto.java

public String encrypt(String input) {
    String output = input;/*from www .j a v  a 2  s  .  c o  m*/
    try {
        byte[] enc = encipher.doFinal(input.getBytes("UTF-8"));
        output = new String(Base64.encodeBase64(enc));
    } catch (IllegalBlockSizeException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (BadPaddingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return output;
}

From source file:poisondog.security.DecryptMission.java

@Override
public String execute(String input) throws NoSuchAlgorithmException, InvalidKeyException {
    SecretKeySpec skeySpec = new SecretKeySpec(mKey.getBytes(), mAlgorithm);
    try {/*w  w w  .j  a  v  a2s  .  c  om*/
        Cipher cipher = Cipher.getInstance(mAlgorithm);
        byte[] decode = Base64.decodeBase64(input);
        cipher.init(Cipher.DECRYPT_MODE, skeySpec);
        byte[] original = cipher.doFinal(decode);
        return new String(original);
    } catch (IllegalBlockSizeException e) {
        e.printStackTrace();
    } catch (NoSuchPaddingException e) {
        e.printStackTrace();
    } catch (BadPaddingException e) {
        e.printStackTrace();
    }
    return null;
}