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.amazonaws.cognito.sync.devauth.client.AESEncryption.java

/**
 * Decrypt a cipher in bytes using the specified key
 * //from ww  w  .  j  a v  a2 s . com
 * @param cipherBytes encrypted bytes
 * @param key the key used in decryption
 * @param iv
 * @return
 * @throws Exception
 */
public static byte[] decrypt(byte[] cipherBytes, String key, byte[] iv) throws Exception {
    Cipher cipher = Cipher.getInstance(ENCRYPTION_ALGORITHM);
    AlgorithmParameters params = AlgorithmParameters.getInstance("AES");
    params.init(new IvParameterSpec(iv));
    cipher.init(Cipher.DECRYPT_MODE, getKey(key), params);
    return cipher.doFinal(cipherBytes);
}

From source file:encrypt.algorithms.AESCBC.java

public String decrypt(String key1, String key2, String encrypted) {
    try {/* ww  w .  j a v  a2 s . c o  m*/
        IvParameterSpec iv = new IvParameterSpec(key2.getBytes("UTF-8"));

        SecretKeySpec skeySpec = new SecretKeySpec(key1.getBytes("UTF-8"), "AES");
        Cipher cipher = Cipher.getInstance("AES/CBC/NOPADDING");
        cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
        byte[] original = cipher.doFinal(Base64.decodeBase64(encrypted));

        return new String(original);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return null;
}

From source file:aes_encryption.AES_Encryption.java

public static String decrypt(String strToDecrypt) {

    try {/*from   www  .  j  a  v  a 2 s .  c  om*/
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING");

        cipher.init(Cipher.DECRYPT_MODE, secretKey);
        setDecryptedString(new String(cipher.doFinal(Base64.decodeBase64(strToDecrypt))));

    } catch (Exception e) {

        System.out.println("Error while decrypting: " + e.toString());
    }
    return null;
}

From source file:architecture.common.BlowFishTest.java

@Test
public void testBlowfish() throws Exception {
    String Key = "password";
    byte[] KeyData = Key.getBytes();
    SecretKeySpec KS = new SecretKeySpec(KeyData, "Blowfish");
    Cipher cipher = Cipher.getInstance("Blowfish");
    cipher.init(Cipher.ENCRYPT_MODE, KS);

    // encrypt message
    String inputText = "MyTextToEncrypt";
    byte[] encrypted = cipher.doFinal(inputText.getBytes());
    String encryptedString = Hex.encodeHexString(encrypted);
    System.out.println(" : " + encryptedString);

    cipher.init(Cipher.DECRYPT_MODE, KS);
    byte[] decrypt = cipher.doFinal(Hex.decodeHex(encryptedString.toCharArray()));
    System.out.println("   : " + new String(decrypt));

}

From source file:com.sshutils.utils.CryptHelper.java

public static String decrypt(String strToDecrypt) {
    try {//from w  ww .j a  v  a 2s .  co m

        //                for (Provider provider: Security.getProviders()) {
        //  log.info(provider.getName());
        //  for (String key: provider.stringPropertyNames())
        //   log.info("\t" + key + "\t" + provider.getProperty(key));
        //}

        Cipher cipher = Cipher.getInstance(ENCRYPT_TYPE, "BC");
        final SecretKeySpec secretKey = new SecretKeySpec(key, "AES");
        cipher.init(Cipher.DECRYPT_MODE, secretKey);
        final String decryptedString = new String(cipher.doFinal(Base64.decodeBase64(strToDecrypt)));
        return decryptedString;
    } catch (Exception e) {
        log.error("Error while decrypting", e);

    }
    return null;
}

From source file:com.web.mavenproject6.utility.EncryptionUtil.java

public String decrypt(byte[] input) throws GeneralSecurityException, NoSuchPaddingException {
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.DECRYPT_MODE, skeySpec);
    return new String(cipher.doFinal(input));
}

From source file:Main.java

/**
 * This method should be used to decrypt a block of data.
 * //w  w  w  .  j a va 2 s  .  com
 * @param fileData the data to decrypt.
 * @param decryptionKey the key to initialize the cipher-algorithm.
 * @param decryptCompleted a flag, that indicates, that a multi-part decryption is to be
 *            completed. e.g. false, if the fileData consist of multiple
 *            parts. true, if the fileData consists only of one single part
 *            and so they could be decrypted in one operation.
 * 
 * @return the decrypted data.
 */
public static byte[] decryptData(byte[] fileData, byte[] decryptionKey, boolean decryptCompleted) {

    // Initializing may only be done at the start of a multi-part
    // crypto-operation.
    // if it's a single part crypto-operation initialization must always be
    // done.
    if (!decryptInitialized) {
        // Initializing the cipher
        try {
            decryptCipher = Cipher.getInstance("AES");
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

        try {
            SecretKeySpec keySpec = new SecretKeySpec(decryptionKey, "AES");
            decryptCipher.init(Cipher.DECRYPT_MODE, keySpec);
        } catch (Exception e) {
            Log.e(TAG, "Error while initializing decryption.");
            return null;
        }
        decryptInitialized = true;
    }

    // Decrypting
    try {
        if (!decryptCompleted) // done in case of multi-part operation
            fileData = decryptCipher.update(fileData);
        else
            // done in case of single part operation or to finish a
            // multi-part operation
            fileData = decryptCipher.doFinal(fileData);
    } catch (Exception e) {
        Log.e(TAG, "Error during decryption.");
        return null;
    }

    // at the and of an multi-part operation flags must be reset
    if (decryptCompleted)
        decryptInitialized = false;

    return fileData;
}

From source file:com.forsrc.utils.MyRsa2Utils.java

/**
 * Decrypt string./*from   ww w .  j a va2s  . c o  m*/
 *
 * @param privateKey the private key
 * @param cipherText the cipher text
 * @return the string
 * @throws RsaException the rsa exception
 */
public static String decrypt(PrivateKey privateKey, String cipherText) throws RsaException {
    Cipher cipher = null;
    try {
        cipher = Cipher.getInstance(RsaKey.ALGORITHM, new org.bouncycastle.jce.provider.BouncyCastleProvider());
    } catch (NoSuchAlgorithmException e) {
        throw new RsaException(e);
    } catch (NoSuchPaddingException e) {
        throw new RsaException(e);
    }
    try {
        cipher.init(Cipher.DECRYPT_MODE, privateKey);
    } catch (InvalidKeyException e) {
        throw new RsaException(e);
    }
    byte[] input = null;
    try {
        input = new Base64().decode(cipherText);
    } catch (Exception e) {
        throw new RsaException(e);
    }
    ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
    try {
        int blockSize = cipher.getBlockSize();
        blockSize = blockSize == 0 ? 117 : blockSize;
        int i = 0;
        int start = 0;

        do {
            start = i++ * blockSize;
            baos.write(cipher.doFinal(input, start, blockSize));
        } while (input.length - start - blockSize > 0);

    } catch (IllegalBlockSizeException e) {
        throw new RsaException(e);
    } catch (BadPaddingException e) {
        throw new RsaException(e);
    } catch (IOException e) {
        throw new RsaException(e);
    }
    return new String(baos.toByteArray());
}

From source file:com.commander4j.util.JCipher.java

public String decrypt(String encrypted) throws Exception {
    Cipher cipher = getCipher(Cipher.DECRYPT_MODE);
    byte[] plainBytes = cipher.doFinal(Base64.decodeBase64(encrypted));

    return new String(plainBytes);
}

From source file:ch.helmchen.camlapse.user.control.Encryption.java

public static String decrypt(String aBase64String) throws GeneralSecurityException, IOException {
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
    SecretKey key = keyFactory.generateSecret(new PBEKeySpec(PASSWORD));
    Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES");
    pbeCipher.init(Cipher.DECRYPT_MODE, key, new PBEParameterSpec(SALT, 20));
    return new String(pbeCipher.doFinal(base64Decode(aBase64String)));
}