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.anteam.demo.codec.cipher.symmetric.DESTest.java

License:asdf

@Test
public void testDES() throws Exception {
    SecretKey key = KeyGenerator.getInstance("DES").generateKey();

    // for CBC; must be 8 bytes
    byte[] initVector = new byte[] { 0x01, 0x02, 0x03, 0x04, 0x0F, 0x0E, 0x0D, 0x0C };

    AlgorithmParameterSpec algParamSpec = new IvParameterSpec(initVector);
    Cipher m_encrypter = Cipher.getInstance("DES/CBC/PKCS5Padding");
    Cipher m_decrypter = Cipher.getInstance("DES/CBC/PKCS5Padding");

    m_encrypter.init(Cipher.ENCRYPT_MODE, key, algParamSpec);
    m_decrypter.init(Cipher.DECRYPT_MODE, key, algParamSpec);

    byte[] clearText = "?????;(*)*$(R%*PDSJF>XJIPUFIWE(*#*&$)@#*"
            .getBytes();/* w w w  . j  av  a  2s.  c  o m*/

    System.out.println(clearText.length);

    byte[] encryptedText = m_encrypter.doFinal(clearText);
    System.out.println(encryptedText.length);

    byte[] decryptedText = m_decrypter.doFinal(encryptedText);
    System.out.println(decryptedText.length);

    System.out.println(new String(clearText));
    System.out.println(new String(encryptedText));
    System.out.println(new String(decryptedText));

}

From source file:net.sf.hajdbc.codec.crypto.CipherCodec.java

/**
 * {@inheritDoc}//from  w  ww  . ja v  a  2 s  . c  o m
 * @see net.sf.hajdbc.codec.Codec#decode(java.lang.String)
 */
@Override
public String decode(String value) throws SQLException {
    try {
        Cipher cipher = Cipher.getInstance(this.key.getAlgorithm());

        cipher.init(Cipher.DECRYPT_MODE, this.key);

        return new String(cipher.doFinal(Base64.decodeBase64(value.getBytes())));
    } catch (GeneralSecurityException e) {
        throw new SQLException(e);
    }
}

From source file:Main.java

@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
private static String decryptStringImpl(Context context, final String encryptedText) {
    String plainText = null;/*from   ww  w.  ja  v a 2s. c  o m*/
    try {
        final KeyStore keyStore = getKeyStore(context);

        PrivateKey privateKey = (PrivateKey) keyStore.getKey(KEY_ALIAS, null);

        String algorithm = ALGORITHM_OLD;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            algorithm = ALGORITHM;
        }
        Cipher cipher = Cipher.getInstance(algorithm);
        cipher.init(Cipher.DECRYPT_MODE, privateKey);

        CipherInputStream cipherInputStream = new CipherInputStream(
                new ByteArrayInputStream(Base64.decode(encryptedText, Base64.DEFAULT)), cipher);

        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        int b;
        while ((b = cipherInputStream.read()) != -1) {
            outputStream.write(b);
        }
        outputStream.close();
        plainText = outputStream.toString("UTF-8");
    } catch (Exception e) {
        e.printStackTrace();
    }
    return plainText;
}

From source file:com.soctec.soctec.utils.Encryptor.java

/**
 * Constructs the encryptor./* w  w w .j  ava 2 s  . c o m*/
 * Creates the needed keys and cipher for encryption.
 *
 */
public Encryptor() {

    try {
        keySpec = new DESKeySpec(keyString.getBytes("UTF8"));
        keyFactory = SecretKeyFactory.getInstance("DES");
        key = keyFactory.generateSecret(keySpec);

        ecipher = Cipher.getInstance("DES");
        decipher = Cipher.getInstance("DES");
        ecipher.init(Cipher.ENCRYPT_MODE, key);
        decipher.init(Cipher.DECRYPT_MODE, key);

    } catch (InvalidKeyException | UnsupportedEncodingException | NoSuchAlgorithmException
            | InvalidKeySpecException | NoSuchPaddingException e) {
        e.printStackTrace();
    }
}

From source file:com.wso2telco.proxy.util.DecryptAES.java

public static String decrypt(String encryptedText) throws NoSuchPaddingException, NoSuchAlgorithmException,
        InvalidKeyException, BadPaddingException, IllegalBlockSizeException, ConfigurationException {
    if (encryptionKey != null) {
        byte[] encryptionKeyByteValue = encryptionKey.getBytes();
        SecretKey secretKey = new SecretKeySpec(encryptionKeyByteValue, AuthProxyConstants.ASE_KEY);
        String decryptedText = null;
        if (encryptedText != null) {
            byte[] encryptedTextByte = Base64.decodeBase64(encryptedText);
            Cipher cipher = Cipher.getInstance("AES");

            cipher.init(Cipher.DECRYPT_MODE, secretKey);
            byte[] decryptedByte = cipher.doFinal(encryptedTextByte);
            decryptedText = new String(decryptedByte);
        }//from  w  ww  .  j ava 2  s.  c o  m
        return decryptedText;
    } else {
        throw new ConfigurationException("MSISDN EncryptionKey could not be found in mobile-connect.xml");
    }
}

From source file:de.hybris.platform.b2b.punchout.services.impl.SymmetricManager.java

public static String decrypt(final String encrypted, final String key) throws PunchOutCipherException {
    String decrypted = null;//from www . ja  v  a 2  s .c o  m

    try {
        final Key skeySpec = new SecretKeySpec(new Base64().decode(key), ALGORITHM);
        final Cipher cipher = Cipher.getInstance(ALGORITHM);
        cipher.init(Cipher.DECRYPT_MODE, skeySpec);

        final byte[] decodedValue = new Base64().decode(encrypted.getBytes());
        final byte[] decryptedValue = cipher.doFinal(decodedValue);
        decrypted = new String(decryptedValue);
    } catch (final NoSuchAlgorithmException | NoSuchPaddingException e) {
        // should never happen
        LOG.error("System was unable instantiate Cipher.", e);
    } catch (InvalidKeyException | BadPaddingException | IllegalBlockSizeException e) {
        final String msg = "Error occured during decryption." + e.getMessage();
        LOG.info(msg);
        throw new PunchOutCipherException(msg, e);
    }
    return decrypted;
}

From source file:com.rr.familyPlanning.ui.security.decryptObject.java

/**
 * Decrypts the String and serializes the object
 *
 * @param base64Data/*from  www  .ja  v a 2 s  .c o  m*/
 * @param base64IV
 * @return
 * @throws Exception
 */
public Object decryptObject(String base64Data, String base64IV) throws Exception {
    // Decode the data
    byte[] encryptedData = Base64.decodeBase64(base64Data.getBytes());

    // Decode the Init Vector
    byte[] rawIV = Base64.decodeBase64(base64IV.getBytes());

    // Configure the Cipher
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    IvParameterSpec ivSpec = new IvParameterSpec(rawIV);
    MessageDigest digest = MessageDigest.getInstance("SHA-256");
    digest.update(keyString.getBytes());
    byte[] key = new byte[16];
    System.arraycopy(digest.digest(), 0, key, 0, key.length);
    SecretKeySpec keySpec = new SecretKeySpec(key, "AES");
    cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);

    // Decrypt the data..
    byte[] decrypted = cipher.doFinal(encryptedData);

    // Deserialize the object
    ByteArrayInputStream stream = new ByteArrayInputStream(decrypted);

    ObjectInput in = new ObjectInputStream(stream);
    Object obj = null;
    try {
        obj = in.readObject();
    } finally {
        stream.close();
        in.close();
    }
    return obj;
}

From source file:com.intera.roostrap.util.EncryptionUtil.java

private static void encryptOrDecrypt(String encryptionKey, int mode, InputStream is, OutputStream os)
        throws InvalidKeyException, IOException {
    DESKeySpec keySpec = new DESKeySpec(toBytes(encryptionKey));

    SecretKey key = null;//w ww. ja  v  a2 s  .com
    Cipher cipher = null;
    try {
        SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("DES");
        key = secretKeyFactory.generateSecret(keySpec);
        cipher = Cipher.getInstance("DES");

    } catch (Exception e) {
        throw new RuntimeException(e);
    }

    if (mode == Cipher.ENCRYPT_MODE) {
        cipher.init(Cipher.ENCRYPT_MODE, key);
        CipherInputStream cis = new CipherInputStream(is, cipher);
        doCopy(cis, os);
    } else if (mode == Cipher.DECRYPT_MODE) {
        cipher.init(Cipher.DECRYPT_MODE, key);
        CipherOutputStream cos = new CipherOutputStream(os, cipher);
        doCopy(is, cos);
    }
}

From source file:com.creditcloud.common.security.impl.DESTextCipher.java

@Override
public void init(String salt) {
    try {/* w  w w . ja va2s  . co m*/
        SecretKey sk = keyFactory.generateSecret(new DESKeySpec(salt.getBytes()));
        encryptCipher.init(Cipher.ENCRYPT_MODE, sk);
        decryptCipher.init(Cipher.DECRYPT_MODE, sk);
    } catch (InvalidKeyException | InvalidKeySpecException ex) {
        logger.error("Can't init Cipher.[salt={}]", salt, ex);
    }
}

From source file:com.aast.encrypt.EncryptManager.java

public static String decryptAES(String key, String encrypted) {
    try {/*from  w w  w . ja v a 2s  .co m*/
        byte[] keyBytes = getKeyFromString(key);
        IvParameterSpec iv = new IvParameterSpec(keyBytes);
        SecretKeySpec skeySpec = new SecretKeySpec(keyBytes, "AES");

        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
        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;
}