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.scorpio4.util.io.IOStreamCrypto.java

public CipherInputStream decrypt(InputStream in) throws NoSuchPaddingException, NoSuchAlgorithmException,
        InvalidAlgorithmParameterException, InvalidKeyException {
    final SecretKey key = new SecretKeySpec(bytePassword, cipherSpec);
    final IvParameterSpec IV = new IvParameterSpec(ivBytes);
    final Cipher cipher = Cipher.getInstance(cipherTransformation);
    cipher.init(Cipher.DECRYPT_MODE, key, IV);
    return new CipherInputStream(new Base64InputStream(in), cipher);
}

From source file:com.haulmont.cuba.web.test.PasswordEncryptionTest.java

protected String decryptPassword(String password) {
    SecretKeySpec key = new SecretKeySpec(PASSWORD_KEY.getBytes(), "DES");
    IvParameterSpec ivSpec = new IvParameterSpec(PASSWORD_KEY.getBytes());
    String result;// ww w.  java  2 s  .  c  o m
    try {
        Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, key, ivSpec);
        result = new String(cipher.doFinal(Hex.decodeHex(password.toCharArray())));
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    return result;
}

From source file:de.openflorian.crypt.provider.BlowfishCipher.java

@Override
public String decrypt(String str) throws GeneralSecurityException {
    if (key == null || key.isEmpty())
        throw new IllegalStateException("The key is not set or is length=0.");

    if (str == null)
        return null;

    try {/*from   ww  w .j av  a2 s  . c  om*/
        SecretKeySpec keySpec;
        keySpec = new SecretKeySpec(key.getBytes("UTF8"), "Blowfish");

        Cipher cipher = Cipher.getInstance("Blowfish/ECB/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, keySpec);

        return new String(cipher.doFinal(Base64.decodeBase64(str.getBytes("UTF8"))), "UTF8");
    } catch (Exception e) {
        log.error(e.getMessage(), e);
        throw new GeneralSecurityException(e.getMessage(), e);
    }
}

From source file:it.latraccia.pkcs11.reader.util.AESUtil.java

public static String decryptString(String encryptedText, String password)
        throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterException,
        InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
    String decrypted = null;/*from  w  w  w .jav a2s .  co  m*/
    byte[] key = password.getBytes();
    if (key.length != 16) {
        throw new IllegalArgumentException("Invalid key size.");
    }
    byte[] value = Base64.decodeBase64(encryptedText);

    // Decrypt with AES/CBC/PKCS5Padding
    SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    cipher.init(Cipher.DECRYPT_MODE, skeySpec, new IvParameterSpec(new byte[16]));
    byte[] original = cipher.doFinal(value);
    decrypted = new String(original);

    return decrypted;
}

From source file:com.enviosya.client.tool.Tool.java

public String Desencriptar(String textoEncriptado) throws Exception {

    //String secretKey = "qualityinfosolutions"; //llave para desenciptar datos
    String base64EncryptedString = "";

    try {/*from  w  ww. ja  va  2s.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 e) {
        //Ac tengo que agregar el retorno de la exception
    }
    return base64EncryptedString;
}

From source file:it.geosolutions.figis.persistence.dao.util.PwEncoder.java

/**
 * Decode the password on base 64/*  ww w  . ja  va  2 s  . c o  m*/
 * @param msg
 * @return
 */
public static String decode(String msg) {
    try {
        SecretKeySpec keySpec = new SecretKeySpec(KEY, "AES");
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.DECRYPT_MODE, keySpec);

        byte[] de64 = Base64.decodeBase64(msg);
        byte[] decrypted = cipher.doFinal(de64);

        return new String(decrypted);
    } catch (NoSuchAlgorithmException ex) {
        throw new RuntimeException("Error while encoding", ex);
    } catch (NoSuchPaddingException ex) {
        throw new RuntimeException("Error while encoding", ex);
    } catch (IllegalBlockSizeException ex) {
        throw new RuntimeException("Error while encoding", ex);
    } catch (BadPaddingException ex) {
        throw new RuntimeException("Error while encoding", ex);
    } catch (InvalidKeyException ex) {
        throw new RuntimeException("Error while encoding", ex);
    }
}

From source file:com.arwantech.docjavaui.utils.TripleDES.java

public String decrypt(String encryptedString) {
    String decryptedText = null;//from   w w w  . j  av a2 s. c om
    try {
        cipher.init(Cipher.DECRYPT_MODE, key);
        byte[] encryptedText = Base64.decodeBase64(encryptedString.getBytes());
        byte[] plainText = cipher.doFinal(encryptedText);
        decryptedText = new String(plainText);
    } catch (InvalidKeyException | IllegalBlockSizeException | BadPaddingException e) {
        e.printStackTrace();
        decryptedText = "Error decrypts";
    } finally {
        return decryptedText;
    }

}

From source file:de.adorsys.morphiaencryption.AES256CryptoProvider.java

@Override
public byte[] decrypt(byte[] encData) {
    try {/* www  .  ja v a2s  .  c  om*/
        Cipher cipher = Cipher.getInstance(AES_CBC_PKCS5_PADDING);
        cipher.init(Cipher.DECRYPT_MODE, key, getIV());
        byte[] decrypted = cipher.doFinal(encData);
        return decrypted;
    } catch (BadPaddingException | IllegalBlockSizeException | InvalidKeyException
            | InvalidAlgorithmParameterException | NoSuchAlgorithmException | NoSuchPaddingException e) {
        throw new CryptException(e);
    }
}

From source file:com.example.license.RSAUtil.java

/**
 * ?/*from  w  w w.ja va2s  . c om*/
 * 
 * @param data
 *            ?
 * @param key
 *            
 * @return ??
 */
public static String decrypt(String data, String seed) throws Exception {
    KeyPair keyPair = generatorKeyPair(seed);
    Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
    // ?Cipher?
    cipher.init(Cipher.DECRYPT_MODE, keyPair.getPublic());
    // ?
    return new String(cipher.doFinal(Base64.decodeBase64(data)));
}

From source file:com.jwm123.loggly.reporter.TripleDesCipher.java

public TripleDesCipher(String keyPath, AppDirectory appDir) throws NoSuchAlgorithmException,
        NoSuchProviderException, NoSuchPaddingException, InvalidKeyException, IOException {
    this.appDir = appDir;
    if (key == null) {
        this.keyPath = keyPath;
        getKey();//from w w w .  j  a  va 2s  .  co  m
    }
    SecretKey keySpec = new SecretKeySpec(key, ALGORITHM);
    encrypter = Cipher.getInstance(TRIPLE_DES_TRANSFORMATION);
    encrypter.init(Cipher.ENCRYPT_MODE, keySpec);
    decrypter = Cipher.getInstance(TRIPLE_DES_TRANSFORMATION);
    decrypter.init(Cipher.DECRYPT_MODE, keySpec);
}