Search in sources :

Example 91 with AlgorithmParameters

use of java.security.AlgorithmParameters in project jdk8u_jdk by JetBrains.

the class SealedObjectTest method doTest.

/*
     * Run the test:
     * - init a cipher with AES/GCM/NoPadding transformation
     * - seal an object
     * - check if we can't seal it again with the same key/IV
     * - unseal the object using different methods of SealedObject class
     * - check if the original and sealed objects are equal
     */
static void doTest() throws Exception {
    // init a secret Key
    KeyGenerator kg = KeyGenerator.getInstance(AES, PROVIDER);
    kg.init(KEY_LENGTH);
    SecretKey key = kg.generateKey();
    // initialization
    Cipher cipher = Cipher.getInstance(TRANSFORMATION, PROVIDER);
    cipher.init(Cipher.ENCRYPT_MODE, key);
    AlgorithmParameters params = cipher.getParameters();
    // seal an object
    SealedObject so = new SealedObject(key, cipher);
    try {
        // check if we can't seal it again with the same key/IV
        so = new SealedObject(key, cipher);
        throw new RuntimeException("FAILED: expected IllegalStateException hasn't " + "been thrown");
    } catch (IllegalStateException ise) {
        System.out.println("Expected exception when seal it again with" + " the same key/IV: " + ise);
    }
    // unseal the object using getObject(Cipher) and compare
    cipher.init(Cipher.DECRYPT_MODE, key, params);
    SecretKey unsealedKey = (SecretKey) so.getObject(cipher);
    assertKeysSame(unsealedKey, key, "SealedObject.getObject(Cipher)");
    // unseal the object using getObject(Key) and compare
    unsealedKey = (SecretKey) so.getObject(key);
    assertKeysSame(unsealedKey, key, "SealedObject.getObject(Key)");
    // unseal the object using getObject(Key, String) and compare
    unsealedKey = (SecretKey) so.getObject(key, PROVIDER);
    assertKeysSame(unsealedKey, key, "SealedObject.getObject(Key, String)");
}
Also used : SecretKey(javax.crypto.SecretKey) SealedObject(javax.crypto.SealedObject) Cipher(javax.crypto.Cipher) KeyGenerator(javax.crypto.KeyGenerator) AlgorithmParameters(java.security.AlgorithmParameters)

Example 92 with AlgorithmParameters

use of java.security.AlgorithmParameters in project jdk8u_jdk by JetBrains.

the class TestCipherKeyWrapperTest method wrapTest.

private void wrapTest(String transformation, String wrapAlgo, Key initKey, Key wrapKey, int keyType, boolean isPBE) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, InvalidAlgorithmParameterException {
    String algo = transformation.split("/")[0];
    boolean isAESBlowfish = algo.indexOf("AES") != -1 || algo.indexOf("Blowfish") != -1;
    AlgorithmParameters aps = null;
    AlgorithmParameterSpec pbeParams = null;
    if (isPBE) {
        byte[] salt = new byte[8];
        int iterCnt = 1000;
        new Random().nextBytes(salt);
        pbeParams = new PBEParameterSpec(salt, iterCnt);
    }
    // Wrap & UnWrap operation
    Cipher wrapCI = Cipher.getInstance(wrapAlgo);
    if (isPBE && !isAESBlowfish) {
        wrapCI.init(Cipher.WRAP_MODE, initKey, pbeParams);
    } else if (isAESBlowfish) {
        wrapCI.init(Cipher.WRAP_MODE, initKey);
        aps = wrapCI.getParameters();
    } else {
        wrapCI.init(Cipher.WRAP_MODE, initKey);
    }
    out.println("keysize : " + wrapKey.getEncoded().length);
    byte[] keyWrapper = wrapCI.wrap(wrapKey);
    if (isPBE && !isAESBlowfish) {
        wrapCI.init(Cipher.UNWRAP_MODE, initKey, pbeParams);
    } else if (isAESBlowfish) {
        wrapCI.init(Cipher.UNWRAP_MODE, initKey, aps);
    } else {
        wrapCI.init(Cipher.UNWRAP_MODE, initKey);
    }
    Key unwrappedKey = wrapCI.unwrap(keyWrapper, algo, keyType);
    // Comparison
    if (!Arrays.equals(wrapKey.getEncoded(), unwrappedKey.getEncoded())) {
        throw new RuntimeException("Comparation failed testing " + transformation + ":" + wrapAlgo + ":" + keyType);
    }
}
Also used : Random(java.util.Random) String(java.lang.String) Cipher(javax.crypto.Cipher) AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec) PBEParameterSpec(javax.crypto.spec.PBEParameterSpec) Key(java.security.Key) SecretKey(javax.crypto.SecretKey) AlgorithmParameters(java.security.AlgorithmParameters)

Example 93 with AlgorithmParameters

use of java.security.AlgorithmParameters in project jdk8u_jdk by JetBrains.

the class PBEParametersTest method main.

public static void main(String[] args) throws Exception {
    PBEKeySpec ks = new PBEKeySpec(PASSWORD);
    for (int i = 0; i < PBE_ALGOS.length; i++) {
        String algo = PBE_ALGOS[i];
        SecretKeyFactory skf = SecretKeyFactory.getInstance(algo);
        SecretKey key = skf.generateSecret(ks);
        Cipher c = Cipher.getInstance(algo, "SunJCE");
        c.init(Cipher.ENCRYPT_MODE, key);
        // force the generation of parameters
        c.doFinal(new byte[10]);
        AlgorithmParameters params = c.getParameters();
        if (!params.getAlgorithm().equalsIgnoreCase(algo)) {
            throw new Exception("expect: " + algo + ", but got: " + params.getAlgorithm());
        }
        System.out.println(algo + "...done...");
    }
    System.out.println("Test Passed");
}
Also used : AlgorithmParameters(java.security.AlgorithmParameters)

Example 94 with AlgorithmParameters

use of java.security.AlgorithmParameters in project jdk8u_jdk by JetBrains.

the class KeyWrapper method doTest.

private static void doTest(String provider, String algo) throws Exception {
    SecretKey key;
    SecretKey keyToWrap;
    // init a secret Key
    KeyGenerator kg = KeyGenerator.getInstance(AES, PROVIDER);
    kg.init(KEY_LENGTH);
    key = kg.generateKey();
    keyToWrap = kg.generateKey();
    // initialization
    Cipher cipher = Cipher.getInstance(algo, provider);
    cipher.init(Cipher.WRAP_MODE, key);
    AlgorithmParameters params = cipher.getParameters();
    // wrap the key
    byte[] keyWrapper = cipher.wrap(keyToWrap);
    try {
        // check if we can't wrap it again with the same key/IV
        keyWrapper = cipher.wrap(keyToWrap);
        throw new RuntimeException("FAILED: expected IllegalStateException hasn't " + "been thrown ");
    } catch (IllegalStateException ise) {
        System.out.println(ise.getMessage());
        System.out.println("Expected exception");
    }
    // unwrap the key
    cipher.init(Cipher.UNWRAP_MODE, key, params);
    cipher.unwrap(keyWrapper, algo, Cipher.SECRET_KEY);
    // check if we can unwrap second time
    Key unwrapKey = cipher.unwrap(keyWrapper, algo, Cipher.SECRET_KEY);
    if (!Arrays.equals(keyToWrap.getEncoded(), unwrapKey.getEncoded())) {
        throw new RuntimeException("FAILED: original and unwrapped keys are not equal");
    }
}
Also used : SecretKey(javax.crypto.SecretKey) Cipher(javax.crypto.Cipher) KeyGenerator(javax.crypto.KeyGenerator) Key(java.security.Key) SecretKey(javax.crypto.SecretKey) AlgorithmParameters(java.security.AlgorithmParameters)

Example 95 with AlgorithmParameters

use of java.security.AlgorithmParameters in project jdk8u_jdk by JetBrains.

the class TextPKCS5PaddingTest method main.

public static void main(String[] args) throws Exception {
    Provider provider = Security.getProvider("SunJCE");
    if (provider == null) {
        throw new RuntimeException("SunJCE provider not exist");
    }
    // generate no-padding cipher with secret key
    Cipher c = Cipher.getInstance("DES/CBC/NoPadding", provider);
    KeyGenerator kgen = KeyGenerator.getInstance("DES", provider);
    SecretKey skey = kgen.generateKey();
    // this is the improperly padded plaintext
    c.init(Cipher.ENCRYPT_MODE, skey);
    // encrypt plaintext
    byte[] cipher = c.doFinal(PLAIN_TEXT);
    AlgorithmParameters params = c.getParameters();
    // generate cipher that enforces PKCS5 padding
    c = Cipher.getInstance("DES/CBC/PKCS5Padding", provider);
    c.init(Cipher.DECRYPT_MODE, skey, params);
    try {
        c.doFinal(cipher);
        throw new RuntimeException("ERROR: Expected BadPaddingException not thrown");
    } catch (BadPaddingException expected) {
        out.println("Expected BadPaddingException thrown");
    }
}
Also used : SecretKey(javax.crypto.SecretKey) Cipher(javax.crypto.Cipher) BadPaddingException(javax.crypto.BadPaddingException) KeyGenerator(javax.crypto.KeyGenerator) Provider(java.security.Provider) AlgorithmParameters(java.security.AlgorithmParameters)

Aggregations

AlgorithmParameters (java.security.AlgorithmParameters)107 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)39 IOException (java.io.IOException)31 InvalidParameterSpecException (java.security.spec.InvalidParameterSpecException)22 Cipher (javax.crypto.Cipher)22 SecretKey (javax.crypto.SecretKey)18 AlgorithmParameterSpec (java.security.spec.AlgorithmParameterSpec)13 NoSuchProviderException (java.security.NoSuchProviderException)12 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)11 Key (java.security.Key)11 SecureRandom (java.security.SecureRandom)10 EncryptedPrivateKeyInfo (javax.crypto.EncryptedPrivateKeyInfo)10 InvalidKeyException (java.security.InvalidKeyException)8 PBEParameterSpec (javax.crypto.spec.PBEParameterSpec)8 UnrecoverableKeyException (java.security.UnrecoverableKeyException)7 KeyPair (java.security.KeyPair)6 KeyPairGenerator (java.security.KeyPairGenerator)6 AlgorithmId (sun.security.x509.AlgorithmId)6 Nullable (android.annotation.Nullable)5 Asn1Integer (com.android.hotspot2.asn1.Asn1Integer)5