Search in sources :

Example 1 with SealedObject

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

the class TestSealedObjectNull method main.

public static void main(String[] args) throws IOException, IllegalBlockSizeException, ClassNotFoundException, BadPaddingException {
    Cipher nullCipher = new NullCipher();
    // Seal
    SealedObject so = new SealedObject(SEAL_STR, nullCipher);
    // Unseal and compare
    if (!(SEAL_STR.equals(so.getObject(nullCipher)))) {
        throw new RuntimeException("Unseal and compare failed.");
    }
    System.out.println("Test passed.");
}
Also used : NullCipher(javax.crypto.NullCipher) SealedObject(javax.crypto.SealedObject) NullCipher(javax.crypto.NullCipher) Cipher(javax.crypto.Cipher)

Example 2 with SealedObject

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

the class SealedObjectTest method testSealedObject1.

/**
     * SealedObject(Serializable object, Cipher c) method testing. Tests if the
     * NullPointerException is thrown in the case of null cipher.
     */
public void testSealedObject1() throws Exception {
    String secret = "secret string";
    try {
        new SealedObject(secret, 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);
    SealedObject so = new SealedObject(secret, cipher);
    cipher = Cipher.getInstance("DES/CBC/NoPadding");
    cipher.init(Cipher.ENCRYPT_MODE, key, ips);
    try {
        new SealedObject(secret, cipher);
        fail("IllegalBlockSizeException expected");
    } catch (IllegalBlockSizeException e) {
    //expected
    }
}
Also used : IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) 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 3 with SealedObject

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

the class SealedObjectTest method testSealedObject2.

/**
     * SealedObject(SealedObject so) method testing. Tests if the
     * NullPointerException is thrown in the case of null SealedObject.
     */
public void testSealedObject2() throws Exception {
    try {
        new SealedObject(null) {
        };
        fail("NullPointerException should be thrown in the case " + "of null SealedObject.");
    } catch (NullPointerException e) {
    }
    String secret = "secret string";
    Cipher cipher = new NullCipher();
    SealedObject so1 = new SealedObject(secret, cipher);
    SealedObject so2 = new SealedObject(so1) {
    };
    assertEquals("The secret content of the object should equals " + "to the secret content of initial object.", secret, so2.getObject(cipher));
    assertEquals("The algorithm which was used to seal the object " + "should be the same as the algorithm used to seal the " + "initial object", so1.getAlgorithm(), so2.getAlgorithm());
}
Also used : NullCipher(javax.crypto.NullCipher) SealedObject(javax.crypto.SealedObject) Cipher(javax.crypto.Cipher) NullCipher(javax.crypto.NullCipher)

Example 4 with SealedObject

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

the class SealedObjectTest method testGetObject3.

/**
     * getObject(Key key, String provider) method testing. Tests if the proper
     * exception is thrown in the case of incorrect input parameters and if the
     * object sealed with encryption algorithm can be retrieved by specifying
     * the cryptographic key and provider name.
     */
public void testGetObject3() throws Exception {
    try {
        new SealedObject("secret string", new NullCipher()).getObject(new SecretKeySpec(new byte[] { 0, 0, 0 }, "algorithm"), null);
        fail("IllegalArgumentException should be thrown in the case of " + "null provider.");
    } catch (IllegalArgumentException e) {
    }
    try {
        new SealedObject("secret string", new NullCipher()).getObject(new SecretKeySpec(new byte[] { 0, 0, 0 }, "algorithm"), "");
        fail("IllegalArgumentException should be thrown in the case of " + "empty provider.");
    } catch (IllegalArgumentException e) {
    }
    KeyGenerator kg = KeyGenerator.getInstance("DES");
    Key key = kg.generateKey();
    Cipher cipher = Cipher.getInstance("DES");
    String provider = cipher.getProvider().getName();
    cipher.init(Cipher.ENCRYPT_MODE, key);
    String secret = "secret string";
    SealedObject so = new SealedObject(secret, cipher);
    cipher.init(Cipher.DECRYPT_MODE, key);
    assertEquals("The returned object does not equals to the " + "original object.", secret, so.getObject(key, provider));
    kg = KeyGenerator.getInstance("DESede");
    key = kg.generateKey();
    try {
        so.getObject(key, provider);
        fail("InvalidKeyException expected");
    } catch (InvalidKeyException e) {
    //expected
    }
    try {
        so.getObject(key, "Wrong provider name");
        fail("NoSuchProviderException expected");
    } catch (NoSuchProviderException e) {
    //expected
    }
}
Also used : NullCipher(javax.crypto.NullCipher) SecretKeySpec(javax.crypto.spec.SecretKeySpec) SealedObject(javax.crypto.SealedObject) Cipher(javax.crypto.Cipher) NullCipher(javax.crypto.NullCipher) InvalidKeyException(java.security.InvalidKeyException) NoSuchProviderException(java.security.NoSuchProviderException) KeyGenerator(javax.crypto.KeyGenerator) Key(java.security.Key)

Example 5 with SealedObject

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

the class BasicCryptor method unsealObject.

public synchronized Object unsealObject(Object object) throws CryptoException {
    if (useSealedObject) {
        if (!(object instanceof SealedObject)) {
            return object;
        }
        SealedObject so = (SealedObject) object;
        ClassLoader cl = Thread.currentThread().getContextClassLoader();
        try {
            if (cl != classLoader) {
                Thread.currentThread().setContextClassLoader(BasicCryptor.class.getClassLoader());
            }
            return so.getObject(decryptCipher);
        } catch (Exception e) {
            try {
                initDecryptCipher();
            } catch (CryptoException err) {
            // shouldn't happen
            }
            throw new CryptoException(CorePlugin.Event.TEIID10006, CorePlugin.Util.gs(CorePlugin.Event.TEIID10006, e.getClass().getName(), e.getMessage()));
        } finally {
            Thread.currentThread().setContextClassLoader(cl);
        }
    }
    if (!(object instanceof byte[])) {
        return object;
    }
    byte[] bytes = (byte[]) object;
    bytes = decrypt(bytes);
    try {
        ObjectInputStream ois = new ObjectInputStreamWithClassloader(new ByteArrayInputStream(bytes), classLoader);
        return ois.readObject();
    } catch (Exception e) {
        throw new CryptoException(CorePlugin.Event.TEIID10006, CorePlugin.Util.gs(CorePlugin.Event.TEIID10006, e.getClass().getName(), e.getMessage()));
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) SealedObject(javax.crypto.SealedObject) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) ObjectInputStream(java.io.ObjectInputStream) ObjectInputStreamWithClassloader(org.teiid.core.util.ObjectInputStreamWithClassloader)

Aggregations

SealedObject (javax.crypto.SealedObject)15 Cipher (javax.crypto.Cipher)11 NullCipher (javax.crypto.NullCipher)8 KeyGenerator (javax.crypto.KeyGenerator)7 Key (java.security.Key)6 InvalidKeyException (java.security.InvalidKeyException)5 SecretKeySpec (javax.crypto.spec.SecretKeySpec)4 ObjectInputStream (java.io.ObjectInputStream)3 ObjectOutputStream (java.io.ObjectOutputStream)3 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)3 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)3 SecretKey (javax.crypto.SecretKey)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 IOException (java.io.IOException)2 AlgorithmParameters (java.security.AlgorithmParameters)2 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)2 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)2 IvParameterSpec (javax.crypto.spec.IvParameterSpec)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 FileInputStream (java.io.FileInputStream)1