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:org.matrix.security.crypto.encrypt.AesBytesEncryptor.java

public byte[] decrypt(byte[] encryptedBytes) {
    synchronized (decryptor) {
        byte[] iv = iv(encryptedBytes);
        initCipher(decryptor, Cipher.DECRYPT_MODE, secretKey, new IvParameterSpec(iv));
        return doFinal(decryptor,
                ivGenerator != NULL_IV_GENERATOR ? encrypted(encryptedBytes, iv.length) : encryptedBytes);
    }/*from   ww  w  .  java  2 s  . c o m*/
}

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

private void initBlowfish() {
    encryptCipher = getCipher(CIPHER_NAME, PROVIDER_NAME);
    decryptCipher = getCipher(CIPHER_NAME, PROVIDER_NAME);
    initCipher(encryptCipher, Cipher.ENCRYPT_MODE, keySpec, paramSpec);
    initCipher(decryptCipher, Cipher.DECRYPT_MODE, keySpec, paramSpec);
}

From source file:com.alkacon.opencms.commons.CmsStringCrypter.java

/**
 * Decrypts the given value which was encrypted with the encrypt method.<p>
 * /*  w  ww. ja  v a  2s.c o m*/
 * @param value the value to be decrypted
 * @param password the passsword used for decryption, has to be the same as used for encryption
 * @return the decrypted string of the value or null if something went wrong
 */
public static String decrypt(String value, String password) {

    // check if given value is valid
    if (CmsStringUtil.isEmptyOrWhitespaceOnly(value)) {
        if (LOG.isWarnEnabled()) {
            LOG.warn(Messages.get().getBundle().key(Messages.LOG_WARN_INVALID_DECRYPT_STRING_1, value));
        }
        return null;
    }

    try {

        // create key
        Key key = new SecretKeySpec(getKey(password), ENCRYPTION);
        Cipher cipher = Cipher.getInstance(ENCRYPTION);
        cipher.init(Cipher.DECRYPT_MODE, key);

        // decode from base64
        BASE64Decoder base64decoder = new BASE64Decoder();
        byte[] cleartext = base64decoder.decodeBuffer(value);

        // decrypt text
        byte[] ciphertext = cipher.doFinal(cleartext);
        return CmsEncoder.decode(new String(ciphertext));
    } catch (Exception ex) {
        if (LOG.isErrorEnabled()) {
            LOG.error(Messages.get().getBundle().key(Messages.LOG_ERROR_DECRPYT_0), ex);
        }
    }

    return null;
}

From source file:io.zipi.common.util.AesEncrypter.java

/**
 * Decrypt.//from   w w  w .ja v  a2  s. co m
 * @param encryptedText the encrypted text
 * @param secretKey the secret key
 * @return the string
 */
public static final String decrypt(String secretKey, String encryptedText) {
    if (encryptedText == null) {
        return null;
    }
    if (encryptedText.startsWith("$CRYPT::")) { //$NON-NLS-1$
        byte[] decoded = Base64.decodeBase64(encryptedText.substring(8));
        Cipher cipher;
        try {
            SecretKeySpec skeySpec = keySpecFromSecretKey(secretKey);

            cipher = Cipher.getInstance("AES"); //$NON-NLS-1$
            cipher.init(Cipher.DECRYPT_MODE, skeySpec);
        } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException e) {
            throw new RuntimeException(e);
        }
        try {
            return new String(cipher.doFinal(decoded));
        } catch (IllegalBlockSizeException | BadPaddingException e) {
            throw new RuntimeException(e);
        }
    } else {
        return encryptedText;
    }
}

From source file:com.seer.datacruncher.utils.CryptoUtil.java

/**
 * Method To Decrypt An Ecrypted String/*from www .  ja v a2  s.  com*/
 */
public String decrypt(String encryptedString) {
    String decryptedText = null;
    try {
        if (!checkKeyVerify())
            encryptedString = encryptedString.substring(encryptedString.length() / 2, encryptedString.length());
        cipher.init(Cipher.DECRYPT_MODE, key);
        byte[] encryptedText = Base64.decodeBase64(encryptedString);
        byte[] plainText = cipher.doFinal(encryptedText);
        decryptedText = bytes2String(plainText);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return decryptedText;
}

From source file:com.muk.services.commerce.CryptoServiceImpl.java

private byte[] decrypt(byte[] byteArray) {
    byte[] result = null;

    try {/*from  w w w.  j a v a2 s.c  o  m*/
        final Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, temporaryKey, ivSpec);

        result = cipher.doFinal(byteArray);

    } catch (final InvalidKeyException keyEx) {
        LOG.error("Failed to init cipher.", keyEx);
    } catch (final NoSuchPaddingException padEx) {
        LOG.error("Failed to init cipher.", padEx);
    } catch (final NoSuchAlgorithmException algEx) {
        LOG.error("Failed to init cipher.", algEx);
    } catch (final BadPaddingException badPadEx) {
        LOG.error("Failed to decrypt.", badPadEx);
    } catch (final IllegalBlockSizeException blockEx) {
        LOG.error("Failed to decrypt.", blockEx);
    } catch (final InvalidAlgorithmParameterException paramEx) {
        LOG.error("Failed to decrypt.", paramEx);
    }

    return result;
}

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

/**
 * @see Crypto#decryptRSA(byte[])/*  ww w.  j ava2  s .c  o  m*/
 */
public byte[] decryptRSA(byte[] message) {
    return processRSA(message, privateKey, Cipher.DECRYPT_MODE);
}

From source file:com.credomatic.gprod.db2query2csv.Security.java

/**
 * Descifra una cadena de caracteres apartir de la llave de cifrado y retorna le valor original.
 * @param key llave generada durante el cifrado de la cadena original
 * @param value cadena cifrda//from  w  w w  .j a v a  2  s  .c  o m
 * @return cadena descifrada
 */
public static String decrypt(String key, String value) {
    try {
        Key k = new SecretKeySpec(new Base64().decode(key), "AES");
        Cipher c = Cipher.getInstance("AES");
        c.init(Cipher.DECRYPT_MODE, k);
        byte[] decodedValue = new Base64().decode(value);
        byte[] decValue = c.doFinal(decodedValue);
        String decryptedValue = new String(decValue);
        return decryptedValue;
    } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException
            | BadPaddingException ex) {
        Logger.getLogger(Security.class.getName()).log(Level.SEVERE, null, ex);
    }
    return null;
}

From source file:com.poscoict.license.util.LmsUtil.java

public String decryptRsa(PrivateKey privateKey, String securedValue) throws Exception {
    Cipher cipher = Cipher.getInstance("RSA");
    byte[] encryptedBytes = hexToByteArray(securedValue);
    cipher.init(Cipher.DECRYPT_MODE, privateKey);
    byte[] decryptedBytes = cipher.doFinal(encryptedBytes);
    String decryptedValue = new String(decryptedBytes, "utf-8");
    return decryptedValue;
}

From source file:com.formkiq.core.service.crypto.SecureTokenServiceImpl.java

@Override
public String decryptToken(final PublicKey publicKey, final String encryptedToken)
        throws GeneralSecurityException {

    Cipher cipher = Cipher.getInstance("RSA");

    cipher.init(Cipher.DECRYPT_MODE, publicKey);

    byte[] bs = cipher.doFinal(Base64.getDecoder().decode(encryptedToken));
    return Strings.toString(bs);
}