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:io.syndesis.rest.v1.state.ClientSideState.java

static byte[] encrypt(final String encryptionAlgorithm, final byte[] iv, final byte[] clear,
        final SecretKey encryptionKey) {
    try {//from www.j  a v  a  2  s .  c o  m
        final Cipher cipher = Cipher.getInstance(encryptionAlgorithm);

        cipher.init(Cipher.ENCRYPT_MODE, encryptionKey, new IvParameterSpec(iv));

        return cipher.doFinal(clear);
    } catch (final GeneralSecurityException e) {
        throw new IllegalStateException("Unable to encrypt the given value", e);
    }
}

From source file:cn.mrdear.pay.util.RSAUtils.java

/**
 * //from  www.ja  v a  2s .c  o m
 * 
 * @param publicKey
 *            
 * @param data
 *            ?
 * @return 
 */
public static byte[] encrypt(PublicKey publicKey, byte[] data) {
    Assert.notNull(publicKey);
    Assert.notNull(data);

    try {
        Cipher cipher = Cipher.getInstance(TRANSFORMATION, PROVIDER);
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);
        return cipher.doFinal(data);
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e.getMessage(), e);
    } catch (NoSuchPaddingException e) {
        throw new RuntimeException(e.getMessage(), e);
    } catch (InvalidKeyException e) {
        throw new RuntimeException(e.getMessage(), e);
    } catch (IllegalBlockSizeException e) {
        throw new RuntimeException(e.getMessage(), e);
    } catch (BadPaddingException e) {
        throw new RuntimeException(e.getMessage(), e);
    }
}

From source file:cn.mrdear.pay.util.RSAUtils.java

/**
 * /* w w w  .  j  a v a 2 s.  c  o  m*/
 * 
 * @param privateKey
 *            ?
 * @param data
 *            ?
 * @return 
 */
public static byte[] decrypt(PrivateKey privateKey, byte[] data) {
    Assert.notNull(privateKey);
    Assert.notNull(data);

    try {
        Cipher cipher = Cipher.getInstance(TRANSFORMATION, PROVIDER);
        cipher.init(Cipher.DECRYPT_MODE, privateKey);
        return cipher.doFinal(data);
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e.getMessage(), e);
    } catch (NoSuchPaddingException e) {
        throw new RuntimeException(e.getMessage(), e);
    } catch (InvalidKeyException e) {
        throw new RuntimeException(e.getMessage(), e);
    } catch (IllegalBlockSizeException e) {
        throw new RuntimeException(e.getMessage(), e);
    } catch (BadPaddingException e) {
        throw new RuntimeException(e.getMessage(), e);
    }
}

From source file:Logic.security.java

public static String symmetricDecrypt(String text, String secretKey) {
    Cipher cipher;
    String encryptedString;/*w w  w.  j a va2s  . c  om*/
    byte[] encryptText = null;
    byte[] raw;
    SecretKeySpec skeySpec;
    try {
        raw = Base64.decodeBase64(secretKey);
        skeySpec = new SecretKeySpec(raw, "AES");
        encryptText = Base64.decodeBase64(text);
        cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.DECRYPT_MODE, skeySpec);
        encryptedString = new String(cipher.doFinal(encryptText));
    } catch (Exception e) {
        e.printStackTrace();
        return "Error";
    }
    return encryptedString;
}

From source file:Encrypt.java

private static String decrypt(String message) throws Exception {
    byte[] bytesrc = convertHexString(message);
    Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
    DESKeySpec desKeySpec = new DESKeySpec(key.getBytes("UTF-8"));
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
    SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
    IvParameterSpec iv = new IvParameterSpec(key.getBytes("UTF-8"));

    cipher.init(Cipher.DECRYPT_MODE, secretKey, iv);
    byte[] retByte = cipher.doFinal(bytesrc);
    return new String(retByte);
}

From source file:com.dc.tes.License.java

public static byte[] decrypt(byte[] src, byte[] key) throws Exception {

    // DES????/*ww  w . ja  v  a2s . c  o m*/

    SecureRandom sr = new SecureRandom();

    // ?DESKeySpec

    DESKeySpec dks = new DESKeySpec(key);

    // ?DESKeySpec??

    // SecretKey

    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");

    SecretKey securekey = keyFactory.generateSecret(dks);

    // Cipher??

    Cipher cipher = Cipher.getInstance("DES");

    // Cipher

    cipher.init(Cipher.DECRYPT_MODE, securekey, sr);

    // ??

    // ??

    return cipher.doFinal(src);

}

From source file:Main.java

/**
 * Encrypts message string using a given symmetric key.
 * @param msg Message string to encrypt.
 * @param key Key to encrypt message with.
 * @return Byte array of encrypted and encoded message.
 * @throws NoSuchAlgorithmException//  www  .jav  a  2  s . c o m
 * @throws NoSuchPaddingException
 * @throws InvalidKeyException
 * @throws IllegalBlockSizeException
 * @throws BadPaddingException
 * @throws InvalidAlgorithmParameterException
 */
public static byte[] encryptMessage(String msg, SecretKey key)
        throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException,
        BadPaddingException, InvalidAlgorithmParameterException {
    Cipher cipher = Cipher.getInstance("AES");
    byte[] init = new byte[128 / 8];
    //SecureRandom secureRandom = new SecureRandom();
    //secureRandom.nextBytes(init);
    for (int i = 0; i < 16; i++)
        init[i] = 0;
    cipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(init));
    byte[] msgBytes = msg.getBytes();
    //System.out.println(android.util.Base64.encode(msgBytes, Base64.DEFAULT));
    byte[] msgCipherBytes = cipher.doFinal(msgBytes);
    return msgCipherBytes;
}

From source file:Main.java

public static byte[] aesIGEdecrypt(byte[] tmpAESiv, byte[] tmpAesKey, byte[] data) {
    try {//from  w ww. j  ava 2  s .co  m

        ByteBuffer out = ByteBuffer.allocate(data.length);

        byte[] iv2p = Arrays.copyOfRange(tmpAESiv, 0, tmpAESiv.length / 2);
        byte[] ivp = Arrays.copyOfRange(tmpAESiv, tmpAESiv.length / 2, tmpAESiv.length);

        int len = data.length / AES_BLOCK_SIZE;

        byte[] xorInput = null;
        byte[] xorOutput = null;

        SecretKeySpec keySpec = null;
        keySpec = new SecretKeySpec(tmpAesKey, "AES");
        Cipher cipher = null;
        cipher = Cipher.getInstance("AES/ECB/NoPadding");
        cipher.init(Cipher.DECRYPT_MODE, keySpec);

        byte[] input = null;
        byte[] output = null;

        for (int i = 0; i < len; i++) {
            input = Arrays.copyOfRange(data, i * AES_BLOCK_SIZE, (i + 1) * AES_BLOCK_SIZE);
            xorInput = xor(input, ivp);
            output = cipher.doFinal(xorInput);
            xorOutput = xor(output, iv2p);
            out.put(xorOutput);

            ivp = xorOutput;
            iv2p = input;
        }
        return out.array();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (NoSuchPaddingException e) {
        e.printStackTrace();
    } catch (IllegalBlockSizeException e) {
        e.printStackTrace();
    } catch (BadPaddingException e) {
        e.printStackTrace();
    } catch (InvalidKeyException e) {
        e.printStackTrace();
    }

    return null;
}

From source file:Main.java

public static String enCrypto(String txt, String key) {

    if (txt != null && !"".equals(txt) && !"null".equals(txt)) {

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

            StringBuffer sb = new StringBuffer();
            DESKeySpec desKeySpec = new DESKeySpec(key.getBytes());
            SecretKeyFactory skeyFactory = null;
            Cipher cipher = null;
            try {
                skeyFactory = SecretKeyFactory.getInstance("DES");
                cipher = Cipher.getInstance("DES");
            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
            }
            SecretKey deskey = skeyFactory.generateSecret(desKeySpec);
            cipher.init(Cipher.ENCRYPT_MODE, deskey);
            byte[] cipherText = cipher.doFinal(txt.getBytes());
            for (int n = 0; n < cipherText.length; n++) {
                String stmp = (java.lang.Integer.toHexString(cipherText[n] & 0XFF));
                if (stmp.length() == 1) {
                    sb.append("0" + stmp);
                } else {
                    sb.append(stmp);
                }
            }
            return sb.toString().toUpperCase(Locale.US);

        } catch (Exception e) {

            e.printStackTrace();
        }
    }
    return null;
}

From source file:com.aspose.showcase.qrcodegen.web.api.util.StringEncryptor.java

public static String encrypt(String data, String password) throws Exception {

    Security.addProvider(new BouncyCastleProvider());
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding", "BC");

    final Random r = new SecureRandom();
    byte[] salt = new byte[SALT_SIZE];
    r.nextBytes(salt);/*from  w w  w  .  jav  a  2  s  .  co m*/

    SecretKeyFactory fact = SecretKeyFactory.getInstance("PBEWITHMD5AND128BITAES-CBC-OPENSSL", "BC");

    cipher.init(Cipher.ENCRYPT_MODE,
            fact.generateSecret(new PBEKeySpec(password.toCharArray(), salt, PBE_KEY_SALE_SIZE)));

    byte[] encVal = cipher.doFinal(data.getBytes());

    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    // writing encrypted data along with the salt in the format readable by
    // open ssl api
    bos.write("Salted__".getBytes());
    bos.write(salt);
    bos.write(encVal);
    String encryptedValue = new String(Base64.encode(bos.toByteArray()));
    bos.close();

    return encryptedValue;

}