Search in sources :

Example 36 with IvParameterSpec

use of javax.crypto.spec.IvParameterSpec in project XobotOS by xamarin.

the class JCEStreamCipher method engineInit.

protected void engineInit(int opmode, Key key, AlgorithmParameterSpec params, SecureRandom random) throws InvalidKeyException, InvalidAlgorithmParameterException {
    CipherParameters param;
    this.pbeSpec = null;
    this.pbeAlgorithm = null;
    this.engineParams = null;
    //
    if (!(key instanceof SecretKey)) {
        throw new InvalidKeyException("Key for algorithm " + key.getAlgorithm() + " not suitable for symmetric enryption.");
    }
    if (key instanceof JCEPBEKey) {
        JCEPBEKey k = (JCEPBEKey) key;
        if (k.getOID() != null) {
            pbeAlgorithm = k.getOID().getId();
        } else {
            pbeAlgorithm = k.getAlgorithm();
        }
        if (k.getParam() != null) {
            param = k.getParam();
            pbeSpec = new PBEParameterSpec(k.getSalt(), k.getIterationCount());
        } else if (params instanceof PBEParameterSpec) {
            param = PBE.Util.makePBEParameters(k, params, cipher.getAlgorithmName());
            pbeSpec = (PBEParameterSpec) params;
        } else {
            throw new InvalidAlgorithmParameterException("PBE requires PBE parameters to be set.");
        }
        if (k.getIvSize() != 0) {
            ivParam = (ParametersWithIV) param;
        }
    } else if (params == null) {
        param = new KeyParameter(key.getEncoded());
    } else if (params instanceof IvParameterSpec) {
        param = new ParametersWithIV(new KeyParameter(key.getEncoded()), ((IvParameterSpec) params).getIV());
        ivParam = (ParametersWithIV) param;
    } else {
        throw new IllegalArgumentException("unknown parameter type.");
    }
    if ((ivLength != 0) && !(param instanceof ParametersWithIV)) {
        SecureRandom ivRandom = random;
        if (ivRandom == null) {
            ivRandom = new SecureRandom();
        }
        if ((opmode == Cipher.ENCRYPT_MODE) || (opmode == Cipher.WRAP_MODE)) {
            byte[] iv = new byte[ivLength];
            ivRandom.nextBytes(iv);
            param = new ParametersWithIV(param, iv);
            ivParam = (ParametersWithIV) param;
        } else {
            throw new InvalidAlgorithmParameterException("no IV set when one expected");
        }
    }
    switch(opmode) {
        case Cipher.ENCRYPT_MODE:
        case Cipher.WRAP_MODE:
            cipher.init(true, param);
            break;
        case Cipher.DECRYPT_MODE:
        case Cipher.UNWRAP_MODE:
            cipher.init(false, param);
            break;
        default:
            System.out.println("eeek!");
    }
}
Also used : CipherParameters(org.bouncycastle.crypto.CipherParameters) ParametersWithIV(org.bouncycastle.crypto.params.ParametersWithIV) SecretKey(javax.crypto.SecretKey) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) KeyParameter(org.bouncycastle.crypto.params.KeyParameter) SecureRandom(java.security.SecureRandom) IvParameterSpec(javax.crypto.spec.IvParameterSpec) InvalidKeyException(java.security.InvalidKeyException) PBEParameterSpec(javax.crypto.spec.PBEParameterSpec)

Example 37 with IvParameterSpec

use of javax.crypto.spec.IvParameterSpec in project android_frameworks_base by ResurrectionRemix.

the class CryptoHelper method decryptBundle.

@Nullable
/* default */
Bundle decryptBundle(@NonNull Bundle bundle) throws GeneralSecurityException {
    Preconditions.checkNotNull(bundle, "Cannot decrypt null bundle.");
    byte[] iv = bundle.getByteArray(KEY_IV);
    byte[] encryptedBytes = bundle.getByteArray(KEY_CIPHER);
    byte[] mac = bundle.getByteArray(KEY_MAC);
    if (!verifyMac(encryptedBytes, iv, mac)) {
        Log.w(TAG, "Escrow mac mismatched!");
        return null;
    }
    IvParameterSpec ivSpec = new IvParameterSpec(iv);
    Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
    cipher.init(Cipher.DECRYPT_MODE, mEncryptionKey, ivSpec);
    byte[] decryptedBytes = cipher.doFinal(encryptedBytes);
    Parcel decryptedParcel = Parcel.obtain();
    decryptedParcel.unmarshall(decryptedBytes, 0, decryptedBytes.length);
    decryptedParcel.setDataPosition(0);
    Bundle decryptedBundle = new Bundle();
    decryptedBundle.readFromParcel(decryptedParcel);
    decryptedParcel.recycle();
    return decryptedBundle;
}
Also used : Parcel(android.os.Parcel) Bundle(android.os.Bundle) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) Nullable(android.annotation.Nullable)

Example 38 with IvParameterSpec

use of javax.crypto.spec.IvParameterSpec in project wechat by motianhuo.

the class DES method decryptDES.

/**
	 * 数据解密
	 * 
	 * @param decryptString
	 * @return
	 * @throws Exception
	 */
public static String decryptDES(String decryptString) throws Exception {
    byte[] byteMi = new BASE64().decode(decryptString);
    IvParameterSpec zeroIv = new IvParameterSpec(iv);
    SecretKeySpec key = new SecretKeySpec(getkeys().getBytes(), "DES");
    Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
    cipher.init(Cipher.DECRYPT_MODE, key, zeroIv);
    byte[] decryptedData = cipher.doFinal(byteMi);
    return new String(decryptedData);
}
Also used : SecretKeySpec(javax.crypto.spec.SecretKeySpec) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher)

Example 39 with IvParameterSpec

use of javax.crypto.spec.IvParameterSpec in project perun by CESNET.

the class Utils method cipherInput.

/**
	 * Return en/decrypted version of input using AES/CBC/PKCS5PADDING cipher.
	 * Perun's internal secretKey and initVector are used (you can configure them in
	 * perun.properties file).
	 *
	 * @param plainText text to en/decrypt
	 * @param decrypt TRUE = decrypt input / FALSE = encrypt input
	 * @return en/decrypted text
	 * @throws cz.metacentrum.perun.core.api.exceptions.InternalErrorException if anything fails
	 */
public static String cipherInput(String plainText, boolean decrypt) throws InternalErrorException {
    try {
        String encryptionKey = BeansUtils.getCoreConfig().getPwdresetSecretKey();
        String initVector = BeansUtils.getCoreConfig().getPwdresetInitVector();
        Cipher c = Cipher.getInstance("AES/CBC/PKCS5PADDING");
        SecretKeySpec k = new SecretKeySpec(encryptionKey.getBytes("UTF-8"), "AES");
        c.init((decrypt) ? Cipher.DECRYPT_MODE : Cipher.ENCRYPT_MODE, k, new IvParameterSpec(initVector.getBytes("UTF-8")));
        if (decrypt) {
            byte[] bytes = Base64.decodeBase64(plainText.getBytes("UTF-8"));
            return new String(c.doFinal(bytes), "UTF-8");
        } else {
            byte[] bytes = Base64.encodeBase64(c.doFinal(plainText.getBytes("UTF-8")));
            return new String(bytes, "UTF-8");
        }
    } catch (Exception ex) {
        throw new InternalErrorException("Error when encrypting message", ex);
    }
}
Also used : SecretKeySpec(javax.crypto.spec.SecretKeySpec) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) SQLException(java.sql.SQLException) MalformedURLException(java.net.MalformedURLException) IllegalArgumentException(java.lang.IllegalArgumentException)

Example 40 with IvParameterSpec

use of javax.crypto.spec.IvParameterSpec in project graylog2-server by Graylog2.

the class AESTools method decrypt.

@Nullable
public static String decrypt(String cipherText, String encryptionKey, String salt) {
    try {
        @SuppressFBWarnings("CIPHER_INTEGRITY") Cipher cipher = Cipher.getInstance("AES/CBC/ISO10126Padding", "SunJCE");
        SecretKeySpec key = new SecretKeySpec(encryptionKey.getBytes("UTF-8"), "AES");
        cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(salt.getBytes("UTF-8")));
        return new String(cipher.doFinal(Hex.decode(cipherText)), "UTF-8");
    } catch (Exception e) {
        LOG.error("Could not decrypt value.", e);
    }
    return null;
}
Also used : SecretKeySpec(javax.crypto.spec.SecretKeySpec) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) Nullable(javax.annotation.Nullable)

Aggregations

IvParameterSpec (javax.crypto.spec.IvParameterSpec)229 Cipher (javax.crypto.Cipher)150 SecretKeySpec (javax.crypto.spec.SecretKeySpec)107 SecretKey (javax.crypto.SecretKey)49 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)46 InvalidKeyException (java.security.InvalidKeyException)43 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)42 AlgorithmParameterSpec (java.security.spec.AlgorithmParameterSpec)39 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)30 BadPaddingException (javax.crypto.BadPaddingException)28 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)25 Key (java.security.Key)21 KeyGenerator (javax.crypto.KeyGenerator)21 IOException (java.io.IOException)19 SecureRandom (java.security.SecureRandom)17 GeneralSecurityException (java.security.GeneralSecurityException)15 MyCipher (org.apache.harmony.crypto.tests.support.MyCipher)15 PBEParameterSpec (javax.crypto.spec.PBEParameterSpec)14 MessageDigest (java.security.MessageDigest)13 KeyParameter (org.bouncycastle.crypto.params.KeyParameter)13