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.security.ch08_rsa.RSACoderTextKey.java

/**
 * /*from  w w w  .  j  a  va  2 s  . c om*/
 *
 * @param data
 *            ?
 * @param key
 *            
 * @return byte[] ?
 * @throws Exception
 */
private static byte[] decryptByPublicKey(byte[] data, byte[] key) throws Exception {

    // ?
    X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(key);

    KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);

    // ?
    PublicKey publicKey = keyFactory.generatePublic(x509KeySpec);

    // ?
    Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());

    cipher.init(Cipher.DECRYPT_MODE, publicKey);

    return cipher.doFinal(data);
}

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

/**
 * ?/*from   ww  w.  j av  a2  s .c  om*/
 * 
 * @param data
 *            ?
 * @param key
 *            
 * @return ??
 */
public static String decrypt(String data, String key) throws Exception {
    Key deskey = keyGenerator(key);
    Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
    // ?Cipher?
    cipher.init(Cipher.DECRYPT_MODE, deskey);
    // ?
    return new String(cipher.doFinal(Base64.decodeBase64(data)));
}

From source file:ar.gob.ambiente.servicios.gestionpersonas.entidades.util.CriptPass.java

/**
 * Mtodo para desencriptar una contrasea encriptada
 * @param textoEncriptado//  w ww  .j a  va  2 s.co m
 * @return
 * @throws Exception 
 */
public static String desencriptar(String textoEncriptado) throws Exception {

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

    try {
        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 (UnsupportedEncodingException | NoSuchAlgorithmException | NoSuchPaddingException
            | InvalidKeyException | IllegalBlockSizeException | BadPaddingException ex) {
        System.out.println(ex.getMessage());
    }
    return base64EncryptedString;
}

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

public static String decrypt(String data, PublicKey pub_key) throws Exception {
    Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
    // ?Cipher?//from ww w . j  av  a  2s. co m
    cipher.init(Cipher.DECRYPT_MODE, pub_key);
    // ?
    return new String(cipher.doFinal(Base64.decodeBase64(data)));
}

From source file:com.ec2box.manage.util.EncryptionUtil.java

/**
 * return decrypted value of encrypted string
 *
 * @param str encrypted string//from   ww w . j  a v  a 2s . c o m
 * @return decrypted string
 */
public static String decrypt(String str) {
    String retVal = null;
    if (str != null && str.length() > 0) {
        try {
            Cipher c = Cipher.getInstance("AES");
            c.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key, "AES"));
            byte[] decodedVal = Base64.decodeBase64(str.getBytes());
            retVal = new String(c.doFinal(decodedVal));
        } catch (Exception ex) {
            log.error(ex.toString(), ex);
        }

    }
    return retVal;
}

From source file:ch.newscron.encryption.Encryption.java

/**
 * Given a String, it is decoded and the result is returned as a String as well.
 * @param encodedUrl is a String that have the full data encrypted
 * @return decoded String/*from w w  w .j ava2 s .  c  o m*/
 */
public static String decode(String encodedUrl) {

    try {

        //Decode URL
        final SecretKeySpec secretKey = new SecretKeySpec(key, "AES");
        cipher.init(Cipher.DECRYPT_MODE, secretKey,
                new IvParameterSpec(initializationVector.getBytes("UTF-8")));

        String result = new String(cipher.doFinal(Base64.decodeBase64(encodedUrl)));

        //Extract and remove hash from JSONObject
        JSONParser parser = new JSONParser();
        JSONObject receivedData = (JSONObject) parser.parse(result);
        String receivedHash = (String) receivedData.get("hash");
        receivedData.remove("hash");

        //Compare received hash with newly computed hash
        if (checkDataValidity(receivedData)) {
            byte[] hashOfData = createMD5Hash(receivedData);

            if (receivedHash.equals(new String(hashOfData, "UTF-8"))) { //Valid data
                return receivedData.toString();
            }
        }
    } catch (Exception e) {
    } //Invalid data (including encryption algorithm exceptions

    return null;
}

From source file:adminpassword.AESDemo.java

@SuppressWarnings("static-access")
public String decrypt(String encryptedText) throws Exception {

    byte[] saltBytes = salt.getBytes("UTF-8");
    byte[] encryptedTextBytes = new Base64().decodeBase64(encryptedText);

    // Derive the key
    SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
    PBEKeySpec spec = new PBEKeySpec(password.toCharArray(), saltBytes, pswdIterations, keySize);

    SecretKey secretKey = factory.generateSecret(spec);
    SecretKeySpec secret = new SecretKeySpec(secretKey.getEncoded(), "AES");

    // Decrypt the message
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    cipher.init(Cipher.DECRYPT_MODE, secret, new IvParameterSpec(ivBytes));

    byte[] decryptedTextBytes = null;
    try {/*  w w w.j a va 2s .c o  m*/
        decryptedTextBytes = cipher.doFinal(encryptedTextBytes);
    } catch (IllegalBlockSizeException e) {
        e.printStackTrace();
    } catch (BadPaddingException e) {
        e.printStackTrace();
    }

    return new String(decryptedTextBytes);
}

From source file:com.github.sshw.crypt.EncryptionBean.java

public String decrypt(String message, String password) throws Exception {
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    Key key = keyFromPassword(password);
    byte[] cb = Base64.decodeBase64(message);
    IvParameterSpec ivb = new IvParameterSpec(key.getEncoded());
    cipher.init(Cipher.DECRYPT_MODE, key, ivb);
    String plaintext = new String(cipher.doFinal(cb));
    return plaintext;
}

From source file:edu.kit.dama.util.CryptUtil.java

/**
 * Hidden constuctor.//from www  . j  av a  2 s.  co m
 *
 * @param pSecret The secret used for the SecretKeySpec. The secret must
 * have a length of 128, 192 or 256 bits.
 */
private CryptUtil(byte[] pSecret) {
    try {
        SecretKeySpec skeySpec = new SecretKeySpec(pSecret, "AES");
        deCipher = Cipher.getInstance("AES/CBC/PKCS5Padding", new BouncyCastleProvider());
        deCipher.init(Cipher.DECRYPT_MODE, skeySpec, new IvParameterSpec(new byte[16]));
        enCipher = Cipher.getInstance("AES/CBC/PKCS5Padding", new BouncyCastleProvider());
        enCipher.init(Cipher.ENCRYPT_MODE, skeySpec, new IvParameterSpec(new byte[16]));
    } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException
            | InvalidAlgorithmParameterException e) {
        throw new IllegalStateException("Failed to create cipher instances.", e);
    }
}

From source file:corner.encrypt.services.impl.DESedeEncryptServiceImpl.java

/**
 * @see corner.encrypt.services.EncryptService#decrypt(byte[], byte[])
 *///from   w  ww  .j  a v  a2  s.  c  o  m
@Override
public byte[] decrypt(byte[] src, byte[] keybyte) {
    try {
        SecretKey deskey = new SecretKeySpec(keybyte, Algorithm);
        Cipher c1 = Cipher.getInstance(Algorithm);
        c1.init(Cipher.DECRYPT_MODE, deskey);
        return c1.doFinal(src);
    } catch (java.security.NoSuchAlgorithmException e1) {
        e1.printStackTrace();
    } catch (javax.crypto.NoSuchPaddingException e2) {
        e2.printStackTrace();
    } catch (java.lang.Exception e3) {
        e3.printStackTrace();
    }
    return null;
}