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"); } }
/** * read() method testing. Tests that method returns the correct value * (related to the InputStream) and that it returns -1 at the end of stream. */ @TestTargetNew( level = TestLevel.COMPLETE, notes = "Can not check IOException.", method = "read", args = {} ) public void testRead1() throws Exception { byte[] data = new byte[] { -127, -100, -50, -10, -1, 0, 1, 10, 50, 127 }; TestInputStream tis = new TestInputStream(data); CipherInputStream cis = new CipherInputStream(tis, new NullCipher()); byte res; for (int i = 0; i < data.length; i++) { if ((res = (byte) cis.read()) != data[i]) { fail("read() returned the incorrect value. " + "Expected: " + data[i] + ", Got: " + res + "."); } } if (cis.read() != -1) { fail("read() should return -1 at the end of the stream."); } }
/** * write(int b) method testing. Tests that method writes correct values to * the underlying output stream. */ @TestTargetNew( level = TestLevel.COMPLETE, notes = "Can not check IOException.", method = "write", args = {int.class} ) 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."); } }
/** * write(byte[] b) method testing. Tests that method writes correct values * to the underlying output stream. */ @TestTargetNew( level = TestLevel.COMPLETE, notes = "Can not check IOException.", method = "write", args = {byte[].class} ) 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 } }
/** * write(byte[] b, int off, int len) method testing. */ @TestTargetNew( level = TestLevel.COMPLETE, notes = "Can not check IOException.", method = "write", args = {byte[].class, int.class, int.class} ) public void testWrite3() 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, 1); } cos.flush(); byte[] result = tos.toByteArray(); if (!Arrays.equals(result, data)) { fail("CipherOutputStream wrote incorrect data."); } }
/** * @tests write(byte[] b, int off, int len) */ @TestTargetNew( level = TestLevel.COMPLETE, notes = "Can not check IOException.", method = "write", args = {byte[].class, int.class, int.class} ) 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 }
/** * @tests javax.crypto.CipherInputStream#read(byte[] b, int off, int len) */ @TestTargetNew( level = TestLevel.PARTIAL, notes = "Regression test. Checks NullPointerException", method = "read", args = {byte[].class, int.class, int.class} ) public void testReadBII() throws Exception { // Regression for HARMONY-1080 CipherInputStream stream = new CipherInputStream(null, new NullCipher()); try { stream.read(new byte[1], 1, 0); fail("NullPointerException expected"); } catch (NullPointerException e) { // expected } }
/** * readObject(ObjectInputStream s) method testing. Tests if the * serialization/deserialization works correctly: object is serialized, * deserialized, the content od deserialized object equals to the content of * initial object. */ @TestTargetNew( level = TestLevel.COMPLETE, notes = "", method = "!Serialization", args = {} ) public void testReadObject() throws Exception { String secret = "secret string"; SealedObject so = new SealedObject(secret, new NullCipher()); ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(bos); oos.writeObject(so); ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream( bos.toByteArray())); SealedObject so_des = (SealedObject) ois.readObject(); assertEquals("The secret content of deserialized object " + "should be equal to the secret content of initial object", secret, so_des.getObject(new NullCipher())); assertEquals("The value returned by getAlgorithm() method of " + "deserialized object should be equal to the value returned " + "by getAlgorithm() method of initial object", so .getAlgorithm(), so_des.getAlgorithm()); }
/** * read() method testing. Tests that method returns the correct value * (related to the InputStream) and that it returns -1 at the end of stream. */ public void testRead1() throws Exception { byte[] data = new byte[] { -127, -100, -50, -10, -1, 0, 1, 10, 50, 127 }; TestInputStream tis = new TestInputStream(data); CipherInputStream cis = new CipherInputStream(tis, new NullCipher()); byte res; for (int i = 0; i < data.length; i++) { if ((res = (byte) cis.read()) != data[i]) { fail("read() returned the incorrect value. " + "Expected: " + data[i] + ", Got: " + res + "."); } } if (cis.read() != -1) { fail("read() should return -1 at the end of the stream."); } }
/** * readObject(ObjectInputStream s) method testing. Tests if the * serialization/deserialization works correctly: object is serialized, * deserialized, the content od deserialized object equals to the * content of initial object. */ public void testReadObject() throws Exception { String secret = "secret string"; SealedObject so = new SealedObject(secret, new NullCipher()); ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(bos); oos.writeObject(so); ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream( bos.toByteArray())); SealedObject so_des = (SealedObject) ois.readObject(); assertEquals("The secret content of deserialized object " + "should be equal to the secret content of initial object", secret, so_des.getObject(new NullCipher())); assertEquals("The value returned by getAlgorithm() method of " + "deserialized object should be equal to the value returned " + "by getAlgorithm() method of initial object", so .getAlgorithm(), so_des.getAlgorithm()); }
/** * 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()); }
/** * 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)); }
/** * 解密<br> * 用私钥解密 * * @param data * @param key * @return * @throws Exception */ public static byte[] decrypt(byte[] data, String key) throws Exception { // 对密钥解密 byte[] keyBytes = decryptBASE64(key); // 取得私钥 PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes); KeyFactory keyFactory = ECKeyFactory.INSTANCE; ECPrivateKey priKey = (ECPrivateKey) keyFactory .generatePrivate(pkcs8KeySpec); ECPrivateKeySpec ecPrivateKeySpec = new ECPrivateKeySpec(priKey.getS(), priKey.getParams()); // 对数据解密 // TODO Chipher不支持EC算法 未能实现 Cipher cipher = new NullCipher(); // Cipher.getInstance(ALGORITHM, keyFactory.getProvider()); cipher.init(Cipher.DECRYPT_MODE, priKey, ecPrivateKeySpec.getParams()); return cipher.doFinal(data); }
/** * 加密<br> * 用公钥加密 * * @param data * @param privateKey * @return * @throws Exception */ public static byte[] encrypt(byte[] data, String privateKey) throws Exception { // 对公钥解密 byte[] keyBytes = decryptBASE64(privateKey); // 取得公钥 X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes); KeyFactory keyFactory = ECKeyFactory.INSTANCE; ECPublicKey pubKey = (ECPublicKey) keyFactory .generatePublic(x509KeySpec); ECPublicKeySpec ecPublicKeySpec = new ECPublicKeySpec(pubKey.getW(), pubKey.getParams()); // 对数据加密 // TODO Chipher不支持EC算法 未能实现 Cipher cipher = new NullCipher(); // Cipher.getInstance(ALGORITHM, keyFactory.getProvider()); cipher.init(Cipher.ENCRYPT_MODE, pubKey, ecPublicKeySpec.getParams()); return cipher.doFinal(data); }
@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); }
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."); }
public Cipher cipher () throws NoSuchAlgorithmException, NoSuchPaddingException { if (cipherAlgorithm == null) throw new NoSuchAlgorithmException (toString () + ": unresolved cipher suite"); if (cipherAlgorithm == CipherAlgorithm.NULL) return new NullCipher (); String alg = null; if (isCBCMode) alg = cipherAlgorithm + "/CBC/NoPadding"; else alg = cipherAlgorithm.toString(); GetSecurityPropertyAction gspa = new GetSecurityPropertyAction ("jessie.jce.provider"); final String provider = (String) AccessController.doPrivileged (gspa); if (provider != null) { try { return Cipher.getInstance (alg, provider); } catch (NoSuchProviderException nspe) { } } return Cipher.getInstance (alg); }
public Cipher cipher () throws NoSuchAlgorithmException, NoSuchPaddingException { if (cipherAlgorithm == null) throw new NoSuchAlgorithmException (toString () + ": unresolved cipher suite"); if (cipherAlgorithm == CipherAlgorithm.NULL) return new NullCipher (); String alg = null; if (cipherAlgorithm == CipherAlgorithm.RC4) alg = "RC4"; else alg = cipherAlgorithm + "/CBC/NoPadding"; GetSecurityPropertyAction gspa = new GetSecurityPropertyAction ("jessie.jce.provider"); final String provider = (String) AccessController.doPrivileged (gspa); if (provider != null) { try { return Cipher.getInstance (alg, provider); } catch (NoSuchProviderException nspe) { } } return Cipher.getInstance (alg); }
/** * read(byte[] b) method testing. Tests that method returns the correct * value (related to the InputStream) and that it returns -1 at the end of * stream. */ @TestTargetNew( level = TestLevel.COMPLETE, notes = "Can not check IOException.", method = "read", args = {byte[].class} ) public void testRead2() throws Exception { byte[] data = new byte[] { -127, -100, -50, -10, -1, 0, 1, 10, 50, 127 }; TestInputStream tis = new TestInputStream(data); CipherInputStream cis = new CipherInputStream(tis, new NullCipher()); int expected = data.length; byte[] result = new byte[expected]; int ind = 0; // index into the data array (to check the got data) int got = cis.read(result); // the number of got bytes while (true) { for (int j = 0; j < got - ind; j++) { if (result[j] != data[ind + j]) { fail("read(byte[] b) returned incorrect data."); } } if (got == expected) { break; } else if (got > expected) { fail("The data returned by read(byte[] b) " + "is larger than expected."); } else { ind = got; got += cis.read(result); } } if (cis.read(result) != -1) { fail("read(byte[] b) should return -1 " + "at the end of the stream."); } }
/** * read(byte[] b, int off, int len) method testing. Tests that method * returns the correct value (related to the InputStream), that it discards * bytes in the case of null buffer, and that it returns -1 at the end of * stream. */ @TestTargetNew( level = TestLevel.COMPLETE, notes = "Can not check IOException.", method = "read", args = {byte[].class, int.class, int.class} ) public void testRead3() throws Exception { byte[] data = new byte[] { -127, -100, -50, -10, -1, 0, 1, 10, 50, 127 }; TestInputStream tis = new TestInputStream(data); CipherInputStream cis = new CipherInputStream(tis, new NullCipher()); int expected = data.length; byte[] result = new byte[expected]; int skip = 2; int ind = skip; // index into the data array (to check the got data) // should read and discard bytes; cis.read(null, 0, skip); int got = skip + cis.read(result, 0, 1); // the number of got bytes while (true) { for (int j = 0; j < got - ind; j++) { assertEquals("read(byte[] b, int off, int len) " + "returned incorrect data.", result[j], data[ind + j]); } if (got == expected) { break; } else if (got > expected) { fail("The data returned by " + "read(byte[] b, int off, int len) " + "is larger than expected."); } else { ind = got; got += cis.read(result, 0, 3); } } if (cis.read(result, 0, 1) != -1) { fail("read() should return -1 at the end of the stream."); } }
/** * available() method testing. Tests that the method always return 0. */ @TestTargetNew( level = TestLevel.COMPLETE, notes = "Can not check IOException.", method = "available", args = {} ) public void testAvailable() throws Exception { byte[] data = new byte[] { -127, -100, -50, -10, -1, 0, 1, 10, 50, 127 }; TestInputStream tis = new TestInputStream(data); CipherInputStream cis = new CipherInputStream(tis, new NullCipher()); assertEquals("The returned by available() method value " + "should be 0.", cis.available(), 0); }
/** * close() method testing. Tests that the method calls the close() * method of the underlying input stream. */ @TestTargetNew( level = TestLevel.COMPLETE, notes = "Can not check IOException.", method = "close", args = {} ) public void testClose() throws Exception { byte[] data = new byte[] { -127, -100, -50, -10, -1, 0, 1, 10, 50, 127 }; TestInputStream tis = new TestInputStream(data); CipherInputStream cis = new CipherInputStream(tis, new NullCipher()); cis.close(); assertTrue("The close() method should call the close() method " + "of its underlying input stream.", tis.wasClosed()); }