Example usage for javax.crypto Cipher DECRYPT_MODE

List of usage examples for javax.crypto Cipher DECRYPT_MODE

Introduction

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

Prototype

int DECRYPT_MODE

To view the source code for javax.crypto Cipher DECRYPT_MODE.

Click Source Link

Document

Constant used to initialize cipher to decryption mode.

Usage

From source file:com.miyue.util.Cryptos.java

/**
 * AES, .//from   w ww  . ja va  2  s. c om
 * 
 * @param input Hex?
 * @param key ?AES?
 * @param iv ???
 */
public static String aesDecrypt(byte[] input, byte[] key, byte[] iv) {
    byte[] decryptResult = aes(input, key, iv, Cipher.DECRYPT_MODE);
    return new String(decryptResult);
}

From source file:com.liusoft.dlog4j.upgrade.StringUtils.java

/**
 * //from w w  w  . j  a  va  2s.c o  m
 * @param src   ??
 * @param key   8?
 * @return      ??
 * @throws Exception
 */
public static byte[] decrypt(byte[] src, byte[] key) throws Exception {
    //      DES????
    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:com.tapchatapp.android.service.GCMReceiver.java

private byte[] decrypt(byte[] cipherText, byte[] key, byte[] iv) throws Exception {
    SecretKey keySpec = new SecretKeySpec(key, "AES");
    IvParameterSpec ivSpec = new IvParameterSpec(iv);
    Cipher aes = Cipher.getInstance("AES/CBC/PKCS5Padding");
    aes.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);
    return aes.doFinal(cipherText);
}

From source file:com.cubusmail.server.mail.security.MailPasswordEncryptor.java

public String decryptPassword(byte[] encryptedPassword) {

    Cipher cipher;/*from   w w  w. j a  v  a2 s.  c om*/
    try {
        log.debug("decrypt...");
        cipher = Cipher.getInstance(this.algorithm);
        cipher.init(Cipher.DECRYPT_MODE, this.keyPair.getPrivate());

        CipherInputStream cis = new CipherInputStream(new ByteArrayInputStream(encryptedPassword), cipher);
        ByteArrayOutputStream baosDecryptedData = new ByteArrayOutputStream();
        byte[] buffer = new byte[8192];
        int len = 0;
        while ((len = cis.read(buffer)) > 0) {
            baosDecryptedData.write(buffer, 0, len);
        }
        baosDecryptedData.flush();
        cis.close();

        log.debug("...finish");

        return new String(baosDecryptedData.toByteArray());
    } catch (Exception e) {
        log.error(e.getMessage(), e);
        throw new IllegalStateException(e.getMessage(), e);
    }
}

From source file:org.edeoliveira.oauth2.dropwizard.oauth2.auth.CookieEncrypter.java

public String decode(String content) throws Exception {
    Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
    cipher.init(Cipher.DECRYPT_MODE, keySpec);
    byte[] output = cipher.doFinal(Base64.decodeBase64(content));
    return new String(output, UTF8);
}

From source file:ie.peternagy.jcrypto.algo.AesWrapper.java

/**
 * Initialize the cipher//from w ww.j  ava2s. co  m
 *
 * @param isEncrypt - true >> encryption
 */
public void initCipher(boolean isEncrypt) {
    try {
        state = isEncrypt;
        cipher = Cipher.getInstance(CIPHER_ALGORITHM);
        cipher.init(isEncrypt ? Cipher.ENCRYPT_MODE : Cipher.DECRYPT_MODE, secretKey, new IvParameterSpec(iv));
    } catch (GeneralSecurityException e) {
        System.err.println(e);
        throw new RuntimeException("Invalid environment, check max key size xx", e);
    }
}

From source file:com.aqnote.shared.cryptology.symmetric.AES.java

private static void generateCipher(String rawKey) {
    try {//from  ww  w  . j a va2  s .c  o m
        SecretKeySpec keySpec = new SecretKeySpec(rawKey.getBytes(ENCODE_UTF_8), CIPHER_NAME);
        encodeCipher = Cipher.getInstance(CIPHER_NAME);
        encodeCipher.init(Cipher.ENCRYPT_MODE, keySpec);

        decodeCipher = Cipher.getInstance(CIPHER_NAME);
        byte iv[] = encodeCipher.getIV();
        IvParameterSpec ivParameterSpec = new IvParameterSpec(iv);
        decodeCipher.init(Cipher.DECRYPT_MODE, keySpec, ivParameterSpec);
    } catch (InvalidKeyException e) {
        throw new RuntimeException(e);
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    } catch (NoSuchPaddingException e) {
        throw new RuntimeException(e);
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException(e);
    } catch (InvalidAlgorithmParameterException e) {
        e.printStackTrace();
    }

}

From source file:algorithm.AesEncryption.java

@Override
public void decryptFile(File encrypted, File decrypted) {
    try {//from w  w  w .j  av  a 2  s  .  co m
        decrypt.init(Cipher.DECRYPT_MODE, secretKeySpec);

        decryptedString = new String(decrypt.doFinal(Base64.decodeBase64(buildString(encrypted))));

        writeBytes(new ByteArrayInputStream(decryptedString.getBytes(StandardCharsets.UTF_8)),
                new FileOutputStream(decrypted));
    } catch (InvalidKeyException | IllegalBlockSizeException | BadPaddingException | IOException ex) {
        Logger.getLogger(AesEncryption.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:com.drisoftie.cwdroid.util.CredentialUtils.java

private static byte[] decrypt(byte[] key, byte[] encrypted) throws NoSuchPaddingException,
        NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
    SecretKeySpec skeySpec = new SecretKeySpec(key, AES);
    Cipher cipher = getDefaultCipher();
    cipher.init(Cipher.DECRYPT_MODE, skeySpec);
    return cipher.doFinal(encrypted);
}

From source file:com.forsrc.utils.AesUtils.java

/**
 * Decrypt string.// w w  w  .ja va  2s  .c  om
 *
 * @param code the code
 * @return String string
 * @throws AesException the aes exception
 * @Title: decrypt
 * @Description:
 */
public String decrypt(String code) throws AesException {

    byte[] raw = null;
    try {
        raw = KEY.getBytes(CHARSET_ASCII);
    } catch (UnsupportedEncodingException e) {
        throw new AesException(e);
    }

    SecretKeySpec skeySpec = new SecretKeySpec(raw, SECRET_KEY);

    Cipher cipher = null;
    try {
        cipher = Cipher.getInstance(CIPHER_KEY);
    } catch (NoSuchAlgorithmException e) {
        throw new AesException(e);
    } catch (NoSuchPaddingException e) {
        throw new AesException(e);
    }

    IvParameterSpec iv = new IvParameterSpec(IV_PARAMETER.getBytes());

    try {
        cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
    } catch (InvalidKeyException e) {
        throw new AesException(e);
    } catch (InvalidAlgorithmParameterException e) {
        throw new AesException(e);
    }

    byte[] encrypted = null;
    try {
        encrypted = new Base64().decode(code);
    } catch (Exception e) {
        throw new AesException(e);
    }

    byte[] original = null;
    try {
        original = cipher.doFinal(encrypted);
    } catch (IllegalBlockSizeException e) {
        throw new AesException(e);
    } catch (BadPaddingException e) {
        throw new AesException(e);
    }

    try {
        return new String(original, CHARSET_UTF8);
    } catch (UnsupportedEncodingException e) {
        throw new AesException(e);
    }

}