Search in sources :

Example 56 with AlgorithmParameterSpec

use of java.security.spec.AlgorithmParameterSpec in project jdk8u_jdk by JetBrains.

the class Padding 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 {
        Random rdm = new Random();
        byte[] plainText;
        ci = Cipher.getInstance(algo + "/" + mo + "/" + pad, PROVIDER);
        KeyGenerator kg = KeyGenerator.getInstance(algo, PROVIDER);
        kg.init(KEY_LENGTH);
        key = kg.generateKey();
        for (int i = 0; i < 15; i++) {
            plainText = new byte[1600 + i + 1];
            rdm.nextBytes(plainText);
            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];
            for (int j = 0; j < len; j++) {
                tmp[j] = recoveredText[j];
            }
            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 57 with AlgorithmParameterSpec

use of java.security.spec.AlgorithmParameterSpec 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 58 with AlgorithmParameterSpec

use of java.security.spec.AlgorithmParameterSpec in project jdk8u_jdk by JetBrains.

the class DOMCanonicalizationMethod method hashCode.

@Override
public int hashCode() {
    int result = 17;
    result = 31 * result + getAlgorithm().hashCode();
    AlgorithmParameterSpec spec = getParameterSpec();
    if (spec != null) {
        result = 31 * result + spec.hashCode();
    }
    return result;
}
Also used : AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec)

Example 59 with AlgorithmParameterSpec

use of java.security.spec.AlgorithmParameterSpec in project jdk8u_jdk by JetBrains.

the class DOMTransform method hashCode.

@Override
public int hashCode() {
    int result = 17;
    result = 31 * result + getAlgorithm().hashCode();
    AlgorithmParameterSpec spec = getParameterSpec();
    if (spec != null) {
        result = 31 * result + spec.hashCode();
    }
    return result;
}
Also used : AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec)

Example 60 with AlgorithmParameterSpec

use of java.security.spec.AlgorithmParameterSpec in project jdk8u_jdk by JetBrains.

the class Cipher method checkCryptoPerm.

private void checkCryptoPerm(CipherSpi checkSpi, Key key, AlgorithmParameters params) throws InvalidKeyException, InvalidAlgorithmParameterException {
    if (cryptoPerm == CryptoAllPermission.INSTANCE) {
        return;
    }
    // Convert the specified parameters into specs and then delegate.
    AlgorithmParameterSpec pSpec;
    try {
        pSpec = getAlgorithmParameterSpec(params);
    } catch (InvalidParameterSpecException ipse) {
        throw new InvalidAlgorithmParameterException("Failed to retrieve algorithm parameter specification");
    }
    checkCryptoPerm(checkSpi, key, pSpec);
}
Also used : InvalidParameterSpecException(java.security.spec.InvalidParameterSpecException) AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec)

Aggregations

AlgorithmParameterSpec (java.security.spec.AlgorithmParameterSpec)173 IvParameterSpec (javax.crypto.spec.IvParameterSpec)56 Cipher (javax.crypto.Cipher)55 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)49 InvalidKeyException (java.security.InvalidKeyException)42 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)37 SecretKey (javax.crypto.SecretKey)27 SecureRandom (java.security.SecureRandom)24 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)24 BadPaddingException (javax.crypto.BadPaddingException)21 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)20 BigInteger (java.math.BigInteger)19 RSAKeyGenParameterSpec (java.security.spec.RSAKeyGenParameterSpec)19 ShortBufferException (javax.crypto.ShortBufferException)19 Key (java.security.Key)18 SecretKeySpec (javax.crypto.spec.SecretKeySpec)18 AlgorithmParameters (java.security.AlgorithmParameters)16 KeyGenerator (javax.crypto.KeyGenerator)16 IOException (java.io.IOException)14 MyCipher (org.apache.harmony.crypto.tests.support.MyCipher)14