Search in sources :

Example 6 with NullCipher

use of javax.crypto.NullCipher 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 7 with NullCipher

use of javax.crypto.NullCipher 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 8 with NullCipher

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

the class CipherOutputStream1Test method testWrite1.

/**
     * write(int b) method testing. Tests that method writes correct values to
     * the underlying output stream.
     */
public void testWrite1() throws Exception {
    byte[] data = new byte[] { -127, -100, -50, -10, -1, 0, 1, 10, 50, 127 };
    TestOutputStream tos = new TestOutputStream();
    CipherOutputStream cos = new CipherOutputStream(tos, new NullCipher());
    for (int i = 0; i < data.length; i++) {
        cos.write(data[i]);
    }
    cos.flush();
    byte[] result = tos.toByteArray();
    if (!Arrays.equals(result, data)) {
        fail("CipherOutputStream wrote incorrect data.");
    }
}
Also used : CipherOutputStream(javax.crypto.CipherOutputStream) NullCipher(javax.crypto.NullCipher)

Example 9 with NullCipher

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

the class CipherOutputStream1Test method testWrite5.

/**
     * write(byte[] b, int off, int len)
     */
public void testWrite5() throws Exception {
    //Regression for HARMONY-758
    Cipher cf = Cipher.getInstance("DES/CBC/PKCS5Padding");
    NullCipher nc = new NullCipher();
    CipherOutputStream stream1 = new CipherOutputStream(new BufferedOutputStream((OutputStream) null), nc);
    CipherOutputStream stream2 = new CipherOutputStream(stream1, cf);
    CipherOutputStream stream3 = new CipherOutputStream(stream2, nc);
    stream3.write(new byte[] { 0 }, 0, 0);
//no exception expected
}
Also used : CipherOutputStream(javax.crypto.CipherOutputStream) NullCipher(javax.crypto.NullCipher) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) CipherOutputStream(javax.crypto.CipherOutputStream) Cipher(javax.crypto.Cipher) NullCipher(javax.crypto.NullCipher) BufferedOutputStream(java.io.BufferedOutputStream)

Example 10 with NullCipher

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

the class CipherOutputStream1Test method testWrite2.

/**
     * write(byte[] b) method testing. Tests that method writes correct values
     * to the underlying output stream.
     */
public void testWrite2() throws Exception {
    byte[] data = new byte[] { -127, -100, -50, -10, -1, 0, 1, 10, 50, 127 };
    TestOutputStream tos = new TestOutputStream();
    CipherOutputStream cos = new CipherOutputStream(tos, new NullCipher());
    cos.write(data);
    cos.flush();
    byte[] result = tos.toByteArray();
    if (!Arrays.equals(result, data)) {
        fail("CipherOutputStream wrote incorrect data.");
    }
    try {
        cos.write(null);
        fail("NullPointerException expected");
    } catch (NullPointerException e) {
    //expected
    }
}
Also used : CipherOutputStream(javax.crypto.CipherOutputStream) NullCipher(javax.crypto.NullCipher)

Aggregations

NullCipher (javax.crypto.NullCipher)25 CipherInputStream (javax.crypto.CipherInputStream)9 Cipher (javax.crypto.Cipher)6 SealedObject (javax.crypto.SealedObject)5 CipherOutputStream (javax.crypto.CipherOutputStream)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 Key (java.security.Key)2 KeyGenerator (javax.crypto.KeyGenerator)2 BufferedInputStream (java.io.BufferedInputStream)1 BufferedOutputStream (java.io.BufferedOutputStream)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 DataInputStream (java.io.DataInputStream)1 FilterInputStream (java.io.FilterInputStream)1 InputStream (java.io.InputStream)1 LineNumberInputStream (java.io.LineNumberInputStream)1 ObjectInputStream (java.io.ObjectInputStream)1 ObjectOutputStream (java.io.ObjectOutputStream)1 OutputStream (java.io.OutputStream)1 PushbackInputStream (java.io.PushbackInputStream)1 DigestInputStream (java.security.DigestInputStream)1