Search in sources :

Example 6 with SealedObject

use of javax.crypto.SealedObject in project robovm by robovm.

the class SealedObjectTest method testDeserialization.

// http://code.google.com/p/android/issues/detail?id=4834
public void testDeserialization() throws Exception {
    // (Boilerplate so we can create SealedObject instances.)
    KeyGenerator kg = KeyGenerator.getInstance("DES");
    Key key = kg.generateKey();
    Cipher cipher = Cipher.getInstance("DES");
    cipher.init(Cipher.ENCRYPT_MODE, key);
    // Incorrect use of readUnshared meant you couldn't have two SealedObjects
    // with the same algorithm or parameters algorithm...
    ArrayList<SealedObject> sealedObjects = new ArrayList<SealedObject>();
    for (int i = 0; i < 10; ++i) {
        sealedObjects.add(new SealedObject("hello", cipher));
    }
    String serializedForm = SerializationTester.serializeHex(sealedObjects);
    // ...so this would throw "java.io.InvalidObjectException: Unshared read of back reference".
    SerializationTester.deserializeHex(serializedForm);
}
Also used : ArrayList(java.util.ArrayList) SealedObject(javax.crypto.SealedObject) Cipher(javax.crypto.Cipher) NullCipher(javax.crypto.NullCipher) KeyGenerator(javax.crypto.KeyGenerator) Key(java.security.Key)

Example 7 with SealedObject

use of javax.crypto.SealedObject in project robovm by robovm.

the class SealedObjectTest method testGetObject2.

/**
     * getObject(Cipher c) method testing. Tests if the proper exception is
     * thrown in the case of incorrect input parameters and if the object sealed
     * with encryption algorithm and specified parameters can be retrieved by
     * specifying the initialized Cipher object.
     */
public void testGetObject2() throws Exception {
    try {
        new SealedObject("secret string", new NullCipher()).getObject((Cipher) null);
        fail("NullPointerException should be thrown in the case of " + "null cipher.");
    } catch (NullPointerException e) {
    }
    KeyGenerator kg = KeyGenerator.getInstance("DES");
    Key key = kg.generateKey();
    IvParameterSpec ips = new IvParameterSpec(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 });
    Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
    cipher.init(Cipher.ENCRYPT_MODE, key, ips);
    String secret = "secret string";
    SealedObject so = new SealedObject(secret, cipher);
    cipher.init(Cipher.DECRYPT_MODE, key, ips);
    assertEquals("The returned object does not equals to the " + "original object.", secret, so.getObject(cipher));
    try {
        so.getObject((Cipher) null);
        fail("NullPointerException expected");
    } catch (NullPointerException e) {
    //expected
    }
}
Also used : NullCipher(javax.crypto.NullCipher) SealedObject(javax.crypto.SealedObject) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) NullCipher(javax.crypto.NullCipher) KeyGenerator(javax.crypto.KeyGenerator) Key(java.security.Key)

Example 8 with SealedObject

use of javax.crypto.SealedObject in project robovm by robovm.

the class SealedObjectTest method testGetAlgorithm.

/**
     * getAlgorithm() method testing. Tests if the returned value equals to the
     * corresponding value of Cipher object.
     */
public void testGetAlgorithm() throws Exception {
    String secret = "secret string";
    String algorithm = "DES";
    KeyGenerator kg = KeyGenerator.getInstance(algorithm);
    Key key = kg.generateKey();
    Cipher cipher = Cipher.getInstance(algorithm);
    cipher.init(Cipher.ENCRYPT_MODE, key);
    SealedObject so = new SealedObject(secret, cipher);
    assertEquals("The algorithm name should be the same as used " + "in cipher.", algorithm, so.getAlgorithm());
}
Also used : SealedObject(javax.crypto.SealedObject) Cipher(javax.crypto.Cipher) NullCipher(javax.crypto.NullCipher) KeyGenerator(javax.crypto.KeyGenerator) Key(java.security.Key)

Example 9 with SealedObject

use of javax.crypto.SealedObject in project jdk8u_jdk by JetBrains.

the class PBESealedObject method runTest.

// Have a generic throws Exception as it can throw many different exceptions
public boolean runTest(Provider p, String algo, PrintStream out) throws Exception {
    byte[] salt = new byte[8];
    int ITERATION_COUNT = 1000;
    AlgorithmParameters pbeParams = null;
    String baseAlgo = new StringTokenizer(algo, "/").nextToken().toUpperCase();
    boolean isAES = baseAlgo.contains("AES");
    try {
        // Initialization
        Cipher ci = Cipher.getInstance(algo, p);
        new Random().nextBytes(salt);
        AlgorithmParameterSpec aps = new PBEParameterSpec(salt, ITERATION_COUNT);
        SecretKeyFactory skf = SecretKeyFactory.getInstance(baseAlgo, p);
        SecretKey key = skf.generateSecret(new PBEKeySpec("Secret Lover".toCharArray()));
        // Seal
        if (isAES) {
            ci.init(Cipher.ENCRYPT_MODE, key);
            pbeParams = ci.getParameters();
        } else {
            ci.init(Cipher.ENCRYPT_MODE, key, aps);
        }
        SealedObject so = new SealedObject(key, ci);
        // Unseal and compare
        if (isAES) {
            ci.init(Cipher.DECRYPT_MODE, key, pbeParams);
        } else {
            ci.init(Cipher.DECRYPT_MODE, key, aps);
        }
        SecretKey unsealedKey;
        unsealedKey = (SecretKey) so.getObject(ci);
        if (!Arrays.equals(unsealedKey.getEncoded(), key.getEncoded())) {
            return false;
        }
        unsealedKey = (SecretKey) so.getObject(key);
        if (!Arrays.equals(unsealedKey.getEncoded(), key.getEncoded())) {
            return false;
        }
        unsealedKey = (SecretKey) so.getObject(key, "SunJCE");
        return Arrays.equals(unsealedKey.getEncoded(), key.getEncoded());
    } catch (InvalidKeyException ex) {
        if (baseAlgo.endsWith("TRIPLEDES") || baseAlgo.endsWith("AES_256")) {
            out.println("Expected exception , keyStrength > 128 within" + algo);
            return true;
        }
        throw ex;
    }
}
Also used : PBEKeySpec(javax.crypto.spec.PBEKeySpec) SealedObject(javax.crypto.SealedObject) InvalidKeyException(java.security.InvalidKeyException) StringTokenizer(java.util.StringTokenizer) SecretKey(javax.crypto.SecretKey) Random(java.util.Random) Cipher(javax.crypto.Cipher) AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec) SecretKeyFactory(javax.crypto.SecretKeyFactory) PBEParameterSpec(javax.crypto.spec.PBEParameterSpec) AlgorithmParameters(java.security.AlgorithmParameters)

Example 10 with SealedObject

use of javax.crypto.SealedObject 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)

Aggregations

SealedObject (javax.crypto.SealedObject)10 Cipher (javax.crypto.Cipher)9 NullCipher (javax.crypto.NullCipher)8 KeyGenerator (javax.crypto.KeyGenerator)6 Key (java.security.Key)5 AlgorithmParameters (java.security.AlgorithmParameters)2 InvalidKeyException (java.security.InvalidKeyException)2 SecretKey (javax.crypto.SecretKey)2 IvParameterSpec (javax.crypto.spec.IvParameterSpec)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 ObjectInputStream (java.io.ObjectInputStream)1 ObjectOutputStream (java.io.ObjectOutputStream)1 NoSuchProviderException (java.security.NoSuchProviderException)1 AlgorithmParameterSpec (java.security.spec.AlgorithmParameterSpec)1 ArrayList (java.util.ArrayList)1 Random (java.util.Random)1 StringTokenizer (java.util.StringTokenizer)1 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)1 SecretKeyFactory (javax.crypto.SecretKeyFactory)1