use of java.security.InvalidKeyException in project robovm by robovm.
the class ExemptionMechanismTest method test_initLjava_security_KeyLjava_security_spec_AlgorithmParameterSpec.
public void test_initLjava_security_KeyLjava_security_spec_AlgorithmParameterSpec() throws Exception {
Provider mProv = (new SpiEngUtils()).new MyProvider("MyExMechProvider", "Provider for ExemptionMechanism testing", srvExemptionMechanism.concat(".").concat(defaultAlg), ExemptionMechanismProviderClass);
ExemptionMechanism em = new ExemptionMechanism(new MyExemptionMechanismSpi(), mProv, defaultAlg) {
};
Key key = new MyExemptionMechanismSpi().new tmpKey("Proba", new byte[0]);
em.init(key, new RSAKeyGenParameterSpec(10, new BigInteger("10")));
try {
em.init(key, (AlgorithmParameterSpec) null);
fail("InvalidAlgorithmParameterException expected");
} catch (InvalidAlgorithmParameterException e) {
//expected
}
KeyGenerator kg = KeyGenerator.getInstance("DES");
kg.init(56, new SecureRandom());
key = kg.generateKey();
try {
em.init(null, new RSAKeyGenParameterSpec(10, new BigInteger("10")));
fail("InvalidKeyException expected");
} catch (InvalidKeyException e) {
//expected
}
try {
em.init(key, new RSAKeyGenParameterSpec(10, new BigInteger("10")));
fail("ExemptionMechanismException expected");
} catch (ExemptionMechanismException e) {
//expected
}
}
use of java.security.InvalidKeyException in project robovm by robovm.
the class CipherTest method test_initWithKeyAlgorithmParameterSpecSecureRandom.
/**
* javax.crypto.Cipher#init(int, java.security.Key,
* java.security.spec.AlgorithmParameterSpec,
* java.security.SecureRandom)
*/
public void test_initWithKeyAlgorithmParameterSpecSecureRandom() throws Exception {
AlgorithmParameterSpec ap = new IvParameterSpec(IV);
Cipher cipher = Cipher.getInstance(ALGORITHM_3DES + "/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_3DES, ap, new SecureRandom());
byte[] cipherIV = cipher.getIV();
assertTrue("IVs differ", Arrays.equals(cipherIV, IV));
cipher = Cipher.getInstance("DES/CBC/NoPadding");
try {
cipher.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_3DES, ap, new SecureRandom());
fail();
} catch (InvalidKeyException expected) {
}
cipher = Cipher.getInstance("DES/CBC/NoPadding");
ap = new RSAKeyGenParameterSpec(10, new BigInteger("10"));
try {
cipher.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_DES, ap, new SecureRandom());
fail();
} catch (InvalidAlgorithmParameterException expected) {
}
}
use of java.security.InvalidKeyException in project robovm by robovm.
the class CipherTest method test_wrap_java_security_Key.
public void test_wrap_java_security_Key() throws Exception {
AlgorithmParameterSpec ap = new IvParameterSpec(IV);
Cipher c = Cipher.getInstance("DES/CBC/PKCS5Padding");
c.init(Cipher.WRAP_MODE, CIPHER_KEY_DES, ap, new SecureRandom());
assertNotNull(c.wrap(CIPHER_KEY_DES));
assertNotNull(c.wrap(CIPHER_KEY_3DES));
String certName = Support_Resources.getURL("test.cert");
InputStream is = new URL(certName).openConnection().getInputStream();
CertificateFactory cf = CertificateFactory.getInstance("X.509");
Certificate cert = cf.generateCertificate(is);
assertNotNull(c.wrap(cert.getPublicKey()));
c = Cipher.getInstance("DES/CBC/NoPadding");
c.init(Cipher.WRAP_MODE, CIPHER_KEY_DES, ap, new SecureRandom());
try {
assertNotNull(c.wrap(cert.getPublicKey()));
fail();
} catch (IllegalBlockSizeException expected) {
}
c.init(Cipher.DECRYPT_MODE, CIPHER_KEY_DES, ap, new SecureRandom());
try {
c.wrap(CIPHER_KEY_DES);
fail();
} catch (IllegalStateException expected) {
}
c.init(Cipher.WRAP_MODE, CIPHER_KEY_DES, ap, new SecureRandom());
try {
c.wrap(new Mock_Key());
fail();
} catch (InvalidKeyException expected) {
}
}
use of java.security.InvalidKeyException in project robovm by robovm.
the class CipherTest method test_initWithKey.
/**
* javax.crypto.Cipher#init(int, java.security.Key)
*/
public void test_initWithKey() throws Exception {
Cipher cipher = Cipher.getInstance(ALGORITHM_3DES + "/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_3DES);
cipher = Cipher.getInstance("DES/CBC/NoPadding");
try {
cipher.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_3DES);
fail();
} catch (InvalidKeyException expected) {
}
}
use of java.security.InvalidKeyException in project robovm by robovm.
the class EncryptedPrivateKeyInfoTest method test_ROUNDTRIP_GetKeySpecKeyProvider02.
/**
* Encrypted data contains invalid PKCS8 key info encoding
*/
public final void test_ROUNDTRIP_GetKeySpecKeyProvider02() {
boolean performed = false;
for (int i = 0; i < algName.length; i++) {
for (int l = 0; l < provider.length; l++) {
if (provider[l] == null) {
continue;
}
TestDataGenerator g;
try {
// generate test data
g = new TestDataGenerator(algName[i][0], algName[i][1], privateKeyInfoDamaged, provider[l]);
} catch (TestDataGenerator.AllowedFailure allowedFailure) {
continue;
}
try {
// create test object
EncryptedPrivateKeyInfo epki;
if (g.ap() == null) {
epki = new EncryptedPrivateKeyInfo(algName[i][0], g.ct());
} else {
epki = new EncryptedPrivateKeyInfo(g.ap(), g.ct());
}
try {
epki.getKeySpec(g.pubK() == null ? g.k() : g.pubK(), provider[l]);
fail(algName[i][0] + ", " + algName[i][1]);
} catch (InvalidKeyException e) {
}
performed = true;
} catch (NoSuchAlgorithmException allowedFailure) {
}
}
}
assertTrue("Test not performed", performed);
}
Aggregations