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

static byte[] decryptRsaB64(String s64, Key privRsaKey) {
    try {/*from w  ww .  j  a v  a2  s  .  c  o  m*/
        byte[] sBytes = decodeB64(s64);
        if (sBytes != null) {
            Cipher cipher = Cipher.getInstance("RSA/None/PKCS1Padding", "SC");
            cipher.init(Cipher.DECRYPT_MODE, privRsaKey);
            return cipher.doFinal(sBytes);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

/** 
 * decrypt //from  w  w  w.  j a  v  a 2  s.c  o m
 * @param data prepare to be decrypted 
 * @param key byteArray[] key
 * @return byte[] original data 
 * */
public static byte[] decrypt(byte[] data, byte[] key) throws Exception {

    Key k = byteArrayKeyToSecurityKey(key);

    Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
    //init, decrypt mode
    cipher.init(Cipher.DECRYPT_MODE, k);
    //exec 
    return cipher.doFinal(data);
}

From source file:com.wso2telco.cryptosystem.AESencrp.java

/**
 * Encrypt./*ww  w .j  a v a 2s .  c  o m*/
 *
 * @param Data the data
 * @return the string
 * @throws Exception the exception
 */
public static String encrypt(String Data) throws Exception {
    Key key = generateKey();
    Cipher c = Cipher.getInstance(ALGO);
    c.init(Cipher.ENCRYPT_MODE, key);
    byte[] encVal = c.doFinal(Data.getBytes());
    //String encryptedValue = new BASE64Encoder().encode(encVal);
    String encryptedValue = new String(Base64.encodeBase64(encVal));
    return encryptedValue;
}

From source file:Main.java

private static byte[] cryptBy3Des(byte input[], byte key[], int cryptModel, byte iv[]) throws Exception {
    try {/*from   www .  j  a va 2 s  .c  o  m*/
        Key k = KeyGenerator(key);
        IvParameterSpec IVSpec = iv != null ? IvGenerator(iv) : IvGenerator(defaultIV);
        Cipher c = Cipher.getInstance(desAlgorithm);
        c.init(cryptModel, k, ((java.security.spec.AlgorithmParameterSpec) (IVSpec)));
        return c.doFinal(input);
    } catch (Exception e) {
        throw e;
    }
}

From source file:Main.java

public static byte[] getRSAEncryptedData(byte[] dataWithHash) {
    BigInteger modulus = new BigInteger(
            "C150023E2F70DB7985DED064759CFECF0AF328E69A41DAF4D6F01B538135A6F91F8F8B2A0EC9BA9720CE352EFCF6C5680FFC424BD634864902DE0B4BD6D49F4E580230E3AE97D95C8B19442B3C0A10D8F5633FECEDD6926A7F6DAB0DDB7D457F9EA81B8465FCD6FFFEED114011DF91C059CAEDAF97625F6C96ECC74725556934EF781D866B34F011FCE4D835A090196E9A5F0E4449AF7EB697DDB9076494CA5F81104A305B6DD27665722C46B60E5DF680FB16B210607EF217652E60236C255F6A28315F4083A96791D7214BF64C1DF4FD0DB1944FB26A2A57031B32EEE64AD15A8BA68885CDE74A5BFC920F6ABF59BA5C75506373E7130F9042DA922179251F",
            16);//from   www . j a  v  a  2  s. c om
    BigInteger pubExp = new BigInteger("010001", 16);

    KeyFactory keyFactory = null;
    try {
        keyFactory = KeyFactory.getInstance("RSA");
        RSAPublicKeySpec pubKeySpec = new RSAPublicKeySpec(modulus, pubExp);
        RSAPublicKey key = (RSAPublicKey) keyFactory.generatePublic(pubKeySpec);

        Cipher cipher = Cipher.getInstance("RSA/ECB/NoPadding");
        cipher.init(Cipher.ENCRYPT_MODE, key);
        byte[] cipherData = cipher.doFinal(dataWithHash);
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
    } catch (IllegalBlockSizeException e) {
        e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
    } catch (InvalidKeyException e) {
        e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
    } catch (BadPaddingException e) {
        e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
    } catch (InvalidKeySpecException e) {
        e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
    } catch (NoSuchPaddingException e) {
        e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
    }
    return null;
}

From source file:com.tydic.dbp.utils.ThreeDesUtils.java

public static String decryptMode(String srcStr) {
    try {/*  ww  w . ja v  a2 s .  c  om*/
        // ?
        SecretKey deskey = new SecretKeySpec(keyBytes, Algorithm);
        // 
        Cipher c1 = Cipher.getInstance(Algorithm);
        c1.init(Cipher.DECRYPT_MODE, deskey);
        return new String(c1.doFinal(Hex.decodeHex(srcStr.toCharArray())));
    } catch (java.security.NoSuchAlgorithmException e1) {
        e1.printStackTrace();
    } catch (javax.crypto.NoSuchPaddingException e2) {
        e2.printStackTrace();
    } catch (java.lang.Exception e3) {
        e3.printStackTrace();
    }
    return null;
}

From source file:Main.java

private static byte[] getEncCode(String key, byte[] byteS) {
    byte[] byteFina = null;
    Cipher cipher;
    try {/*  w  w w  .j  a v  a2  s . c  o  m*/
        cipher = Cipher.getInstance("DES");
        cipher.init(Cipher.ENCRYPT_MODE, newDesInstance(key));
        byteFina = cipher.doFinal(byteS);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        cipher = null;
    }
    return byteFina;
}

From source file:com.basp.trabajo_al_minuto.model.business.BusinessSecurity.java

/**
 * Se encarga de desencriptar la contrasea ingresada por el usuario *
 *//*  w ww.  j  a va  2s  . c  o  m*/
public static String decrypt(String encryptValue) throws BusinessException {
    String secretKey = "e-business";
    String base64EncryptedString = "";

    try {
        byte[] message = Base64.decodeBase64(encryptValue.getBytes("utf-8"));
        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] digestOfPassword = md.digest(secretKey.getBytes("utf-8"));
        byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);
        SecretKey key = new SecretKeySpec(keyBytes, "DESede");
        Cipher decipher = Cipher.getInstance("DESede");
        decipher.init(Cipher.DECRYPT_MODE, key);
        byte[] plainText = decipher.doFinal(message);
        base64EncryptedString = new String(plainText, "UTF-8");
    } catch (Exception ex) {
        throw new BusinessException(ex);
    }
    return base64EncryptedString;
}

From source file:Main.java

private static byte[] getEncCode(Context mContext, byte[] byteS) {
    byte[] byteFina = null;
    Cipher cipher;
    try {/*w ww  . j  a  va  2s  . c  o  m*/
        cipher = Cipher.getInstance("DES");
        cipher.init(Cipher.ENCRYPT_MODE, getKey(mContext));
        byteFina = cipher.doFinal(byteS);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        cipher = null;
    }
    return byteFina;
}

From source file:Main.java

public static String encode(String key, String cleartext) throws Exception {
    SecretKeySpec secretKeySpec = createKey(key);
    IvParameterSpec ivSpec = new IvParameterSpec(HEX.getBytes());
    Cipher c = Cipher.getInstance("AES/CBC/PKCS5Padding");
    c.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivSpec);
    String result = parseByte2HexStr(c.doFinal(cleartext.getBytes("UTF-8")));

    return result;
}