Example usage for javax.crypto Cipher doFinal

List of usage examples for javax.crypto Cipher doFinal

Introduction

In this page you can find the example usage for javax.crypto Cipher doFinal.

Prototype

public final byte[] doFinal(byte[] input) throws IllegalBlockSizeException, BadPaddingException 

Source Link

Document

Encrypts or decrypts data in a single-part operation, or finishes a multiple-part operation.

Usage

From source file:Main.java

public static byte[] decipherAes256(byte[] encrypedPwdBytes, String password) throws NullPointerException {

    if (password == null || password.length() == 0) {
        throw new NullPointerException("Please give Password");
    }//from w  w  w  . java2  s  . c  om

    if (encrypedPwdBytes == null || encrypedPwdBytes.length <= 0) {
        throw new NullPointerException("Please give encrypedPwdBytes");
    }

    try {
        SecretKey key = getKey(password);

        // IMPORTANT TO GET SAME RESULTS ON iOS and ANDROID
        final byte[] iv = new byte[16];
        Arrays.fill(iv, (byte) 0x00);
        IvParameterSpec ivParameterSpec = new IvParameterSpec(iv);

        // cipher is not thread safe
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
        cipher.init(Cipher.DECRYPT_MODE, key, ivParameterSpec);
        byte[] decryptedValueBytes = (cipher.doFinal(encrypedPwdBytes));

        return decryptedValueBytes;

    } catch (InvalidKeyException e) {
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (BadPaddingException e) {
        e.printStackTrace();
    } catch (NoSuchPaddingException e) {
        e.printStackTrace();
    } catch (IllegalBlockSizeException e) {
        e.printStackTrace();
    } catch (InvalidAlgorithmParameterException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

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

    Cipher cipher = null;
    byte[] clearText = null;

    try {//from w  ww  .  j  a  v  a2s  .co m
        // 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:com.agiletec.aps.util.DefaultApsEncrypter.java

public static String encryptString(String plainText) throws ApsSystemException {
    String encryptedString = null;
    try {//  ww  w .ja v  a2s  .  c o m
        Key key = getKey();
        Cipher desCipher = Cipher.getInstance(TRIPLE_DES);
        desCipher.init(Cipher.ENCRYPT_MODE, key);
        byte[] cleartext = plainText.getBytes();
        byte[] ciphertext = desCipher.doFinal(cleartext);
        encryptedString = new String(Base64.encodeBase64(ciphertext));
    } catch (Throwable t) {
        throw new ApsSystemException("Error detcted while encoding a string", t);
    }
    return encryptedString;
}

From source file:com.thoughtworks.go.server.util.EncryptionHelper.java

public static String encryptUsingRSA(String plainText, String publicKeyContent)
        throws InvalidKeyException, NoSuchPaddingException, NoSuchAlgorithmException, IOException,
        BadPaddingException, IllegalBlockSizeException, InvalidKeySpecException {
    Cipher encryptCipher = Cipher.getInstance("RSA");
    encryptCipher.init(Cipher.ENCRYPT_MODE, getRSAPublicKeyFrom(publicKeyContent));
    return Base64.getEncoder().encodeToString(encryptCipher.doFinal(plainText.getBytes(UTF_8)));
}

From source file:com.kylinolap.rest.security.PasswordPlaceholderConfigurer.java

public static String decrypt(String strToDecrypt) {
    try {/*from www .ja  v  a  2 s  . co m*/
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING");
        final SecretKeySpec secretKey = new SecretKeySpec(key, "AES");
        cipher.init(Cipher.DECRYPT_MODE, secretKey);
        final String decryptedString = new String(cipher.doFinal(Base64.decodeBase64(strToDecrypt)));
        return decryptedString;
    } catch (Exception e) {
    }
    return null;
}

From source file:D_common.E_ncript.java

public static byte[] encrypt(byte[] data, String keyStr) {
    try {/*from   w ww.j  av  a2  s  .c  o  m*/
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        final SecretKeySpec secretKey = new SecretKeySpec(makeAESKey(keyStr), "AES");
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);
        return cipher.doFinal(data);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:D_common.E_ncript.java

public static byte[] decrypt(byte[] data, String keyStr) {
    try {//from  w w w  .  j  a va2  s  .  co m
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING");
        final SecretKeySpec secretKey = new SecretKeySpec(makeAESKey(keyStr), "AES");
        cipher.init(Cipher.DECRYPT_MODE, secretKey);
        return cipher.doFinal(data);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:club.jmint.crossing.specs.Security.java

public static String desDecrypt(String data, String key) throws CrossException {
    String ret = null;/*w  w  w.ja va2 s  .  c  o  m*/
    try {
        DESKeySpec desKey = new DESKeySpec(key.getBytes("UTF-8"));
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        SecretKey securekey = keyFactory.generateSecret(desKey);

        Cipher cipher = Cipher.getInstance(CIPHER_DES_ALGORITHM);
        cipher.init(Cipher.DECRYPT_MODE, securekey);

        ret = new String(cipher.doFinal(Base64.decodeBase64(data)));
    } catch (Exception e) {
        CrossLog.printStackTrace(e);
        throw new CrossException(ErrorCode.COMMON_ERR_DECRYPTION.getCode(),
                ErrorCode.COMMON_ERR_DECRYPTION.getInfo());
    }
    return ret;
}

From source file:com.juicioenlinea.application.secutiry.Security.java

public static String encriptar(String texto) {

    final String secretKey = "hunter"; //llave para encriptar datos
    String encryptedString = null;

    try {//from   ww w .j  a  v  a 2  s  .  c om

        MessageDigest md = MessageDigest.getInstance("SHA");
        byte[] digestOfPassword = md.digest(secretKey.getBytes("UTF-8"));
        byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);

        SecretKey key = new SecretKeySpec(keyBytes, "DESede");
        Cipher cipher = Cipher.getInstance("DESede");
        cipher.init(Cipher.ENCRYPT_MODE, key);

        byte[] plainTextBytes = texto.getBytes("UTF-8");
        byte[] buf = cipher.doFinal(plainTextBytes);
        byte[] base64Bytes = Base64.encodeBase64(buf);
        encryptedString = new String(base64Bytes);

    } catch (NoSuchAlgorithmException | UnsupportedEncodingException | NoSuchPaddingException
            | InvalidKeyException | IllegalBlockSizeException | BadPaddingException ex) {
        Logger.getLogger(Security.class.getName()).log(Level.SEVERE, null, ex);
    }
    return encryptedString;
}

From source file:controlpac.EncryptHelper.java

public static String Encriptar(String texto) {
    String base64EncryptedString = "";
    try {/*  w w w.ja  va  2s . c o  m*/
        MessageDigest md = MessageDigest.getInstance("MD5"); //Crea un hash con la clave elegida
        byte[] digestOfPassword = md.digest(secretKey.getBytes("utf-8"));
        byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);
        SecretKey key = new SecretKeySpec(keyBytes, "DESede"); //Crea la clave
        Cipher cipher = Cipher.getInstance("DESede");
        cipher.init(Cipher.ENCRYPT_MODE, key);//Inicializa el cifrado con la clave
        byte[] plainTextBytes = texto.getBytes("utf-8");
        byte[] buf = cipher.doFinal(plainTextBytes);//Cifra el texto
        byte[] base64Bytes = Base64.encodeBase64(buf);//Encodea el texto en base64
        base64EncryptedString = new String(base64Bytes);
    } catch (Exception ex) {
    }
    return base64EncryptedString;
}