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.almende.util.EncryptionUtil.java

/**
 * Decrypt an encrypted string./*from  w  w w .j  ava  2  s .co m*/
 * 
 * @param encryptedText
 *            the encrypted text
 * @return text
 * @throws InvalidKeyException
 *             the invalid key exception
 * @throws InvalidAlgorithmParameterException
 *             the invalid algorithm parameter exception
 * @throws NoSuchAlgorithmException
 *             the no such algorithm exception
 * @throws InvalidKeySpecException
 *             the invalid key spec exception
 * @throws NoSuchPaddingException
 *             the no such padding exception
 * @throws IllegalBlockSizeException
 *             the illegal block size exception
 * @throws BadPaddingException
 *             the bad padding exception
 * @throws UnsupportedEncodingException
 *             the unsupported encoding exception
 */
public static String decrypt(final String encryptedText) throws InvalidKeyException,
        InvalidAlgorithmParameterException, NoSuchAlgorithmException, InvalidKeySpecException,
        NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException {
    final PBEParameterSpec pbeParamSpec = new PBEParameterSpec(S, C);
    final PBEKeySpec pbeKeySpec = new PBEKeySpec(P);
    final SecretKeyFactory keyFac = SecretKeyFactory.getInstance(ENC);
    final SecretKey pbeKey = keyFac.generateSecret(pbeKeySpec);

    final Cipher pbeCipher = Cipher.getInstance(ENC);
    pbeCipher.init(Cipher.DECRYPT_MODE, pbeKey, pbeParamSpec);

    final byte[] text = pbeCipher.doFinal(Base64.decodeBase64(encryptedText));
    return new String(text, "UTF-8").intern();
}

From source file:com.xwiki.authentication.ntlm.NTLMAuthServiceImpl.java

protected String decryptText(String text, XWikiContext context) {
    try {/* w  w  w. j  a  v  a2  s.c om*/
        String secretKey = null;
        secretKey = context.getWiki().Param("xwiki.authentication.encryptionKey");
        secretKey = secretKey.substring(0, 24);

        if (secretKey != null) {
            SecretKeySpec key = new SecretKeySpec(secretKey.getBytes(), "TripleDES");
            Cipher cipher = Cipher.getInstance("TripleDES");
            cipher.init(Cipher.DECRYPT_MODE, key);
            byte[] encrypted = Base64.decode(text.replaceAll("_", "="));
            String decoded = new String(cipher.doFinal(encrypted));
            return decoded;
        } else {
            LOG.error("Encryption key not defined");
        }
    } catch (Exception e) {
        LOG.error("Failed to decrypt text", e);
    }

    return null;
}

From source file:com.doplgangr.secrecy.filesystem.encryption.AES_ECB_Crypter.java

@Override
public SecrecyCipherInputStream getCipherInputStream(File encryptedFile)
        throws SecrecyCipherStreamException, FileNotFoundException {
    Cipher c;//  w w  w .  j a  v  a  2 s  .  c o m
    try {
        c = Cipher.getInstance(mode);
    } catch (NoSuchAlgorithmException e) {
        throw new SecrecyCipherStreamException("Encryption algorithm not found!");
    } catch (NoSuchPaddingException e) {
        throw new SecrecyCipherStreamException("Selected padding not found!");
    }

    try {
        c.init(Cipher.DECRYPT_MODE, aesKey);
    } catch (InvalidKeyException e) {
        throw new SecrecyCipherStreamException("Invalid encryption key!");
    }

    return new SecrecyCipherInputStream(new FileInputStream(encryptedFile), c);
}

From source file:com.qubit.solution.fenixedu.bennu.webservices.services.server.SecurityHeader.java

private byte[] decryptWithPrivateKey(String cipherText, PrivateKey privateKey)
        throws IOException, GeneralSecurityException {

    Cipher cipher = Cipher.getInstance("RSA");
    cipher.init(Cipher.DECRYPT_MODE, privateKey);
    return cipher.doFinal(Base64.decodeBase64(cipherText));
}

From source file:com.servoy.j2db.util.SecuritySupport.java

@SuppressWarnings("nls")
public static String decrypt(Settings settings, String value) throws Exception {
    if (value == null)
        return value;
    Cipher cipher = Cipher.getInstance("DESede");
    cipher.init(Cipher.DECRYPT_MODE, SecuritySupport.getCryptKey(settings));
    return new String(cipher.doFinal(Utils.decodeBASE64(value)));
}

From source file:models.logic.CipherDecipher.java

public static void decrypt(SecretKey key, InputStream is, OutputStream os) throws Throwable {
    encryptOrDecrypt(key, Cipher.DECRYPT_MODE, is, os);
}

From source file:de.teamgrit.grit.util.mailer.EncryptorDecryptor.java

/**
 * Decrypts a given string.//from  w  ww .ja va  2 s. c  om
 * 
 * @param stringToDecrypt
 *            : this is the string that shall be decrypted.
 * @return : the decrypted string or null when failed. : when decrypting
 *         fails.
 * @throws NoSuchPaddingException
 * @throws NoSuchAlgorithmException
 * @throws InvalidKeyException
 * @throws BadPaddingException
 * @throws IllegalBlockSizeException
 */
public String decrypt(String stringToDecrypt) throws NoSuchAlgorithmException, NoSuchPaddingException,
        InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
    // get instance of cryptographic cipher using AES
    Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING");

    // use the provided secret key for decryption using AES
    final SecretKeySpec secretKey = new SecretKeySpec(m_key, "AES");

    // initialize cryptographic cipher with decryption mode
    cipher.init(Cipher.DECRYPT_MODE, secretKey);

    // decrypt the string encoded in base 64
    final String decryptedString = new String(cipher.doFinal(Base64.decodeBase64(stringToDecrypt)));
    return decryptedString;
}

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

/**
 * ?//from   w  ww.  ja  v  a 2s  .c  o m
 * 
 * @param data
 *            ?
 * @param key
 *            
 * @return ??
 */
public static String decryptBase64(String data, String key) throws Exception {
    DESKeySpec desKey = new DESKeySpec(key.getBytes());
    // ?DESKeySpec??
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);
    SecretKey securekey = keyFactory.generateSecret(desKey);

    Cipher cipher = Cipher.getInstance(ALGORITHM);
    cipher.init(Cipher.DECRYPT_MODE, securekey);
    // ?
    return new String(cipher.doFinal(Base64.decodeBase64(data.getBytes())));
}

From source file:br.com.vizzatech.cryptocipher.CryptoXCipher.java

/**
 * Construtor private/*from ww w .j av a  2  s . c  o m*/
 * 
 * @param sal
 *            Palavra chave
 * @param algoritmo
 *            Algoritmo a ser utilizado
 * @param cifraCesar
 *            em quantos numeros os caracteres sero trocados
 * @param charset
 * @throws IllegalArgumentException
 */
private CryptoXCipher(String sal, String algoritmo, int cifraCesar, Charset charset)
        throws IllegalArgumentException {
    try {
        this.charset = charset;
        SecretKey chave = getKey(algoritmo, sal);
        this.encrypt = Cipher.getInstance(algoritmo);
        this.decrypt = Cipher.getInstance(algoritmo);
        this.encrypt.init(Cipher.ENCRYPT_MODE, chave, paramSpec);
        this.decrypt.init(Cipher.DECRYPT_MODE, chave, paramSpec);
        this.cifraCesar = cifraCesar;
    } catch (Exception e) {
        throw new IllegalArgumentException(e);
    }
}

From source file:ai.serotonin.backup.Restore.java

File decryptFile(final File encryptedFile) throws Exception {
    String filename = encryptedFile.getName();

    int pos = filename.indexOf('_');
    final String saltStr = filename.substring(0, pos);
    final byte[] salt = Hex.decodeHex(saltStr.toCharArray());
    filename = filename.substring(pos + 1);

    pos = filename.indexOf('_');
    final String ivStr = filename.substring(0, pos);
    final byte[] iv = Hex.decodeHex(ivStr.toCharArray());
    filename = filename.substring(pos + 1);

    final File file = new File(filename);

    final SecretKey secret = createSecretKey(salt);

    final Cipher cipher = createCipher();
    cipher.init(Cipher.DECRYPT_MODE, secret, new IvParameterSpec(iv));

    cipherizeFile(encryptedFile, file, cipher);

    LOG.info("Decrypted archive to " + filename);

    return file;//  w w  w  . j  a  v  a 2s .c o  m
}