Search in sources :

Example 91 with IvParameterSpec

use of javax.crypto.spec.IvParameterSpec in project jdk8u_jdk by JetBrains.

the class TestAESCipher method runTest.

public void runTest(String algo, String mo, String pad) throws Exception {
    Cipher ci = null;
    byte[] iv = null;
    AlgorithmParameterSpec aps = null;
    SecretKey key = null;
    try {
        // Initialization
        Random rdm = new Random();
        byte[] plainText = new byte[128];
        rdm.nextBytes(plainText);
        ci = Cipher.getInstance(algo + "/" + mo + "/" + pad, PROVIDER);
        KeyGenerator kg = KeyGenerator.getInstance(algo, PROVIDER);
        kg.init(KEY_LENGTH);
        key = kg.generateKey();
        // encrypt
        if (!mo.equalsIgnoreCase("GCM")) {
            ci.init(Cipher.ENCRYPT_MODE, key, aps);
        } else {
            ci.init(Cipher.ENCRYPT_MODE, key);
        }
        byte[] cipherText = new byte[ci.getOutputSize(plainText.length)];
        int offset = ci.update(plainText, 0, plainText.length, cipherText, 0);
        ci.doFinal(cipherText, offset);
        if (!mo.equalsIgnoreCase("ECB")) {
            iv = ci.getIV();
            aps = new IvParameterSpec(iv);
        } else {
            aps = null;
        }
        if (!mo.equalsIgnoreCase("GCM")) {
            ci.init(Cipher.DECRYPT_MODE, key, aps);
        } else {
            ci.init(Cipher.DECRYPT_MODE, key, ci.getParameters());
        }
        byte[] recoveredText = new byte[ci.getOutputSize(cipherText.length)];
        int len = ci.doFinal(cipherText, 0, cipherText.length, recoveredText);
        byte[] tmp = new byte[len];
        System.arraycopy(recoveredText, 0, tmp, 0, len);
        // Comparison
        if (!java.util.Arrays.equals(plainText, tmp)) {
            System.out.println("Original: ");
            dumpBytes(plainText);
            System.out.println("Recovered: ");
            dumpBytes(tmp);
            throw new RuntimeException("Original text is not equal with recovered text, with mode:" + mo);
        }
    } catch (NoSuchAlgorithmException e) {
        //CFB7 and OFB150 are for negative testing
        if (!mo.equalsIgnoreCase("CFB7") && !mo.equalsIgnoreCase("OFB150")) {
            System.out.println("Unexpected NoSuchAlgorithmException with mode: " + mo);
            throw new RuntimeException("Test failed!");
        }
    } catch (NoSuchProviderException | NoSuchPaddingException | InvalidKeyException | InvalidAlgorithmParameterException | ShortBufferException | IllegalBlockSizeException | BadPaddingException e) {
        System.out.println("Test failed!");
        throw e;
    }
}
Also used : InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) BadPaddingException(javax.crypto.BadPaddingException) InvalidKeyException(java.security.InvalidKeyException) SecretKey(javax.crypto.SecretKey) Random(java.util.Random) ShortBufferException(javax.crypto.ShortBufferException) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) NoSuchProviderException(java.security.NoSuchProviderException) AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec) KeyGenerator(javax.crypto.KeyGenerator)

Example 92 with IvParameterSpec

use of javax.crypto.spec.IvParameterSpec in project jdk8u_jdk by JetBrains.

the class PBES2Parameters method parseES.

private String parseES(DerValue encryptionScheme) throws IOException {
    String cipherAlgo = null;
    cipherAlgo_OID = encryptionScheme.data.getOID();
    if (aes128CBC_OID.equals(cipherAlgo_OID)) {
        cipherAlgo = "AES_128";
        // parameter is AES-IV 'OCTET STRING (SIZE(16))'
        cipherParam = new IvParameterSpec(encryptionScheme.data.getOctetString());
        keysize = 128;
    } else if (aes256CBC_OID.equals(cipherAlgo_OID)) {
        cipherAlgo = "AES_256";
        // parameter is AES-IV 'OCTET STRING (SIZE(16))'
        cipherParam = new IvParameterSpec(encryptionScheme.data.getOctetString());
        keysize = 256;
    } else {
        throw new IOException("PBE parameter parsing error: " + "expecting the object identifier for AES cipher");
    }
    return cipherAlgo;
}
Also used : IvParameterSpec(javax.crypto.spec.IvParameterSpec)

Example 93 with IvParameterSpec

use of javax.crypto.spec.IvParameterSpec in project jdk8u_jdk by JetBrains.

the class CipherHelper method getInitializedDes.

/**
     * Obtains an initialized DES cipher.
     *
     * @param encryptMode true if encryption is desired, false is decryption
     * is desired.
     * @param key the bytes for the DES key
     * @param ivBytes the initial vector bytes
     */
private final Cipher getInitializedDes(boolean encryptMode, byte[] key, byte[] ivBytes) throws GSSException {
    try {
        IvParameterSpec iv = new IvParameterSpec(ivBytes);
        SecretKey jceKey = (SecretKey) (new SecretKeySpec(key, "DES"));
        Cipher desCipher = Cipher.getInstance("DES/CBC/NoPadding");
        desCipher.init((encryptMode ? Cipher.ENCRYPT_MODE : Cipher.DECRYPT_MODE), jceKey, iv);
        return desCipher;
    } catch (GeneralSecurityException e) {
        GSSException ge = new GSSException(GSSException.FAILURE, -1, e.getMessage());
        ge.initCause(e);
        throw ge;
    }
}
Also used : SecretKey(javax.crypto.SecretKey) SecretKeySpec(javax.crypto.spec.SecretKeySpec) GeneralSecurityException(java.security.GeneralSecurityException) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher)

Example 94 with IvParameterSpec

use of javax.crypto.spec.IvParameterSpec in project bamboobsc by billchen198318.

the class EncryptorUtils method decrypt.

public static String decrypt(String key1, String iv1, String encrypted) {
    try {
        IvParameterSpec iv = new IvParameterSpec(iv1.getBytes(Constants.BASE_ENCODING));
        SecretKeySpec skeySpec = new SecretKeySpec(key1.getBytes(Constants.BASE_ENCODING), "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;
}
Also used : SecretKeySpec(javax.crypto.spec.SecretKeySpec) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher)

Example 95 with IvParameterSpec

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

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)

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