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.paypal.utilities.AESDecrypt.java

public AESDecrypt(String encryptedString) throws Exception {
    SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
    KeySpec keySpec = new PBEKeySpec(passphrase.toCharArray(), SALT, ITERATION_COUNT, KEY_LENGTH);
    SecretKey secretKeyTemp = secretKeyFactory.generateSecret(keySpec);
    SecretKey secretKey = new SecretKeySpec(secretKeyTemp.getEncoded(), "AES");

    encrypt = Base64.decodeBase64(encryptedString.getBytes());

    eCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    eCipher.init(Cipher.ENCRYPT_MODE, secretKey);

    byte[] iv = extractIV();
    dCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    dCipher.init(Cipher.DECRYPT_MODE, secretKey, new IvParameterSpec(iv));
}

From source file:com.comcast.cdn.traffic_control.traffic_router.core.util.StringProtector.java

public StringProtector(final String passwd) throws GeneralSecurityException {
    final SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
    final SecretKey key = keyFactory.generateSecret(new PBEKeySpec(passwd.toCharArray()));
    encryptor = Cipher.getInstance("PBEWithMD5AndDES");
    encryptor.init(Cipher.ENCRYPT_MODE, key, new PBEParameterSpec(SALT, 20));

    decryptor = Cipher.getInstance("PBEWithMD5AndDES");
    decryptor.init(Cipher.DECRYPT_MODE, key, new PBEParameterSpec(SALT, 20));
}

From source file:com.sds.acube.ndisc.mts.xserver.util.XNDiscCipher.java

/**
 * /*from   w w  w .j  a v  a 2s. co  m*/
 * 
 * @param data ? ?
 * @return ? ?
 */
public static String decode(String data) {
    try {
        Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, key);
        byte d[] = cipher.doFinal(Base64.decodeBase64(data.getBytes("UTF-8")));
        return replaceChar(new String(d));
    } catch (NoSuchAlgorithmException e) {
        System.err.print(XNDiscUtils.printStackTrace(e));
    } catch (NoSuchPaddingException e) {
        System.err.print(XNDiscUtils.printStackTrace(e));
    } catch (IOException e) {
        System.err.print(XNDiscUtils.printStackTrace(e));
    } catch (InvalidKeyException e) {
        System.err.print(XNDiscUtils.printStackTrace(e));
    } catch (IllegalBlockSizeException e) {
        System.err.print(XNDiscUtils.printStackTrace(e));
    } catch (BadPaddingException e) {
        System.err.print(XNDiscUtils.printStackTrace(e));
    }
    return null;
}

From source file:Main.java

/**
 * More flexible AES decrypt that doesn't encode
 *
 * @param key AES key typically 128, 192 or 256 bit
 * @param iv Initiation Vector/*from w w  w .  j av a  2 s. co m*/
 * @param decodedCipherText in bytes (assumed it's already been decoded)
 * @return Decrypted message cipher text (not encoded)
 * @throws GeneralSecurityException if something goes wrong during encryption
 */
public static byte[] decrypt(final SecretKeySpec key, final byte[] iv, final byte[] decodedCipherText)
        throws GeneralSecurityException {
    final Cipher cipher = Cipher.getInstance(AES_MODE);
    IvParameterSpec ivSpec = new IvParameterSpec(iv);
    cipher.init(Cipher.DECRYPT_MODE, key, ivSpec);
    byte[] decryptedBytes = cipher.doFinal(decodedCipherText);

    log("decryptedBytes", decryptedBytes);

    return decryptedBytes;
}

From source file:com.tesora.dve.common.PECryptoUtils.java

public static String decrypt(String str) throws PEException {
    if (StringUtils.isBlank(str))
        return str;

    try {/*from w w w.j av a  2s.  c o m*/
        Cipher cipher = createCrypter(Cipher.DECRYPT_MODE);

        return new String(cipher.doFinal(Base64.decodeBase64(str)), "UTF8");
    } catch (Exception e) {
        throw new PEException("Failed to decrypt '" + str + "'", e);
    }
}

From source file:com.myapp.common.AES4MEncrypt.java

/**
 * /*from ww  w. j a v  a2  s. c  om*/
 * 
 * @param sSrc ?
 * @param sKey KEY
 * @return
 * @throws Exception
 * @author cdduqiang
 * @date 201443
 */
public static String decrypt(String sSrc, String sKey) throws Exception {
    if (sKey == null) {
        log.error("Decrypt Key ??");
        throw new Exception("Decrypt Key ??");
    }

    byte[] raw = hex2byte(sKey);
    SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    IvParameterSpec iv = new IvParameterSpec("0102030405060708".getBytes());
    cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
    byte[] encrypted1 = hex2byte(sSrc);

    byte[] original = cipher.doFinal(encrypted1);
    return new String(original, ENCODING);// anslBytes2String(original);
}

From source file:mx.bigdata.sat.cfdi.CFDv3Debugger.java

private void dumpDigests() throws Exception {
    System.err.println(cfd.getCadenaOriginal());
    String certStr = cfd.document.getCertificado();
    Base64 b64 = new Base64();
    byte[] cbs = b64.decode(certStr);
    X509Certificate cert = (X509Certificate) KeyLoaderFactory
            .createInstance(KeyLoaderEnumeration.PUBLIC_KEY_LOADER, new ByteArrayInputStream(cbs)).getKey();
    cert.checkValidity();/*from   w  w  w  .j a v a  2s  . co  m*/
    String sigStr = cfd.document.getSello();
    byte[] signature = b64.decode(sigStr);
    CFDv3.dump("Digestion firmada", signature, System.err);
    Cipher dec = Cipher.getInstance("RSA");
    dec.init(Cipher.DECRYPT_MODE, cert);
    byte[] result = dec.doFinal(signature);
    CFDv3.dump("Digestion decriptada", result, System.err);
    ASN1InputStream aIn = new ASN1InputStream(result);
    ASN1Sequence seq = (ASN1Sequence) aIn.readObject();
    ASN1OctetString sigHash = (ASN1OctetString) seq.getObjectAt(1);
    CFDv3.dump("Sello", sigHash.getOctets(), System.err);
}

From source file:in.mtap.iincube.mongoser.codec.crypto.Psyfer.java

public static Psyfer getInstance(String secretKey) throws NoSuchAlgorithmException,
        UnsupportedEncodingException, NoSuchPaddingException, InvalidKeyException {
    MessageDigest digest = MessageDigest.getInstance("SHA-1");
    byte[] key = digest.digest(secretKey.getBytes("UTF-8"));
    key = Arrays.copyOf(key, 16);
    SecretKeySpec secretKeySpec = new SecretKeySpec(key, "AES");
    Cipher eCipher = Cipher.getInstance("AES");
    Cipher deCipher = Cipher.getInstance("AES");
    eCipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
    deCipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
    return new Psyfer(eCipher, deCipher);
}

From source file:com.bytecode.util.Crypto.java

private static byte[] decrypt(String keystring, byte[] message, int bits) throws Exception {
    byte[] decValue = null;
    byte[] nonceBytes = Arrays.copyOf(Arrays.copyOf(message, 8), 16);
    IvParameterSpec nonce = new IvParameterSpec(nonceBytes);

    Key key = generateKey(keystring, bits);
    Cipher c = Cipher.getInstance(ALGORITHM);
    c.init(Cipher.DECRYPT_MODE, key, nonce);
    decValue = c.doFinal(message, 8, message.length - 8);

    return decValue;
}

From source file:com.wabacus.util.DesEncryptTools.java

public static String decrypt(String encryptedString) {
    try {//w  w  w.j a va  2  s.c  o  m
        if (KEY_OBJ == null) {
            log.warn("" + encryptedString);
            return encryptedString;
        }
        byte[] b = base64Decode(encryptedString);
        Cipher c1 = Cipher.getInstance(Algorithm);
        c1.init(Cipher.DECRYPT_MODE, KEY_OBJ);
        return new String(c1.doFinal(b));
    } catch (Exception e) {
        throw new WabacusConfigLoadingException("" + encryptedString + "", e);
    }
}