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:enc_mods.aes.java

public String getDecryptedString(String str) {
    String decrypted = "";
    try {//  w  ww  . ja v  a 2 s  . c  om
        cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING");
        cipher.init(Cipher.DECRYPT_MODE, secretkey);
        decrypted = new String(cipher.doFinal(Base64.decodeBase64(str)));
    } catch (Exception e) {
        e.printStackTrace();
    }
    return decrypted;
}

From source file:cl.niclabs.tscrypto.common.encryption.KeyChain.java

public byte[] decrypt(String rsaKeyAlias, String encryptedData) {
    byte[] decrypted = null;

    try {/*from www.  ja  va  2 s.c  om*/
        Cipher decipher = Cipher.getInstance("RSA");
        decipher.init(Cipher.DECRYPT_MODE, getPrivateKey(rsaKeyAlias));
        decrypted = decipher.doFinal(Base64.decodeBase64(encryptedData));
    } catch (NoSuchAlgorithmException | UnrecoverableEntryException | KeyStoreException | NoSuchPaddingException
            | InvalidKeyException | IllegalBlockSizeException | BadPaddingException e) {
        e.printStackTrace();
    }
    return decrypted;
}

From source file:Conexion.newClass.java

public String Decode(String textoEncriptado) throws Exception {

    String secretKey = "mailEncrypted"; //llave para encriptar datos
    String base64EncryptedString = "";

    try {/*w ww.ja v  a 2 s. c  o m*/
        byte[] message = Base64.decodeBase64(textoEncriptado.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) {
    }
    return base64EncryptedString;
}

From source file:net.duckling.ddl.web.agent.util.AuthUtil.java

private static String decodeAuth(String auth) throws NoSuchAlgorithmException, NoSuchPaddingException,
        InvalidKeyException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException {
    SecretKeySpec spec = new SecretKeySpec(getKey(), "AES");
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.DECRYPT_MODE, spec);
    byte[] result = cipher.doFinal(Base64.decodeBase64(auth));
    return new String(result, "UTF-8");
}

From source file:cn.lynx.emi.license.ViewLicense.java

private static final String _decrypt(String data) {
    byte[] corekey = Base64.decodeBase64(LICENSE_CORE_KEY);
    byte[] rawData = Base64.decodeBase64(data);

    X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(corekey);
    try {// ww  w. j  a va 2  s . co m
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        Key publicKey = keyFactory.generatePublic(x509EncodedKeySpec);

        Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
        cipher.init(Cipher.DECRYPT_MODE, publicKey);

        return new String(cipher.doFinal(rawData), "UTF-8");
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:com.qubole.quark.catalog.db.encryption.AESEncrypt.java

public String convertToEntityAttribute(String phrase) throws SQLException {
    try {//from ww w .  j a  va  2  s .c om
        Cipher decryptCipher = Cipher.getInstance("AES");
        decryptCipher.init(Cipher.DECRYPT_MODE, generateMySQLAESKey(this.key, "UTF-8"));

        return new String(decryptCipher.doFinal(Hex.decodeHex(phrase.toCharArray())));
    } catch (Exception e) {
        throw new SQLException(e);
    }
}

From source file:com.launchkey.sdk.crypto.JCECrypto.java

/**
 * @param privateKey/* w  w  w.j a  v a 2 s .c o  m*/
 * @param provider
 */
public JCECrypto(PrivateKey privateKey, Provider provider) {
    this.provider = provider;
    this.privateKey = privateKey;

    // Test out the provider and key
    try {
        Cipher rsaDecryptCipher = Cipher.getInstance(RSA_CRYPTO_CIPHER, provider);
        rsaDecryptCipher.init(Cipher.DECRYPT_MODE, privateKey);
        getSha256withRSA();
        Cipher.getInstance(AES_CRYPTO_CIPHER, provider);
    } catch (NoSuchAlgorithmException e) {
        throw new IllegalArgumentException("Provider does not provide required cipher: RSA/ECB/OAEPWithSHA1",
                e);
    } catch (NoSuchPaddingException e) {
        throw new IllegalArgumentException("Provider does not provide required padding: MGF1", e);
    } catch (InvalidKeyException e) {
        throw new IllegalArgumentException("Invalid private key provided", e);
    }
}

From source file:Logi.GSeries.Libraries.Encryption.java

public static String decrypt(String encryptedString, String password) {
    try {//from  www. j a va  2 s  . com
        byte[] encryptedWithIV = Base64.decodeBase64(encryptedString);
        byte initialVector[] = new byte[16];
        byte[] encrypted = new byte[encryptedWithIV.length - initialVector.length];
        System.arraycopy(encryptedWithIV, 0, encrypted, 0, encrypted.length);
        System.arraycopy(encryptedWithIV, encrypted.length, initialVector, 0, initialVector.length);
        IvParameterSpec ivspec = new IvParameterSpec(initialVector);
        SecretKeySpec skeySpec = new SecretKeySpec(password.getBytes("UTF-8"), "AES");
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
        cipher.init(Cipher.DECRYPT_MODE, skeySpec, ivspec);
        byte[] original = cipher.doFinal(encrypted);
        return new String(original);
    } catch (Exception ex) {
        Logger.getLogger(Encryption.class.getName()).log(Level.SEVERE, null, ex);
        return "Error";
    }
}

From source file:com.kixeye.chassis.transport.crypto.SymmetricKeyCryptoUtils.java

/**
 * Decrypts a data blob using the given key and cipher.
 * /*from  w ww .  j  av  a2 s  .c om*/
 * @param data
 * @param offset
 * @param length
 * @param key
 * @param cipher
 * @return
 * @throws GeneralSecurityException
 */
public static byte[] decrypt(byte[] data, int offset, int length, Key key, String cipherTransformation,
        String cipherProvider) throws GeneralSecurityException {
    Cipher cipher = loadCipher(cipherTransformation, cipherProvider);
    cipher.init(Cipher.DECRYPT_MODE, key);

    return cipher.doFinal(data, offset, length);
}

From source file:com.jk.security.JKEncDec.java

/**
 * Decrypt./*from  w w w . j  a v  a 2  s  . c o  m*/
 *
 * @param cipherText
 *            the cipher text
 * @return the string
 */
public static String decrypt(String cipherText) {
    try {
        byte[] cipherBytes = toBytes(cipherText);
        Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding", "SunJCE");
        SecretKeySpec key = new SecretKeySpec(encryptionKey.getBytes("UTF-8"), "AES");
        cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(IV.getBytes("UTF-8")));
        return new String(cipher.doFinal(cipherBytes), "UTF-8").trim();
    } catch (Exception e) {
        throw new JKSecurityException(e);
    }
}