javax.crypto.NullCipher Java Examples

The following examples show how to use javax.crypto.NullCipher. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CipherNCFuncTest.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws ShortBufferException,
        IllegalBlockSizeException, BadPaddingException {
    byte[] plainText = new byte[801];
    // Initialization
    RandomFactory.getRandom().nextBytes(plainText);
    Cipher ci = new NullCipher();
    // Encryption
    byte[] cipherText = new byte[ci.getOutputSize(plainText.length)];
    int offset = ci.update(plainText, 0, plainText.length, cipherText, 0);
    ci.doFinal(cipherText, offset);
    // Decryption
    byte[] recoveredText = new byte[ci.getOutputSize(cipherText.length)];
    int len = ci.doFinal(cipherText, 0, cipherText.length, recoveredText);
    // Comparison
    if (len != plainText.length ||
            !TestUtilities.equalsBlock(plainText, cipherText, len) ||
            !TestUtilities.equalsBlock(plainText, recoveredText, len)) {
        throw new RuntimeException(
            "Test failed because plainText not equal to cipherText and revoveredText");
    }
}
 
Example #2
Source File: CipherNCFuncTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws ShortBufferException,
        IllegalBlockSizeException, BadPaddingException {
    byte[] plainText = new byte[801];
    // Initialization
    RandomFactory.getRandom().nextBytes(plainText);
    Cipher ci = new NullCipher();
    // Encryption
    byte[] cipherText = new byte[ci.getOutputSize(plainText.length)];
    int offset = ci.update(plainText, 0, plainText.length, cipherText, 0);
    ci.doFinal(cipherText, offset);
    // Decryption
    byte[] recoveredText = new byte[ci.getOutputSize(cipherText.length)];
    int len = ci.doFinal(cipherText, 0, cipherText.length, recoveredText);
    // Comparison
    if (len != plainText.length ||
            !TestUtilities.equalsBlock(plainText, cipherText, len) ||
            !TestUtilities.equalsBlock(plainText, recoveredText, len)) {
        throw new RuntimeException(
            "Test failed because plainText not equal to cipherText and revoveredText");
    }
}
 
Example #3
Source File: CipherNCFuncTest.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws ShortBufferException,
        IllegalBlockSizeException, BadPaddingException {
    byte[] plainText = new byte[801];
    // Initialization
    RandomFactory.getRandom().nextBytes(plainText);
    Cipher ci = new NullCipher();
    // Encryption
    byte[] cipherText = new byte[ci.getOutputSize(plainText.length)];
    int offset = ci.update(plainText, 0, plainText.length, cipherText, 0);
    ci.doFinal(cipherText, offset);
    // Decryption
    byte[] recoveredText = new byte[ci.getOutputSize(cipherText.length)];
    int len = ci.doFinal(cipherText, 0, cipherText.length, recoveredText);
    // Comparison
    if (len != plainText.length ||
            !TestUtilities.equalsBlock(plainText, cipherText, len) ||
            !TestUtilities.equalsBlock(plainText, recoveredText, len)) {
        throw new RuntimeException(
            "Test failed because plainText not equal to cipherText and revoveredText");
    }
}
 
Example #4
Source File: CipherNCFuncTest.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws ShortBufferException,
        IllegalBlockSizeException, BadPaddingException {
    byte[] plainText = new byte[801];
    // Initialization
    RandomFactory.getRandom().nextBytes(plainText);
    Cipher ci = new NullCipher();
    // Encryption
    byte[] cipherText = new byte[ci.getOutputSize(plainText.length)];
    int offset = ci.update(plainText, 0, plainText.length, cipherText, 0);
    ci.doFinal(cipherText, offset);
    // Decryption
    byte[] recoveredText = new byte[ci.getOutputSize(cipherText.length)];
    int len = ci.doFinal(cipherText, 0, cipherText.length, recoveredText);
    // Comparison
    if (len != plainText.length ||
            !TestUtilities.equalsBlock(plainText, cipherText, len) ||
            !TestUtilities.equalsBlock(plainText, recoveredText, len)) {
        throw new RuntimeException(
            "Test failed because plainText not equal to cipherText and revoveredText");
    }
}
 
Example #5
Source File: CipherNCFuncTest.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws ShortBufferException,
        IllegalBlockSizeException, BadPaddingException {
    byte[] plainText = new byte[801];
    // Initialization
    RandomFactory.getRandom().nextBytes(plainText);
    Cipher ci = new NullCipher();
    // Encryption
    byte[] cipherText = new byte[ci.getOutputSize(plainText.length)];
    int offset = ci.update(plainText, 0, plainText.length, cipherText, 0);
    ci.doFinal(cipherText, offset);
    // Decryption
    byte[] recoveredText = new byte[ci.getOutputSize(cipherText.length)];
    int len = ci.doFinal(cipherText, 0, cipherText.length, recoveredText);
    // Comparison
    if (len != plainText.length ||
            !TestUtilities.equalsBlock(plainText, cipherText, len) ||
            !TestUtilities.equalsBlock(plainText, recoveredText, len)) {
        throw new RuntimeException(
            "Test failed because plainText not equal to cipherText and revoveredText");
    }
}
 
Example #6
Source File: CipherNCFuncTest.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws ShortBufferException,
        IllegalBlockSizeException, BadPaddingException {
    byte[] plainText = new byte[801];
    // Initialization
    RandomFactory.getRandom().nextBytes(plainText);
    Cipher ci = new NullCipher();
    // Encryption
    byte[] cipherText = new byte[ci.getOutputSize(plainText.length)];
    int offset = ci.update(plainText, 0, plainText.length, cipherText, 0);
    ci.doFinal(cipherText, offset);
    // Decryption
    byte[] recoveredText = new byte[ci.getOutputSize(cipherText.length)];
    int len = ci.doFinal(cipherText, 0, cipherText.length, recoveredText);
    // Comparison
    if (len != plainText.length ||
            !TestUtilities.equalsBlock(plainText, cipherText, len) ||
            !TestUtilities.equalsBlock(plainText, recoveredText, len)) {
        throw new RuntimeException(
            "Test failed because plainText not equal to cipherText and revoveredText");
    }
}
 
Example #7
Source File: CipherNCFuncTest.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws ShortBufferException,
        IllegalBlockSizeException, BadPaddingException {
    byte[] plainText = new byte[801];
    // Initialization
    RandomFactory.getRandom().nextBytes(plainText);
    Cipher ci = new NullCipher();
    // Encryption
    byte[] cipherText = new byte[ci.getOutputSize(plainText.length)];
    int offset = ci.update(plainText, 0, plainText.length, cipherText, 0);
    ci.doFinal(cipherText, offset);
    // Decryption
    byte[] recoveredText = new byte[ci.getOutputSize(cipherText.length)];
    int len = ci.doFinal(cipherText, 0, cipherText.length, recoveredText);
    // Comparison
    if (len != plainText.length ||
            !TestUtilities.equalsBlock(plainText, cipherText, len) ||
            !TestUtilities.equalsBlock(plainText, recoveredText, len)) {
        throw new RuntimeException(
            "Test failed because plainText not equal to cipherText and revoveredText");
    }
}
 
Example #8
Source File: CipherNCFuncTest.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws ShortBufferException,
        IllegalBlockSizeException, BadPaddingException {
    byte[] plainText = new byte[801];
    // Initialization
    RandomFactory.getRandom().nextBytes(plainText);
    Cipher ci = new NullCipher();
    // Encryption
    byte[] cipherText = new byte[ci.getOutputSize(plainText.length)];
    int offset = ci.update(plainText, 0, plainText.length, cipherText, 0);
    ci.doFinal(cipherText, offset);
    // Decryption
    byte[] recoveredText = new byte[ci.getOutputSize(cipherText.length)];
    int len = ci.doFinal(cipherText, 0, cipherText.length, recoveredText);
    // Comparison
    if (len != plainText.length ||
            !TestUtilities.equalsBlock(plainText, cipherText, len) ||
            !TestUtilities.equalsBlock(plainText, recoveredText, len)) {
        throw new RuntimeException(
            "Test failed because plainText not equal to cipherText and revoveredText");
    }
}
 
Example #9
Source File: NullCipherTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
public void testUpdatebyteArrayintint2() {
    //Regression for HARMONY-758
    try {
        new NullCipher().update(new byte[1], 1, Integer.MAX_VALUE);
        fail("Expected IllegalArgumentException was not thrown");
    } catch (IllegalArgumentException e) {
    }
}
 
Example #10
Source File: TestSealedObjectNull.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
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.");
}
 
Example #11
Source File: NullCipherTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
public void testDoFinalbyteArrayintintbyteArray2() throws Exception {
    //Regression for HARMONY-758
    try {
        new NullCipher().update(new byte[1], 1, Integer.MAX_VALUE,
                new byte[1]);
        fail("Expected IllegalArgumentException was not thrown");
    } catch (IllegalArgumentException e) {
    }
}
 
Example #12
Source File: NullCipherTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
public void testDoFinalbyteArrayintintbyteArray3() throws Exception {
    //Regression for HARMONY-758
    try {
        new NullCipher().update(new byte[1], 0, 1, new byte[0]);
        fail("Expected IndexOutOfBoundsException was not thrown");
    } catch (IndexOutOfBoundsException e) {
    }
}
 
Example #13
Source File: TestSealedObjectNull.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
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.");
}
 
Example #14
Source File: NullCipherTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
public void testDoFinalbyteArrayintintbyteArrayint2() throws Exception {
    //Regression for HARMONY-758
    try {
        new NullCipher().update(new byte[1], 1, Integer.MAX_VALUE,
                new byte[1], 0);
        fail("Expected IllegalArgumentException was not thrown");
    } catch (IllegalArgumentException e) {
    }
}
 
Example #15
Source File: NullCipherTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
public void testDoFinalbyteArrayintintbyteArrayint3() throws Exception {
    //Regression for HARMONY-758
    try {
        new NullCipher().update(new byte[1], 0, 1,
                new byte[0], 0);
        fail("Expected IndexOutOfBoundsException was not thrown");
    } catch (IndexOutOfBoundsException e) {
    }
}
 
Example #16
Source File: JDKUtil.java    From marshalsec with MIT License 5 votes vote down vote up
@SuppressWarnings ( "resource" )
public static Object makeIteratorTriggerNative ( UtilFactory uf, Object it ) throws Exception, ClassNotFoundException, NoSuchMethodException,
        InstantiationException, IllegalAccessException, InvocationTargetException {
    Cipher m = Reflections.createWithoutConstructor(NullCipher.class);
    Reflections.setFieldValue(m, "serviceIterator", it);
    Reflections.setFieldValue(m, "lock", new Object());

    InputStream cos = new CipherInputStream(null, m);

    Class<?> niCl = Class.forName("java.lang.ProcessBuilder$NullInputStream"); //$NON-NLS-1$
    Constructor<?> niCons = niCl.getDeclaredConstructor();
    niCons.setAccessible(true);

    Reflections.setFieldValue(cos, "input", niCons.newInstance());
    Reflections.setFieldValue(cos, "ibuffer", new byte[0]);

    Object b64Data = Class.forName("com.sun.xml.internal.bind.v2.runtime.unmarshaller.Base64Data").newInstance();
    DataSource ds = (DataSource) Reflections
            .createWithoutConstructor(Class.forName("com.sun.xml.internal.ws.encoding.xml.XMLMessage$XmlDataSource")); //$NON-NLS-1$
    Reflections.setFieldValue(ds, "is", cos);
    Reflections.setFieldValue(b64Data, "dataHandler", new DataHandler(ds));
    Reflections.setFieldValue(b64Data, "data", null);

    Object nativeString = Reflections.createWithoutConstructor(Class.forName("jdk.nashorn.internal.objects.NativeString"));
    Reflections.setFieldValue(nativeString, "value", b64Data);
    return uf.makeHashCodeTrigger(nativeString);
}
 
Example #17
Source File: TestSealedObjectNull.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
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.");
}
 
Example #18
Source File: TestSealedObjectNull.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
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.");
}
 
Example #19
Source File: TestSealedObjectNull.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
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.");
}
 
Example #20
Source File: TestSealedObjectNull.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
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.");
}
 
Example #21
Source File: TestSealedObjectNull.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
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.");
}
 
Example #22
Source File: TestSealedObjectNull.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
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.");
}
 
Example #23
Source File: JDKUtil.java    From learnjavabug with MIT License 5 votes vote down vote up
@SuppressWarnings ( "resource" )
public static Object makeIteratorTriggerNative ( UtilFactory uf, Object it ) throws Exception, ClassNotFoundException, NoSuchMethodException,
        InstantiationException, IllegalAccessException, InvocationTargetException {
    Cipher m = Reflections.createWithoutConstructor(NullCipher.class);
    Reflections.setFieldValue(m, "serviceIterator", it);
    Reflections.setFieldValue(m, "lock", new Object());

    InputStream cos = new CipherInputStream(null, m);

    Class<?> niCl = Class.forName("java.lang.ProcessBuilder$NullInputStream"); //$NON-NLS-1$
    Constructor<?> niCons = niCl.getDeclaredConstructor();
    niCons.setAccessible(true);

    Reflections.setFieldValue(cos, "input", niCons.newInstance());
    Reflections.setFieldValue(cos, "ibuffer", new byte[0]);

    Object b64Data = Class.forName("com.sun.xml.internal.bind.v2.runtime.unmarshaller.Base64Data").newInstance();
    DataSource ds = (DataSource) Reflections
            .createWithoutConstructor(Class.forName("com.sun.xml.internal.ws.encoding.xml.XMLMessage$XmlDataSource")); //$NON-NLS-1$
    Reflections.setFieldValue(ds, "is", cos);
    Reflections.setFieldValue(b64Data, "dataHandler", new DataHandler(ds));
    Reflections.setFieldValue(b64Data, "data", null);

    Object nativeString = Reflections.createWithoutConstructor(Class.forName("jdk.nashorn.internal.objects.NativeString"));
    Reflections.setFieldValue(nativeString, "value", b64Data);
    return uf.makeHashCodeTrigger(nativeString);
}
 
Example #24
Source File: TestNPE.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
    NullCipher nc = new NullCipher();
    // testing init(...)
    nc.init(Cipher.ENCRYPT_MODE, (Certificate) null);
    nc.init(Cipher.ENCRYPT_MODE, (Certificate) null, (SecureRandom) null);
    nc.init(Cipher.ENCRYPT_MODE, (Key) null);
    nc.init(Cipher.ENCRYPT_MODE, (Key) null, (AlgorithmParameters) null);
    nc.init(Cipher.ENCRYPT_MODE, (Key) null, (AlgorithmParameterSpec) null);
    nc.init(Cipher.ENCRYPT_MODE, (Key) null, (AlgorithmParameterSpec) null,
        (SecureRandom) null);
    nc.init(Cipher.ENCRYPT_MODE, (Key) null, (AlgorithmParameters) null,
        (SecureRandom) null);
    nc.init(Cipher.ENCRYPT_MODE, (Key) null, (SecureRandom) null);
    // testing getBlockSize()
    if (nc.getBlockSize() != 1) {
        throw new Exception("Error with getBlockSize()");
    }
    // testing update(...)
    byte[] out = nc.update(BYTES);
    if (!Arrays.equals(out, BYTES)) {
        throw new Exception("Error with update(byte[])");
    }
    out = nc.update(BYTES, 0, BYTES.length);
    if (!Arrays.equals(out, BYTES)) {
        throw new Exception("Error with update(byte[], int, int)");
    }
    if (nc.update(BYTES, 0, BYTES.length, out) != BYTES.length) {
        throw new Exception("Error with update(byte[], int, int, byte[])");
    }
    if (nc.update(BYTES, 0, BYTES.length, out, 0) != BYTES.length) {
        throw new Exception(
            "Error with update(byte[], int, int, byte[], int)");
    }
    // testing doFinal(...)
    if (nc.doFinal() != null) {
        throw new Exception("Error with doFinal()");
    }
    if (nc.doFinal(out, 0) != 0) {
         throw new Exception("Error with doFinal(byte[], 0)");
    }
    out = nc.doFinal(BYTES);
    if (!Arrays.equals(out, BYTES)) {
        throw new Exception("Error with doFinal(byte[])");
    }
    out = nc.doFinal(BYTES, 0, BYTES.length);
    if (!Arrays.equals(out, BYTES)) {
        throw new Exception("Error with doFinal(byte[], int, int)");
    }
    if (nc.doFinal(BYTES, 0, BYTES.length, out) != BYTES.length) {
        throw new Exception(
            "Error with doFinal(byte[], int, int, byte[])");
    }
    if (nc.doFinal(BYTES, 0, BYTES.length, out, 0) != BYTES.length) {
        throw new Exception(
            "Error with doFinal(byte[], int, int, byte[], int)");
    }
    // testing getExemptionMechanism()
    if (nc.getExemptionMechanism() != null) {
        throw new Exception("Error with getExemptionMechanism()");
    }
    // testing getOutputSize(int)
    if (nc.getOutputSize(5) != 5) {
        throw new Exception("Error with getOutputSize()");
    }
    // testing getIV(), getParameters(), getAlgorithm(), etc.
    if (nc.getIV() == null) { // should've been null;
                              // left it as is for backward-compatibility
        throw new Exception("Error with getIV()");
    }
    if (nc.getParameters() != null) {
        throw new Exception("Error with getParameters()");
    }
    if (nc.getAlgorithm() != null) {
        throw new Exception("Error with getAlgorithm()");
    }
    if (nc.getProvider() != null) { // not associated with any provider
        throw new Exception("Error with getProvider()");
    }
    System.out.println("Test Done");
}
 
Example #25
Source File: TestNPE.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
    NullCipher nc = new NullCipher();
    // testing init(...)
    nc.init(Cipher.ENCRYPT_MODE, (Certificate) null);
    nc.init(Cipher.ENCRYPT_MODE, (Certificate) null, (SecureRandom) null);
    nc.init(Cipher.ENCRYPT_MODE, (Key) null);
    nc.init(Cipher.ENCRYPT_MODE, (Key) null, (AlgorithmParameters) null);
    nc.init(Cipher.ENCRYPT_MODE, (Key) null, (AlgorithmParameterSpec) null);
    nc.init(Cipher.ENCRYPT_MODE, (Key) null, (AlgorithmParameterSpec) null,
        (SecureRandom) null);
    nc.init(Cipher.ENCRYPT_MODE, (Key) null, (AlgorithmParameters) null,
        (SecureRandom) null);
    nc.init(Cipher.ENCRYPT_MODE, (Key) null, (SecureRandom) null);
    // testing getBlockSize()
    if (nc.getBlockSize() != 1) {
        throw new Exception("Error with getBlockSize()");
    }
    // testing update(...)
    byte[] out = nc.update(BYTES);
    if (!Arrays.equals(out, BYTES)) {
        throw new Exception("Error with update(byte[])");
    }
    out = nc.update(BYTES, 0, BYTES.length);
    if (!Arrays.equals(out, BYTES)) {
        throw new Exception("Error with update(byte[], int, int)");
    }
    if (nc.update(BYTES, 0, BYTES.length, out) != BYTES.length) {
        throw new Exception("Error with update(byte[], int, int, byte[])");
    }
    if (nc.update(BYTES, 0, BYTES.length, out, 0) != BYTES.length) {
        throw new Exception(
            "Error with update(byte[], int, int, byte[], int)");
    }
    // testing doFinal(...)
    if (nc.doFinal() != null) {
        throw new Exception("Error with doFinal()");
    }
    if (nc.doFinal(out, 0) != 0) {
         throw new Exception("Error with doFinal(byte[], 0)");
    }
    out = nc.doFinal(BYTES);
    if (!Arrays.equals(out, BYTES)) {
        throw new Exception("Error with doFinal(byte[])");
    }
    out = nc.doFinal(BYTES, 0, BYTES.length);
    if (!Arrays.equals(out, BYTES)) {
        throw new Exception("Error with doFinal(byte[], int, int)");
    }
    if (nc.doFinal(BYTES, 0, BYTES.length, out) != BYTES.length) {
        throw new Exception(
            "Error with doFinal(byte[], int, int, byte[])");
    }
    if (nc.doFinal(BYTES, 0, BYTES.length, out, 0) != BYTES.length) {
        throw new Exception(
            "Error with doFinal(byte[], int, int, byte[], int)");
    }
    // testing getExemptionMechanism()
    if (nc.getExemptionMechanism() != null) {
        throw new Exception("Error with getExemptionMechanism()");
    }
    // testing getOutputSize(int)
    if (nc.getOutputSize(5) != 5) {
        throw new Exception("Error with getOutputSize()");
    }
    // testing getIV(), getParameters(), getAlgorithm(), etc.
    if (nc.getIV() == null) { // should've been null;
                              // left it as is for backward-compatibility
        throw new Exception("Error with getIV()");
    }
    if (nc.getParameters() != null) {
        throw new Exception("Error with getParameters()");
    }
    if (nc.getAlgorithm() != null) {
        throw new Exception("Error with getAlgorithm()");
    }
    if (nc.getProvider() != null) { // not associated with any provider
        throw new Exception("Error with getProvider()");
    }
    System.out.println("Test Done");
}
 
Example #26
Source File: TestNPE.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
    NullCipher nc = new NullCipher();
    // testing init(...)
    nc.init(Cipher.ENCRYPT_MODE, (Certificate) null);
    nc.init(Cipher.ENCRYPT_MODE, (Certificate) null, (SecureRandom) null);
    nc.init(Cipher.ENCRYPT_MODE, (Key) null);
    nc.init(Cipher.ENCRYPT_MODE, (Key) null, (AlgorithmParameters) null);
    nc.init(Cipher.ENCRYPT_MODE, (Key) null, (AlgorithmParameterSpec) null);
    nc.init(Cipher.ENCRYPT_MODE, (Key) null, (AlgorithmParameterSpec) null,
        (SecureRandom) null);
    nc.init(Cipher.ENCRYPT_MODE, (Key) null, (AlgorithmParameters) null,
        (SecureRandom) null);
    nc.init(Cipher.ENCRYPT_MODE, (Key) null, (SecureRandom) null);
    // testing getBlockSize()
    if (nc.getBlockSize() != 1) {
        throw new Exception("Error with getBlockSize()");
    }
    // testing update(...)
    byte[] out = nc.update(BYTES);
    if (!Arrays.equals(out, BYTES)) {
        throw new Exception("Error with update(byte[])");
    }
    out = nc.update(BYTES, 0, BYTES.length);
    if (!Arrays.equals(out, BYTES)) {
        throw new Exception("Error with update(byte[], int, int)");
    }
    if (nc.update(BYTES, 0, BYTES.length, out) != BYTES.length) {
        throw new Exception("Error with update(byte[], int, int, byte[])");
    }
    if (nc.update(BYTES, 0, BYTES.length, out, 0) != BYTES.length) {
        throw new Exception(
            "Error with update(byte[], int, int, byte[], int)");
    }
    // testing doFinal(...)
    if (nc.doFinal() != null) {
        throw new Exception("Error with doFinal()");
    }
    if (nc.doFinal(out, 0) != 0) {
         throw new Exception("Error with doFinal(byte[], 0)");
    }
    out = nc.doFinal(BYTES);
    if (!Arrays.equals(out, BYTES)) {
        throw new Exception("Error with doFinal(byte[])");
    }
    out = nc.doFinal(BYTES, 0, BYTES.length);
    if (!Arrays.equals(out, BYTES)) {
        throw new Exception("Error with doFinal(byte[], int, int)");
    }
    if (nc.doFinal(BYTES, 0, BYTES.length, out) != BYTES.length) {
        throw new Exception(
            "Error with doFinal(byte[], int, int, byte[])");
    }
    if (nc.doFinal(BYTES, 0, BYTES.length, out, 0) != BYTES.length) {
        throw new Exception(
            "Error with doFinal(byte[], int, int, byte[], int)");
    }
    // testing getExemptionMechanism()
    if (nc.getExemptionMechanism() != null) {
        throw new Exception("Error with getExemptionMechanism()");
    }
    // testing getOutputSize(int)
    if (nc.getOutputSize(5) != 5) {
        throw new Exception("Error with getOutputSize()");
    }
    // testing getIV(), getParameters(), getAlgorithm(), etc.
    if (nc.getIV() == null) { // should've been null;
                              // left it as is for backward-compatibility
        throw new Exception("Error with getIV()");
    }
    if (nc.getParameters() != null) {
        throw new Exception("Error with getParameters()");
    }
    if (nc.getAlgorithm() != null) {
        throw new Exception("Error with getAlgorithm()");
    }
    if (nc.getProvider() != null) { // not associated with any provider
        throw new Exception("Error with getProvider()");
    }
    System.out.println("Test Done");
}
 
Example #27
Source File: TestNPE.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
    NullCipher nc = new NullCipher();
    // testing init(...)
    nc.init(Cipher.ENCRYPT_MODE, (Certificate) null);
    nc.init(Cipher.ENCRYPT_MODE, (Certificate) null, (SecureRandom) null);
    nc.init(Cipher.ENCRYPT_MODE, (Key) null);
    nc.init(Cipher.ENCRYPT_MODE, (Key) null, (AlgorithmParameters) null);
    nc.init(Cipher.ENCRYPT_MODE, (Key) null, (AlgorithmParameterSpec) null);
    nc.init(Cipher.ENCRYPT_MODE, (Key) null, (AlgorithmParameterSpec) null,
        (SecureRandom) null);
    nc.init(Cipher.ENCRYPT_MODE, (Key) null, (AlgorithmParameters) null,
        (SecureRandom) null);
    nc.init(Cipher.ENCRYPT_MODE, (Key) null, (SecureRandom) null);
    // testing getBlockSize()
    if (nc.getBlockSize() != 1) {
        throw new Exception("Error with getBlockSize()");
    }
    // testing update(...)
    byte[] out = nc.update(BYTES);
    if (!Arrays.equals(out, BYTES)) {
        throw new Exception("Error with update(byte[])");
    }
    out = nc.update(BYTES, 0, BYTES.length);
    if (!Arrays.equals(out, BYTES)) {
        throw new Exception("Error with update(byte[], int, int)");
    }
    if (nc.update(BYTES, 0, BYTES.length, out) != BYTES.length) {
        throw new Exception("Error with update(byte[], int, int, byte[])");
    }
    if (nc.update(BYTES, 0, BYTES.length, out, 0) != BYTES.length) {
        throw new Exception(
            "Error with update(byte[], int, int, byte[], int)");
    }
    // testing doFinal(...)
    if (nc.doFinal() != null) {
        throw new Exception("Error with doFinal()");
    }
    if (nc.doFinal(out, 0) != 0) {
         throw new Exception("Error with doFinal(byte[], 0)");
    }
    out = nc.doFinal(BYTES);
    if (!Arrays.equals(out, BYTES)) {
        throw new Exception("Error with doFinal(byte[])");
    }
    out = nc.doFinal(BYTES, 0, BYTES.length);
    if (!Arrays.equals(out, BYTES)) {
        throw new Exception("Error with doFinal(byte[], int, int)");
    }
    if (nc.doFinal(BYTES, 0, BYTES.length, out) != BYTES.length) {
        throw new Exception(
            "Error with doFinal(byte[], int, int, byte[])");
    }
    if (nc.doFinal(BYTES, 0, BYTES.length, out, 0) != BYTES.length) {
        throw new Exception(
            "Error with doFinal(byte[], int, int, byte[], int)");
    }
    // testing getExemptionMechanism()
    if (nc.getExemptionMechanism() != null) {
        throw new Exception("Error with getExemptionMechanism()");
    }
    // testing getOutputSize(int)
    if (nc.getOutputSize(5) != 5) {
        throw new Exception("Error with getOutputSize()");
    }
    // testing getIV(), getParameters(), getAlgorithm(), etc.
    if (nc.getIV() == null) { // should've been null;
                              // left it as is for backward-compatibility
        throw new Exception("Error with getIV()");
    }
    if (nc.getParameters() != null) {
        throw new Exception("Error with getParameters()");
    }
    if (nc.getAlgorithm() != null) {
        throw new Exception("Error with getAlgorithm()");
    }
    if (nc.getProvider() != null) { // not associated with any provider
        throw new Exception("Error with getProvider()");
    }
    System.out.println("Test Done");
}
 
Example #28
Source File: NullCipherTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
protected void setUp() throws Exception {
    super.setUp();
    c = new NullCipher();
}
 
Example #29
Source File: TestNPE.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
    NullCipher nc = new NullCipher();
    // testing init(...)
    nc.init(Cipher.ENCRYPT_MODE, (Certificate) null);
    nc.init(Cipher.ENCRYPT_MODE, (Certificate) null, (SecureRandom) null);
    nc.init(Cipher.ENCRYPT_MODE, (Key) null);
    nc.init(Cipher.ENCRYPT_MODE, (Key) null, (AlgorithmParameters) null);
    nc.init(Cipher.ENCRYPT_MODE, (Key) null, (AlgorithmParameterSpec) null);
    nc.init(Cipher.ENCRYPT_MODE, (Key) null, (AlgorithmParameterSpec) null,
        (SecureRandom) null);
    nc.init(Cipher.ENCRYPT_MODE, (Key) null, (AlgorithmParameters) null,
        (SecureRandom) null);
    nc.init(Cipher.ENCRYPT_MODE, (Key) null, (SecureRandom) null);
    // testing getBlockSize()
    if (nc.getBlockSize() != 1) {
        throw new Exception("Error with getBlockSize()");
    }
    // testing update(...)
    byte[] out = nc.update(BYTES);
    if (!Arrays.equals(out, BYTES)) {
        throw new Exception("Error with update(byte[])");
    }
    out = nc.update(BYTES, 0, BYTES.length);
    if (!Arrays.equals(out, BYTES)) {
        throw new Exception("Error with update(byte[], int, int)");
    }
    if (nc.update(BYTES, 0, BYTES.length, out) != BYTES.length) {
        throw new Exception("Error with update(byte[], int, int, byte[])");
    }
    if (nc.update(BYTES, 0, BYTES.length, out, 0) != BYTES.length) {
        throw new Exception(
            "Error with update(byte[], int, int, byte[], int)");
    }
    // testing doFinal(...)
    if (nc.doFinal() != null) {
        throw new Exception("Error with doFinal()");
    }
    if (nc.doFinal(out, 0) != 0) {
         throw new Exception("Error with doFinal(byte[], 0)");
    }
    out = nc.doFinal(BYTES);
    if (!Arrays.equals(out, BYTES)) {
        throw new Exception("Error with doFinal(byte[])");
    }
    out = nc.doFinal(BYTES, 0, BYTES.length);
    if (!Arrays.equals(out, BYTES)) {
        throw new Exception("Error with doFinal(byte[], int, int)");
    }
    if (nc.doFinal(BYTES, 0, BYTES.length, out) != BYTES.length) {
        throw new Exception(
            "Error with doFinal(byte[], int, int, byte[])");
    }
    if (nc.doFinal(BYTES, 0, BYTES.length, out, 0) != BYTES.length) {
        throw new Exception(
            "Error with doFinal(byte[], int, int, byte[], int)");
    }
    // testing getExemptionMechanism()
    if (nc.getExemptionMechanism() != null) {
        throw new Exception("Error with getExemptionMechanism()");
    }
    // testing getOutputSize(int)
    if (nc.getOutputSize(5) != 5) {
        throw new Exception("Error with getOutputSize()");
    }
    // testing getIV(), getParameters(), getAlgorithm(), etc.
    if (nc.getIV() == null) { // should've been null;
                              // left it as is for backward-compatibility
        throw new Exception("Error with getIV()");
    }
    if (nc.getParameters() != null) {
        throw new Exception("Error with getParameters()");
    }
    if (nc.getAlgorithm() != null) {
        throw new Exception("Error with getAlgorithm()");
    }
    if (nc.getProvider() != null) { // not associated with any provider
        throw new Exception("Error with getProvider()");
    }
    System.out.println("Test Done");
}
 
Example #30
Source File: EncryptedInputStream.java    From Rumble with GNU General Public License v3.0 4 votes vote down vote up
protected EncryptedInputStream(InputStream is) {
    super(is);
    input = is;
    cipher = new NullCipher();
}