/** * Creates a {@link javax.crypto.Cipher} instance from a {@link javax.crypto.CipherSpi}. * * @param cipherSpi the {@link javax.cyrpto.CipherSpi} instance * @param transformation the transformation cipherSpi was obtained for * @return a {@link javax.crypto.Cipher} instance * @throws Throwable in case of an error */ private static Cipher getCipher(final CipherSpi cipherSpi, final String transformation) throws Throwable { //06-19 15:22:00.727: W/erverSocketPipelineSink(31810): Caused by: java.lang.NoSuchMethodException: <init> [class javax.crypto.CipherSpi, class java.lang.String] /* This API isn't public - usually you're expected to simply use Cipher.getInstance(). * Due to the signed-jar restriction for JCE providers that is not an option, so we * use one of the private constructors of Cipher. */ final Class<Cipher> cipherClass = Cipher.class; final Constructor<Cipher> cipherConstructor = cipherClass.getDeclaredConstructor(CipherSpi.class, String.class); cipherConstructor.setAccessible(true); try { return cipherConstructor.newInstance(cipherSpi, transformation); } catch (final InvocationTargetException e) { throw e.getCause(); } }
/** * @tests javax.crypto.Cipher#getBlockSize() */ @TestTargets({ @TestTargetNew( level = TestLevel.COMPLETE, notes = "", method = "getBlockSize", args = {} ), @TestTargetNew( level = TestLevel.COMPLETE, notes = "", clazz = CipherSpi.class, method = "engineGetBlockSize", args = {} ) }) public void test_getBlockSize() throws Exception { final String algorithm = "DESede/CBC/PKCS5Padding"; Cipher cipher = Cipher.getInstance(algorithm); assertEquals("Block size does not match", 8, cipher.getBlockSize()); }
@TestTargets({ @TestTargetNew( level = TestLevel.COMPLETE, notes = "", method = "getParameters", args = {} ), @TestTargetNew( level = TestLevel.COMPLETE, notes = "", clazz = CipherSpi.class, method = "engineGetParameters", args = {} ) }) public void testGetParameters() throws Exception { Cipher c = Cipher.getInstance("DES"); assertNull(c.getParameters()); }
@TestTargets({ @TestTargetNew( level = TestLevel.COMPLETE, notes = "Checks inherited method from Cipher.", method = "getIV", args = {} ), @TestTargetNew( level = TestLevel.COMPLETE, notes = "Checks inherited method from Cipher.", clazz = CipherSpi.class, method = "engineGetIV", args = {} ) }) public void testGetIV() { assertTrue("Incorrect IV", Arrays.equals(c.getIV(), new byte[8])); }
/** * Sets the padding of a {@link javax.crypto.CipherSpi} instance. * * Like {@link #getCipher(String)}, we're accessing a private API * here, so me must work around the access restrictions * * @param cipherSpi the {@link javax.crypto.CipherSpi} instance * @param padding the padding to set * @throws Throwable if {@link javax.crypto.CipherSpi#engineSetPadding} throws */ private static void cipherSpiSetPadding(final CipherSpi cipherSpi, final String padding) throws Throwable { final Method engineSetPadding = getMethod(cipherSpi.getClass(), "engineSetPadding", String.class); engineSetPadding.setAccessible(true); try { engineSetPadding.invoke(cipherSpi, padding); } catch (final InvocationTargetException e) { throw e.getCause(); } }
/** * Sets the mode of a {@link javax.crypto.CipherSpi} instance. * * Like {@link #getCipher(String)}, we're accessing a private API * here, so me must work around the access restrictions * * @param cipherSpi the {@link javax.crypto.CipherSpi} instance * @param mode the mode to set * @throws Throwable if {@link javax.crypto.CipherSpi#engineSetPadding} throws */ private static void cipherSpiSetMode(final CipherSpi cipherSpi, final String mode) throws Throwable { final Method engineSetMode = getMethod(cipherSpi.getClass(), "engineSetMode", String.class); engineSetMode.setAccessible(true); try { engineSetMode.invoke(cipherSpi, mode); } catch (final InvocationTargetException e) { throw e.getCause(); } }
/** * @tests javax.crypto.Cipher#getInstance(java.lang.String) */ @TestTargets({ @TestTargetNew( level = TestLevel.COMPLETE, notes = "", method = "getInstance", args = {java.lang.String.class} ), @TestTargetNew( level = TestLevel.COMPLETE, notes = "", clazz = CipherSpi.class, method = "engineSetMode", args = {java.lang.String.class} ), @TestTargetNew( level = TestLevel.COMPLETE, notes = "", clazz = CipherSpi.class, method = "engineSetPadding", args = {java.lang.String.class} ) }) public void test_getInstanceLjava_lang_String() throws Exception { Cipher cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding"); assertNotNull("Received a null Cipher instance", cipher); try { Cipher.getInstance("WrongAlgorithmName"); fail("NoSuchAlgorithmException expected"); } catch (NoSuchAlgorithmException e) { //expected } // RI throws NoSuchAlgorithmException for wrong padding. }
/** * @tests javax.crypto.Cipher#getOutputSize(int) */ @TestTargets({ @TestTargetNew( level = TestLevel.COMPLETE, notes = "", method = "getOutputSize", args = {int.class} ), @TestTargetNew( level = TestLevel.COMPLETE, notes = "", clazz = CipherSpi.class, method = "engineGetOutputSize", args = {int.class} ) }) public void test_getOutputSizeI() throws Exception { SecureRandom sr = new SecureRandom(); Cipher cipher = Cipher.getInstance(algorithm + "/ECB/PKCS5Padding"); try { cipher.getOutputSize(25); fail("IllegalStateException expected"); } catch (IllegalStateException e) { //expected } cipher.init(Cipher.ENCRYPT_MODE, cipherKey, sr); // A 25-byte input could result in at least 4 8-byte blocks int result = cipher.getOutputSize(25); assertTrue("Output size too small", result > 31); // A 8-byte input should result in 2 8-byte blocks result = cipher.getOutputSize(8); assertTrue("Output size too small", result > 15); }
/** * @tests javax.crypto.Cipher#init(int, java.security.Key, * java.security.SecureRandom) */ @TestTargets({ @TestTargetNew( level = TestLevel.COMPLETE, notes = "", method = "init", args = {int.class, java.security.Key.class, java.security.SecureRandom.class} ), @TestTargetNew( level = TestLevel.COMPLETE, notes = "", clazz = CipherSpi.class, method = "engineInit", args = {int.class, java.security.Key.class, java.security.SecureRandom.class} ) }) public void test_initILjava_security_KeyLjava_security_SecureRandom() throws Exception { SecureRandom sr = new SecureRandom(); Cipher cipher = Cipher.getInstance(algorithm + "/ECB/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, cipherKey, sr); cipher = Cipher.getInstance("DES/CBC/NoPadding"); try { cipher.init(Cipher.ENCRYPT_MODE, cipherKey, sr); fail("InvalidKeyException expected"); } catch (InvalidKeyException e) { //expected } }
public CipherSpi create(Object constructorParam) { try { return Adaptor.adapt(type.newInstance(), CipherSpi.class); } catch (Exception e) { throw new IllegalArgumentException("Invalid type: " + type, e); } }
public final void testGetBlockSize001() throws Exception { Provider.Service s = provider.getService("Cipher", algorithm); PrivateProxy cipherSpi = PrivateProxyFactory.createPrivateProxy((CipherSpi) s.newInstance(null)); cipher = Cipher.getInstance(algorithm,provider); cipher.init(Cipher.ENCRYPT_MODE, Util.getInstanceKey()); try { assertEquals("Must be same blockSize",cipherSpi.call("engineGetBlockSize", new Object[0]),cipher.getBlockSize()); } catch (Throwable e) { fail("fail with: " + e); } }
testCipher(CipherSpi c, Provider p, String s) { super(c, p, s); }
public CipherSpiFactory(Class<? extends javax.crypto.CipherSpi> type) { this.type = type; }
/** * Creates a Cipher object. * * @param cipherSpi the delegate * @param provider the provider * @param transformation the transformation */ protected CipherForKeyProtector(CipherSpi cipherSpi, Provider provider, String transformation) { super(cipherSpi, provider, transformation); }