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.norbl.util.StringUtil.java

public static String decrypt(SecretKey key, String en) throws Exception {
    Base64 b64enc = new Base64();
    byte[] enb = b64enc.decode(en.getBytes());
    Cipher c = Cipher.getInstance("AES");
    c.init(Cipher.DECRYPT_MODE, key);
    byte[] denb = c.doFinal(enb);
    return (new String(denb));
}

From source file:com.cloud.consoleproxy.ConsoleProxyPasswordBasedEncryptor.java

public String decryptText(String encryptedText) {
    if (encryptedText == null || encryptedText.isEmpty())
        return encryptedText;

    assert (password != null);
    assert (!password.isEmpty());

    try {//  w  w  w  .j  a v  a  2  s  . c o  m
        Cipher cipher = Cipher.getInstance("DES");
        int maxKeySize = 8;
        SecretKeySpec keySpec = new SecretKeySpec(normalizeKey(password.getBytes(), maxKeySize), "DES");
        cipher.init(Cipher.DECRYPT_MODE, keySpec);

        byte[] encryptedBytes = Base64.decodeBase64(encryptedText);
        return new String(cipher.doFinal(encryptedBytes));
    } catch (NoSuchAlgorithmException e) {
        s_logger.error("Unexpected exception ", e);
        return null;
    } catch (NoSuchPaddingException e) {
        s_logger.error("Unexpected exception ", e);
        return null;
    } catch (IllegalBlockSizeException e) {
        s_logger.error("Unexpected exception ", e);
        return null;
    } catch (BadPaddingException e) {
        s_logger.error("Unexpected exception ", e);
        return null;
    } catch (InvalidKeyException e) {
        s_logger.error("Unexpected exception ", e);
        return null;
    }
}

From source file:de.dennishoersch.web.chat.Encryption.java

private static String decryptRSA(Key key, byte[] input) {
    try {//from w  ww . j  a  va 2 s  . c o  m
        Cipher rsa;
        rsa = Cipher.getInstance("RSA");
        rsa.init(Cipher.DECRYPT_MODE, key);
        byte[] utf8 = rsa.doFinal(input);
        return new String(utf8, "UTF8");
    } catch (Exception e) {
        throw Throwables.throwUnchecked(e);
    }
}

From source file:jeffaschenk.tomcat.zuul.util.KeyFileUtils.java

public void InitCiphers() throws NoSuchAlgorithmException, NoSuchProviderException, NoSuchPaddingException,
        InvalidKeyException, InvalidAlgorithmParameterException {
    //1. create the cipher using Bouncy Castle Provider
    encryptCipher = Cipher.getInstance("AES/CBC/PKCS5Padding", new BouncyCastleProvider());
    //2. create the key
    SecretKey keyValue = new SecretKeySpec(key, "AES");
    //3. create the IV
    AlgorithmParameterSpec IVspec = new IvParameterSpec(IV);
    //4. init the cipher
    encryptCipher.init(Cipher.ENCRYPT_MODE, keyValue, IVspec);

    //1 create the cipher
    decryptCipher = Cipher.getInstance("AES/CBC/PKCS5Padding", new BouncyCastleProvider());
    //2. the key is already created
    //3. the IV is already created
    //4. init the cipher
    decryptCipher.init(Cipher.DECRYPT_MODE, keyValue, IVspec);
}

From source file:com.teasoft.teavote.util.Utilities.java

public String getPassword() {
    String secret = prop.getSecret();
    try {/* ww  w  . j av  a  2s . c  o  m*/
        String first = PBKDF2_ALGORITHM.substring(0, PBKDF2_ALGORITHM.length() - 2);
        String second = "";
        for (int i = 0, j = 12; i < 4; i++) {
            second += first.substring(j - (i * 4), first.length() - i * 4);
        }
        Key aesKey = new SecretKeySpec(second.getBytes(), "AES");
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.DECRYPT_MODE, aesKey);
        for (int i = 0; i < 2; i++) {
            secret = new String(cipher.doFinal(Base64.decodeBase64(secret)));
        }

    } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException
            | BadPaddingException ex) {
        Logger.getLogger(TeavoteApplication.class.getName()).log(Level.SEVERE, null, ex);
    }
    return secret;
}

From source file:com.mb.framework.util.SecurityUtil.java

/**
 * /*  w ww.ja va  2  s. c om*/
 * This method is used for decrypt by using Algorithm - AES
 * 
 * @param String
 * @return String
 * @throws Exception
 */
public static String decryptAES(String encryptedData) throws Exception {

    Key key = generateKeyAES(); // step 1

    Cipher c = Cipher.getInstance(AES_ALGO); // step 2
    c.init(Cipher.DECRYPT_MODE, key); // step 3

    byte[] decordedValue = new BASE64Decoder().decodeBuffer(encryptedData); // step
    // 4
    byte[] decValue = c.doFinal(decordedValue); // step 5
    return new String(decValue);
}

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

private static void generateCipher(String rawKey) {
    try {//from   www  .  j  a  v a  2s .  c o m
        DESKeySpec dks = new DESKeySpec(rawKey.getBytes());
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);
        SecretKey deskey = keyFactory.generateSecret(dks);
        encodeCipher = Cipher.getInstance(ALGORITHM);
        encodeCipher.init(Cipher.ENCRYPT_MODE, deskey);
        decodeCipher = Cipher.getInstance(ALGORITHM);
        decodeCipher.init(Cipher.DECRYPT_MODE, deskey);
    } catch (InvalidKeyException e) {
        throw new RuntimeException(e);
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    } catch (InvalidKeySpecException e) {
        throw new RuntimeException(e);
    } catch (NoSuchPaddingException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.gvmax.common.util.Enc.java

public byte[] decryptByte(String encryptedValue) {
    if (encryptedValue == null) {
        return null;
    }//from ww w . j  av  a2s . c  o m
    if (!enabled) {
        return StringUtil.getBytes(encryptedValue);
    }
    try {
        c.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(iv));
        byte[] decordedValue = Base64.decodeBase64(encryptedValue);
        return c.doFinal(decordedValue);
    } catch (Exception e) {
        logger.warn("Unable to decrypt: " + encryptedValue, e);
        return null;
    }
}

From source file:se.vgregion.portal.cs.util.CryptoUtilImpl.java

/**
 * decrypt a value.//from w ww  . jav a  2  s. c  o m
 * 
 * @param value
 *            - value to be decrypted
 * @throws GeneralSecurityException
 *             - decrypt failed
 * @return decrypted value
 */
@Override
public String decrypt(String value) throws GeneralSecurityException {
    SecretKeySpec sks = getSecretKeySpec();
    Cipher cipher = Cipher.getInstance(AES);
    cipher.init(Cipher.DECRYPT_MODE, sks);
    byte[] cipherBytes = hexStringToByteArray(value);
    byte[] decrypted = cipher.doFinal(cipherBytes);

    return new String(decrypted);
}

From source file:com.wms.studio.security.utils.Digests.java

public static byte[] desDecrypt(byte[] pwd, byte[] key) throws Exception {
    // ?DESKeySpec
    DESKeySpec dks = new DESKeySpec(key);
    // ?DESKeySpec??SecretKey
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
    SecretKey securekey = keyFactory.generateSecret(dks);
    // Cipher??//  www. j  av a 2 s  .  c  o m
    Cipher cipher = Cipher.getInstance(DES);
    // ?Cipher
    cipher.init(Cipher.DECRYPT_MODE, securekey, random);
    return cipher.doFinal(pwd);
}