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:adminpassword.Decryption.java

@SuppressWarnings("static-access")
public String decrypt(String encryptedText, String idKey) throws Exception {
    String password = idKey;/* w  w  w.  ja v a  2s .  c  om*/

    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");

    //strip off the salt and iv
    ByteBuffer buffer = ByteBuffer.wrap(new Base64().decode(encryptedText));
    byte[] saltBytes = new byte[20];
    buffer.get(saltBytes, 0, saltBytes.length);
    byte[] ivBytes1 = new byte[cipher.getBlockSize()];
    buffer.get(ivBytes1, 0, ivBytes1.length);
    byte[] encryptedTextBytes = new byte[buffer.capacity() - saltBytes.length - ivBytes1.length];
    buffer.get(encryptedTextBytes);

    // Deriving the key
    SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
    PBEKeySpec spec = new PBEKeySpec(password.toCharArray(), saltBytes, 65556, 256);

    SecretKey secretKey = factory.generateSecret(spec);
    SecretKeySpec secret = new SecretKeySpec(secretKey.getEncoded(), "AES");
    cipher.init(Cipher.DECRYPT_MODE, secret, new IvParameterSpec(ivBytes1));

    byte[] decryptedTextBytes = null;
    try {
        decryptedTextBytes = cipher.doFinal(encryptedTextBytes);

    } catch (IllegalBlockSizeException e) {
        e.printStackTrace();
    } catch (BadPaddingException e) {
        e.printStackTrace();
    }

    return new String(decryptedTextBytes);
}

From source file:com.CardPaymentGateway.Decrypt.java

public String DecrypData(String MyDecrypData) {
    String DecryptedData = "";
    try {/*  w  w  w .j a v  a 2 s.  co m*/
        AlgorithmParameterSpec paramSpec = new IvParameterSpec(strPassword.getBytes());
        //Whatever you want to encrypt/decrypt using AES /CBC padding
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");

        //You can use ENCRYPT_MODE or DECRYPT_MODE
        cipher.init(Cipher.DECRYPT_MODE, key, paramSpec);

        //decode data using standard decoder
        byte[] output = new BASE64Decoder().decodeBuffer(MyDecrypData);

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

        //                  System.out.println("Original string: " +
        //                          new String(input));
        //                  
        // decryptedData .;
        //System.out.println("Decrypted string: " + new String(decrypted));
        DecryptedData = new String(decrypted);

    } catch (NoSuchAlgorithmException ex) {
        Logger.getLogger(Decrypt.class.getName()).log(Level.SEVERE, null, ex);
    } catch (NoSuchPaddingException ex) {
        Logger.getLogger(Decrypt.class.getName()).log(Level.SEVERE, null, ex);
    } catch (InvalidKeyException ex) {
        Logger.getLogger(Decrypt.class.getName()).log(Level.SEVERE, null, ex);
    } catch (InvalidAlgorithmParameterException ex) {
        Logger.getLogger(Decrypt.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IllegalBlockSizeException ex) {
        Logger.getLogger(Decrypt.class.getName()).log(Level.SEVERE, null, ex);
    } catch (BadPaddingException ex) {
        Logger.getLogger(Decrypt.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(Decrypt.class.getName()).log(Level.SEVERE, null, ex);
    }
    return DecryptedData;
}

From source file:com.wso2telco.cryptosystem.AESencrp.java

/**
 * Decrypt./*from   www  .j a v  a2s.c  om*/
 *
 * @param encryptedData the encrypted data
 * @return the string
 * @throws Exception the exception
 */
public static String decrypt(String encryptedData) throws Exception {
    Key key = generateKey();
    Cipher c = Cipher.getInstance(ALGO);
    c.init(Cipher.DECRYPT_MODE, key);
    //byte[] decordedValue = new BASE64Decoder().decodeBuffer(encryptedData);
    byte[] decordedValue = Base64.decodeBase64(encryptedData.getBytes());
    byte[] decValue = c.doFinal(decordedValue);
    String decryptedValue = new String(decValue);
    return decryptedValue;
}

From source file:com.app.utils.StringEncrypt.java

/**
 * Funcin de tipo String que recibe una llave (key), un vector de inicializacin (iv)
 * y el texto que se desea descifrar//from  w  w  w.  j  av  a  2  s. c  om
 * @param key la llave en tipo String a utilizar
 * @param iv el vector de inicializacin a utilizar
 * @param encrypted el texto cifrado en modo String
 * @return el texto desencriptado en modo String
 * @throws Exception puede devolver excepciones de los siguientes tipos: NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException
 */
public static String decrypt(String key, String iv, String encrypted) throws Exception {
    Cipher cipher = Cipher.getInstance(cI);
    SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes(), alg);
    IvParameterSpec ivParameterSpec = new IvParameterSpec(iv.getBytes());
    byte[] enc = decodeBase64(encrypted);
    cipher.init(Cipher.DECRYPT_MODE, skeySpec, ivParameterSpec);
    byte[] decrypted = cipher.doFinal(enc);
    return new String(decrypted);
}

From source file:license.rsa.WakeRSA.java

/**
 * rsa/*from  w  ww  .  j a v  a  2 s.c  o  m*/
 * @param key
 * @return
 * @throws Exception
 */
private static byte[] rsa(byte[] key) throws Exception {
    PublicKey pubKey = readPublicKeyFromFile();
    Cipher cipher = Cipher.getInstance("RSA");
    cipher.init(Cipher.DECRYPT_MODE, pubKey);
    return cipher.doFinal(key);
}

From source file:com.javiermoreno.springboot.rest.CryptographyServiceImplBlowfish.java

public CryptographyServiceImplBlowfish()
        throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {
    KeyGenerator keyGenerator = KeyGenerator.getInstance("Blowfish");
    keyGenerator.init(128);/*from  w w w .  jav  a 2 s . c om*/
    key = keyGenerator.generateKey();
    cipherEncrypt = Cipher.getInstance("Blowfish/ECB/PKCS5Padding");
    cipherEncrypt.init(Cipher.ENCRYPT_MODE, key);
    cipherDecrypt = Cipher.getInstance("Blowfish/ECB/PKCS5Padding");
    cipherDecrypt.init(Cipher.DECRYPT_MODE, key);
}

From source file:de.taimos.dvalin.interconnect.model.MessageCryptoUtil.java

/**
 *
 * @param data the BASE 64 data//from w  ww  .j  a  v  a2  s. co m
 * @return the decrypted data
 * @throws CryptoException on decryption error
 */
public static String decrypt(final String data) throws CryptoException {
    if (data == null) {
        return null;
    }

    try {
        final Cipher cipher = MessageCryptoUtil.getCipher(Cipher.DECRYPT_MODE);
        return new String(cipher.doFinal(Base64.decodeBase64(data)), Charset.forName("UTF-8"));
    } catch (final Exception e) {
        throw new CryptoException("Decryption of data failed!", e);
    }
}

From source file:gsn.http.ac.Protector.java

public static String decrypt(String value) throws Exception {
    Key key = generateKey();/*  w w w  .j  av  a  2s.com*/
    String salt = getSalt();
    Cipher c = Cipher.getInstance(ALGORITHM);
    c.init(Cipher.DECRYPT_MODE, key);

    String dValue = null;
    String valueToDecrypt = value;
    for (int i = 0; i < ITERATIONS; i++) {
        byte[] decordedValue = new sun.misc.BASE64Decoder().decodeBuffer(valueToDecrypt);
        //byte[] decordedValue = Base64.decodeBase64(valueToDecrypt);
        byte[] decValue = c.doFinal(decordedValue);
        dValue = new String(decValue).substring(salt.length());
        valueToDecrypt = dValue;
    }
    return dValue;
}

From source file:net.ftb.util.CryptoUtils.java

/**
 * Method to AES decrypt string if fails, will attempt to use {@link #decryptLegacy(String str, byte[] key)}
 * @param str string to decrypt//from  w  ww .j  ava  2s  .  c  o m
 * @param key decryption key key
 * @return decrypted string or "" if legacy fails
 */
public static String decrypt(String str, byte[] key) {
    try {
        Cipher aes = Cipher.getInstance("AES");
        aes.init(Cipher.DECRYPT_MODE, new SecretKeySpec(pad(key), "AES"));
        String s = new String(aes.doFinal(Base64.decodeBase64(str)), "utf8");
        if (s.startsWith("FDT:") && s.length() > 4)
            return s.split(":", 2)[1];//we don't want the decryption test
        else
            return decryptLegacy(str, key);
    } catch (Exception e) {
        Logger.logError("Error Decrypting information, attempting legacy decryption", e);
        return decryptLegacy(str, key);
    }
}

From source file:com.thoughtworks.go.server.util.EncryptionHelper.java

public static String decryptUsingRSA(String cipherText, String privateKeyContent)
        throws NoSuchPaddingException, NoSuchAlgorithmException, IOException, InvalidKeySpecException,
        BadPaddingException, IllegalBlockSizeException, InvalidKeyException {
    Cipher decryptCipher = Cipher.getInstance("RSA");
    decryptCipher.init(Cipher.DECRYPT_MODE, getRSAPrivateKeyFrom(privateKeyContent));
    return new String(decryptCipher.doFinal(Base64.getDecoder().decode(cipherText)), UTF_8);
}