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.netcrest.pado.internal.security.AESCipher.java

private static byte[] decryptBinaryToBinary(byte[] pke, byte[] encrypted) throws Exception {
    SecretKeySpec skeySpec = new SecretKeySpec(pke, "AES");
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.DECRYPT_MODE, skeySpec);
    return cipher.doFinal(encrypted);
}

From source file:JavaTron.JTP.java

/**
 * Decrypts an encrypted string/*  w ww. j a  v  a 2s . c  om*/
 * @param property
 * @return A plaintext string
 */
public static String decrypt(String property) {
    String p = new String();
    try {
        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));
        p = new String(pbeCipher.doFinal(base64Decode(property)));
    } catch (Exception e) {
        e.printStackTrace();
    }
    return p;
}

From source file:com.aqnote.shared.cryptology.symmetric.Blowfish.java

/**
 * ?CipherdoFinal?reset ???//  www.j av  a 2s.  c  o m
 * 
 * @param b ?
 * @return ?
 * @throws IllegalBlockSizeException
 * @throws BadPaddingException
 */
public synchronized byte[] decrypt(byte[] b) throws IllegalBlockSizeException, BadPaddingException {
    byte[] buffer = null;
    initCipher(decryptCipher, Cipher.DECRYPT_MODE, keySpec, paramSpec);
    buffer = decryptCipher.doFinal(b);
    return buffer;
}

From source file:com.diona.fileReader.CipherUtil.java

/**
 * Decrypts a given encrypted string./*from w ww  .  j av a  2  s .  com*/
 * 
 * @param bytes
 *          encrypted string to be decrypted.
 * @param context
 *          context to fetch preferences.
 * @return the original string.
 */
public byte[] decryptBytes(final byte[] bytes, final Context context) {
    // Transaction.checkLongRunningProcessing("decryptBytes");

    if (!ENCRYPTION_ENABLED) {
        return bytes;
    }

    byte[] decryptedTextBytes = null;
    try {
        final IvParameterSpec ivspec = new IvParameterSpec(getIV(context));
        final Cipher cipher = Cipher.getInstance(ENCRYPTION_ALGORITHM);
        cipher.init(Cipher.DECRYPT_MODE, getSecretKey(context), ivspec);
        decryptedTextBytes = cipher.doFinal(bytes);

    } catch (final Exception e) {
        Log.e(TAG, "e" + e);
    }
    return decryptedTextBytes;
}

From source file:com.springcryptoutils.core.cipher.symmetric.Base64EncodedCiphererWithStaticKeyImpl.java

/**
 * Encrypts/decrypts a message using the underlying symmetric key and mode.
 *
 * @param message if in encryption mode, the clear-text message to encrypt,
 *        otherwise a base64 encoded version of the raw byte array
 *        containing the message to decrypt
 * @return if in encryption mode, returns a base64 encoded version of the
 *         encrypted message, otherwise returns the clear-text message
 * @throws SymmetricEncryptionException on runtime errors
 * @see #setMode(Mode)//from ww w  . ja  v a 2 s  .  co m
 */
public String encrypt(String message) {
    try {
        final Cipher cipher = (((provider == null) || (provider.length() == 0))
                ? Cipher.getInstance(cipherAlgorithm)
                : Cipher.getInstance(cipherAlgorithm, provider));
        switch (mode) {
        case ENCRYPT:
            cipher.init(Cipher.ENCRYPT_MODE, keySpec, initializationVectorSpec);
            byte[] encryptedMessage = cipher.doFinal(message.getBytes(charsetName));
            return new String(Base64.encodeBase64(encryptedMessage, chunkOutput));
        case DECRYPT:
            cipher.init(Cipher.DECRYPT_MODE, keySpec, initializationVectorSpec);
            byte[] decodedMessage = Base64.decodeBase64(message);
            return new String(cipher.doFinal(decodedMessage), charsetName);
        default:
            return null;
        }
    } catch (Exception e) {
        throw new SymmetricEncryptionException("error encrypting/decrypting message; mode=" + mode, e);
    }
}

From source file:hh.learnj.test.license.test.rsa.RSATest.java

/**
 * ?/* w w w  . j  a v a  2  s.c om*/
 * 
 * @param target
 * @throws Exception
 */
static void decryptionByPrivateKey(String target) throws Exception {
    PrivateKey privateKey = getPrivateKey();
    Cipher cipher = Cipher.getInstance(privateKey.getAlgorithm());
    cipher.init(Cipher.DECRYPT_MODE, privateKey);
    cipher.update(decodeBase64(target));
    String source = new String(cipher.doFinal(), "UTF-8");
    System.out.println("???\r\n" + source);
}

From source file:com.nextep.designer.core.services.impl.RepositoryService.java

@Override
public String decrytPassword(String encryptedPassword) {
    Cipher cipher = getCipher();// www.ja  v  a  2 s .  c o m
    if (cipher != null) {
        try {
            cipher.init(Cipher.DECRYPT_MODE, key, pbeParamSpec);
            final byte[] bytesPassword = Base64.decode(encryptedPassword);
            final byte[] decryptedPassword = cipher.doFinal(bytesPassword);
            return new String(decryptedPassword);
        } catch (InvalidKeyException e) {
            throw new ErrorException(CoreMessages.getString("repositoryService.invalidEncryptionKey"), e); //$NON-NLS-1$
        } catch (IllegalBlockSizeException e) {
            throw new ErrorException(CoreMessages.getString("repositoryService.encodingProblem"), e); //$NON-NLS-1$
        } catch (BadPaddingException e) {
            throw new ErrorException(CoreMessages.getString("repositoryService.encodingProblem"), e); //$NON-NLS-1$
        } catch (InvalidAlgorithmParameterException e) {
            throw new ErrorException(CoreMessages.getString("repositoryService.encodingProblem"), e); //$NON-NLS-1$
        } catch (Base64DecodingException e) {
            throw new ErrorException(CoreMessages.getString("repositoryService.encodingProblem"), e); //$NON-NLS-1$
        }
    }
    return encryptedPassword;
}

From source file:eap.util.EDcodeUtil.java

public static byte[] desDecode(byte[] data, byte[] key) {
    return des(data, key, Cipher.DECRYPT_MODE);
}

From source file:compiler.downloader.MegaHandler.java

private int login_process(JSONObject json, long[] password_aes) throws IOException {

    String master_key_b64 = null;
    try {//from w ww .j  a v a 2 s.  c om
        master_key_b64 = json.getString("k");
    } catch (JSONException e) {
        e.printStackTrace();
    }
    if (master_key_b64 == null || master_key_b64.isEmpty()) {
        return -1;
    }

    long[] encrypted_master_key = MegaCrypt.base64_to_a32(master_key_b64);
    master_key = MegaCrypt.decrypt_key(encrypted_master_key, password_aes);

    if (json.has("csid")) {
        String encrypted_rsa_private_key_b64 = null;
        try {
            encrypted_rsa_private_key_b64 = json.getString("privk");
        } catch (JSONException e) {
            e.printStackTrace();
        }

        long[] encrypted_rsa_private_key = MegaCrypt.base64_to_a32(encrypted_rsa_private_key_b64);
        long[] rsa_private_key = MegaCrypt.decrypt_key(encrypted_rsa_private_key, master_key);
        String private_key = MegaCrypt.a32_to_str(rsa_private_key);

        BigInteger[] rsa_private_key1 = new BigInteger[4];
        for (int i = 0; i < 4; i++) {
            int l = ((((int) private_key.charAt(0)) * 256 + ((int) private_key.charAt(1)) + 7) / 8) + 2;
            rsa_private_key1[i] = MegaCrypt.mpi_to_int(private_key.substring(0, l));
            private_key = private_key.substring(l);
        }

        BigInteger encrypted_sid = null;
        try {
            encrypted_sid = MegaCrypt.mpi_to_int(MegaCrypt.base64_url_decode(json.getString("csid")));
        } catch (JSONException e) {
            e.printStackTrace();
        }

        BigInteger modulus = rsa_private_key1[0].multiply(rsa_private_key1[1]);
        BigInteger privateExponent = rsa_private_key1[2];

        BigInteger sid = null;
        try {
            PrivateKey privateKey = KeyFactory.getInstance("RSA")
                    .generatePrivate(new RSAPrivateKeySpec(modulus, privateExponent));
            Cipher cipher = Cipher.getInstance("RSA/ECB/NoPadding");
            cipher.init(Cipher.DECRYPT_MODE, privateKey);
            // PyCrypt can handle >256 bit length... what the fuck... sometimes i get 257
            if (encrypted_sid.toByteArray().length > 256) {
                Random rg = new Random();
                sequence_number = rg.nextInt(Integer.MAX_VALUE);
                return -2; // lets get a new seession
            }
            sid = new BigInteger(cipher.doFinal(encrypted_sid.toByteArray()));
        } catch (Exception e) {
            e.printStackTrace();
            return -1;
        }

        String sidS = sid.toString(16);
        if (sidS.length() % 2 != 0) {
            sidS = "0" + sidS;
        }
        try {
            byte[] sidsnohex = MegaCrypt.decodeHexString(sidS);
            this.sid = MegaCrypt.base64_url_encode(new String(sidsnohex, "ISO-8859-1").substring(0, 43));
        } catch (Exception e) {
            e.printStackTrace();
            return -1;
        }
    }
    return 0;
}

From source file:cn.util.RSAUtils.java

/**
 * ? //  www.  ja v  a 2 s  . c  o  m
 * 
 * @param data
 * @param privateKey
 * @return
 * @throws Exception
 */
public static String decryptByPrivateKey(String data) throws Exception {
    RSAPrivateKey privateKey = getPrivateKey();
    Cipher cipher = Cipher.getInstance("RSA");
    cipher.init(Cipher.DECRYPT_MODE, privateKey);
    //
    int key_len = privateKey.getModulus().bitLength() / 8;
    //      byte[] bytes = data.getBytes();
    //      byte[] bcd = ASCII_To_BCD(bytes, bytes.length);
    //      System.err.println(bcd.length);
    //      //?
    String ming = "";

    byte[][] arrays = splitArray(Base64Util.decryptBASE64(data), key_len);
    for (byte[] arr : arrays) {
        ming += new String(cipher.doFinal(arr));
    }

    return ming;

}