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.shenit.commons.codec.DesUtils.java

/**
 * //from w  w w. j  av a  2  s . c  om
 * 
 * @param keySpec
 * @param encryptedData
 */
public static byte[] decrypt(byte[] encryptedData, KeySpec keySpec) {
    return crypt(encryptedData, keySpec, Cipher.DECRYPT_MODE);
}

From source file:com.redsqirl.workflow.utils.FileStream.java

private static byte[] decrypt(byte[] ciphertext) throws Exception {
    SecretKey key = generateKey();
    PBEParameterSpec pbeParamSpec = new PBEParameterSpec(salt, 42);
    Cipher cipher = Cipher.getInstance("PBEWithMD5AndDES");
    cipher.init(Cipher.DECRYPT_MODE, key, pbeParamSpec);
    return cipher.doFinal(ciphertext);
}

From source file:com.dragoniade.encrypt.EncryptionHelper.java

private static Cipher getDecrypter(String seed, String algorithm) {
    try {//from  ww w.j av a 2  s  .c  o  m
        byte[] byteKey = getSeed(seed);
        SecretKeySpec keySpec = new SecretKeySpec(byteKey, algorithm);
        Cipher decrypt = Cipher.getInstance(algorithm);
        decrypt.init(Cipher.DECRYPT_MODE, keySpec);
        return decrypt;
    } catch (Exception e) {
        return null;
    }

}

From source file:net.bioclipse.encryption.Encrypter.java

public String decrypt(String encrypted) {
    SecretKeyFactory keyFac;/*from   ww  w. ja  v  a 2 s  .  c o m*/
    SecretKey pbeKey;
    Cipher pbeCipher;

    try {
        keyFac = SecretKeyFactory.getInstance(METHOD);
        pbeKey = keyFac.generateSecret(pbeKeySpec);
        pbeCipher = Cipher.getInstance(METHOD);
        pbeCipher.init(Cipher.DECRYPT_MODE, pbeKey, pbeParamSpec);
        return new String(pbeCipher.doFinal(Base64.decodeBase64(encrypted.getBytes())));
    } catch (NoSuchAlgorithmException e) {
        throw new IllegalStateException(e);
    } catch (InvalidKeySpecException e) {
        throw new IllegalStateException(e);
    } catch (NoSuchPaddingException e) {
        throw new IllegalStateException(e);
    } catch (InvalidKeyException e) {
        throw new IllegalStateException(e);
    } catch (InvalidAlgorithmParameterException e) {
        throw new IllegalStateException(e);
    } catch (IllegalBlockSizeException e) {
        throw new IllegalStateException(e);
    } catch (BadPaddingException e) {
        throw new IllegalStateException(e);
    }
}

From source file:com.telefonica.euro_iaas.sdc.util.RSASignerImpl.java

/**
 * {@inheritDoc}/*  w ww  . j  ava2s .  c  o m*/
 */
@Override
public String unsign(String message, File pemFile) {
    try {
        Security.addProvider(new BouncyCastleProvider());
        JCERSAPublicKey publicKey = readPublicKey(pemFile, "".toCharArray());

        Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
        cipher.init(Cipher.DECRYPT_MODE, publicKey);

        // decryption:
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        baos.write(cipher.doFinal(org.bouncycastle.util.encoders.Base64.decode(message.getBytes("UTF-8"))));
        return baos.toString();
    } catch (IOException e) {
        throw new SdcRuntimeException(e);
    } catch (NoSuchAlgorithmException e) {
        throw new SdcRuntimeException(e);
    } catch (InvalidKeyException e) {
        throw new SdcRuntimeException(e);
    } catch (NoSuchPaddingException e) {
        throw new SdcRuntimeException(e);
    } catch (IllegalBlockSizeException e) {
        throw new SdcRuntimeException(e);
    } catch (BadPaddingException e) {
        throw new SdcRuntimeException(e);
    }
}

From source file:fr.ortolang.diffusion.security.authentication.TicketHelper.java

public static Ticket decodeTicket(String ticket) {
    byte[] encryptedBytes = Base64.decodeBase64(ticket.getBytes());
    try {//w  w w  .  j  a va 2s  . c  om
        Cipher cipher = Cipher.getInstance(ALGORITHM_MODE_PADDING);
        cipher.init(Cipher.DECRYPT_MODE, key);
        byte[] decryptedBytes = cipher.doFinal(encryptedBytes);
        // Extract digest from decryptedBytes (MD5 length is 16 bytes)
        byte[] digest = Arrays.copyOfRange(decryptedBytes, decryptedBytes.length - 16, decryptedBytes.length);
        byte[] serializedMap = Arrays.copyOfRange(decryptedBytes, 0, decryptedBytes.length - 16);
        MessageDigest md = MessageDigest.getInstance(MESSAGE_DIGEST_ALGORITHM);
        if (!Arrays.equals(digest, md.digest(serializedMap))) {
            throw new DigestException();
        }
        ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(serializedMap));
        return (Ticket) ois.readObject();
    } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | BadPaddingException
            | IllegalBlockSizeException | IOException | ClassNotFoundException | DigestException e) {
        LOGGER.log(Level.SEVERE, "Error when decoding ticket: " + e.getMessage());
    }
    return null;
}

From source file:com.clustercontrol.commons.util.CryptUtil.java

public static String decrypt(String key, String word) {
    if (word == null) {
        return null;
    }// w  w w .j a  va 2  s . c om

    byte[] encrypted = Base64.decodeBase64(word);
    // ?
    SecretKeySpec sksSpec = new SecretKeySpec(key.getBytes(), algorithm);
    Cipher cipher = null;
    try {
        cipher = Cipher.getInstance(algorithm);
    } catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
        m_log.warn("encrypt : " + (e.getClass().getName()) + "," + e.getMessage(), e);
        return null;
    }

    try {
        cipher.init(Cipher.DECRYPT_MODE, sksSpec);
    } catch (InvalidKeyException e) {
        m_log.warn("encrypt : " + (e.getClass().getName()) + "," + e.getMessage(), e);
        return null;
    }

    byte[] decrypted;
    try {
        decrypted = cipher.doFinal(encrypted);
    } catch (IllegalBlockSizeException | BadPaddingException e) {
        m_log.warn("encrypt : " + (e.getClass().getName()) + "," + e.getMessage(), e);
        return null;
    }

    return new String(decrypted);
}

From source file:de.extra.client.plugins.outputplugin.crypto.ExtraCryptoUtil.java

/** Decrypts the specified encrypted string, using the specified secret key. */
private static String decrypt(String sName, String secretKey) {
    if (secretKey == null) {
        secretKey = SYM_KEY_STR;//from w w w . j  a va2s .c  o m
    }

    if (sName == null || sName.equals("")) {
        return "";
    }

    String sText = "";
    try {
        SecretKeySpec skeySpec = decodeKey(secretKey);
        Cipher decryptCipher = Cipher.getInstance(TRANSFORMATION);
        decryptCipher.init(Cipher.DECRYPT_MODE, skeySpec);

        byte[] encpArr = new Base64().decode(sName.trim());

        byte[] plainText = decryptCipher.doFinal(encpArr);

        sText = new String(plainText, CHARSET);
    } catch (Exception e) {
        e.printStackTrace(System.err);
    }

    return sText;
}

From source file:my.adam.smo.common.AsymmetricEncryptionBox.java

public byte[] decrypt(PrivateKey prvKey, byte[] cryptogram) {
    try {//  www .  j a  va 2s . co m
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.DECRYPT_MODE, prvKey);
        return cipher.doFinal(cryptogram);
    } catch (Exception ex) {
        throw new IllegalStateException(ex);
    }
}

From source file:de.marius_oe.cfs.cryption.Crypter.java

/**
 * Decrypts the given input stream and stores the decrypted bytes in the
 * destinationFile. If compressStream is <code>true</code>, the given stream
 * has been compressed and is uncompressed after decryption.
 *
 * @param inStream/*from  w  w w .ja v a 2 s.  com*/
 *            source stream with encrypted data and iv at the beginning
 * @param destinationStream
 *            stream for the decrypted data
 * @param compressStream
 *            whether the stream was compressed before encryption
 */
public static void decrypt(InputStream inStream, OutputStream destinationStream, boolean compressStream) {
    logger.debug("decrypting inputstream - compressed: {}", compressStream);

    try {
        // reading iv of stream
        int ivLength = inStream.read();
        byte[] iv = new byte[ivLength];
        inStream.read(iv);

        logger.debug("Decrypt InputStream.");
        inStream = new CipherInputStream(inStream, getCipher(Cipher.DECRYPT_MODE, iv));

        if (compressStream) {
            logger.debug("Decompress InputStream.");

            try {
                inStream = new ZipInputStream(inStream);
                ((ZipInputStream) inStream).getNextEntry();
            } catch (IOException e) {
                logger.debug("Error occured during unzipping - Reason: {}", e.getLocalizedMessage());
                throw new RuntimeException(e);
            }
        }

        // copy stream
        int bytesCopied = IOUtils.copy(inStream, destinationStream);

        logger.debug("decryption done. copied {} decrypted bytes to the outputstream", bytesCopied);

        inStream.close();
        destinationStream.close();
    } catch (IOException e) {
        logger.error("Decryption failed - Reason: {}", e.getLocalizedMessage());
        throw new RuntimeException(e);
    }
}