use of java.security.spec.RSAKeyGenParameterSpec in project robovm by robovm.
the class OpenSSLRSAKeyPairGenerator method initialize.
@Override
public void initialize(AlgorithmParameterSpec params, SecureRandom random) throws InvalidAlgorithmParameterException {
if (!(params instanceof RSAKeyGenParameterSpec)) {
throw new InvalidAlgorithmParameterException("Only RSAKeyGenParameterSpec supported");
}
RSAKeyGenParameterSpec spec = (RSAKeyGenParameterSpec) params;
final BigInteger publicExponent = spec.getPublicExponent();
if (publicExponent != null) {
this.publicExponent = publicExponent.toByteArray();
}
this.modulusBits = spec.getKeysize();
}
use of java.security.spec.RSAKeyGenParameterSpec in project robovm by robovm.
the class ExemptionMechanismSpiTest method testExemptionMechanismSpi01.
/**
* Test for <code>ExemptionMechanismSpi</code> constructor Assertion:
* constructs ExemptionMechanismSpi
* @throws Exception
*/
public void testExemptionMechanismSpi01() throws Exception {
Mock_ExemptionMechanismSpi emSpi = new Mock_ExemptionMechanismSpi() {
};
int len = MyExemptionMechanismSpi.getLength();
byte[] bbRes = emSpi.engineGenExemptionBlob();
assertEquals("Incorrect length", bbRes.length, len);
assertEquals("Incorrect result", emSpi.engineGenExemptionBlob(new byte[1], len), len);
assertEquals("Incorrect output size", 10, emSpi.engineGetOutputSize(100));
Key key = null;
AlgorithmParameters params = null;
AlgorithmParameterSpec parSpec = null;
try {
emSpi.engineInit(key);
fail("InvalidKeyException must be thrown");
} catch (InvalidKeyException e) {
}
try {
emSpi.engineInit(key, params);
fail("InvalidKeyException must be thrown");
} catch (InvalidKeyException e) {
}
try {
emSpi.engineInit(key, parSpec);
fail("InvalidKeyException must be thrown");
} catch (InvalidKeyException e) {
}
key = ((MyExemptionMechanismSpi) emSpi).new tmp1Key("Proba", new byte[0]);
try {
emSpi.engineInit(key);
fail("ExemptionMechanismException must be thrown");
} catch (ExemptionMechanismException e) {
}
try {
emSpi.engineInit(key, params);
fail("ExemptionMechanismException must be thrown");
} catch (ExemptionMechanismException e) {
}
try {
emSpi.engineInit(key, parSpec);
fail("ExemptionMechanismException must be thrown");
} catch (ExemptionMechanismException e) {
}
key = ((MyExemptionMechanismSpi) emSpi).new tmpKey("Proba", new byte[0]);
emSpi.engineInit(key);
emSpi.engineInit(key, AlgorithmParameters.getInstance("DH"));
emSpi.engineInit(key, new RSAKeyGenParameterSpec(10, new BigInteger("10")));
assertEquals("Incorrect result", 10, emSpi.engineGetOutputSize(100));
}
use of java.security.spec.RSAKeyGenParameterSpec 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.spec.RSAKeyGenParameterSpec 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.spec.RSAKeyGenParameterSpec in project robovm by robovm.
the class KeyAgreementTest method testInit04.
/**
* Test for the methods:
* <code>init(Key key, AlgorithmParameterSpec params)</code>
* <code>init(Key key, AlgorithmParameterSpec params, SecureRandom random)</code>
* <code>generateSecret()</code>
* Assertions: initializes KeyAgreement and returns byte array
*/
public void testInit04() throws Exception, InvalidAlgorithmParameterException {
if (!DEFSupported) {
fail(NotSupportMsg);
return;
}
createKeys();
KeyAgreement[] kAgs = createKAs();
DHParameterSpec dhPs = ((DHPrivateKey) privKey).getParams();
AlgorithmParameterSpec aps = new RSAKeyGenParameterSpec(10, new BigInteger("10"));
byte[] bbRes1;
byte[] bbRes2;
byte[] bbRes3;
SecureRandom randomNull = null;
SecureRandom random = new SecureRandom();
for (int i = 0; i < kAgs.length; i++) {
kAgs[i].init(privKey, dhPs);
kAgs[i].doPhase(publKey, true);
bbRes1 = kAgs[i].generateSecret();
kAgs[i].init(privKey, dhPs, random);
kAgs[i].doPhase(publKey, true);
bbRes2 = kAgs[i].generateSecret();
assertEquals("Incorrect byte array length", bbRes1.length, bbRes2.length);
for (int j = 0; j < bbRes1.length; j++) {
assertEquals("Incorrect byte (index: ".concat(Integer.toString(i)).concat(")"), bbRes1[j], bbRes2[j]);
}
kAgs[i].init(privKey, dhPs, randomNull);
kAgs[i].doPhase(publKey, true);
bbRes3 = kAgs[i].generateSecret();
assertEquals("Incorrect byte array length", bbRes1.length, bbRes3.length);
for (int j = 0; j < bbRes1.length; j++) {
assertEquals("Incorrect byte (index: ".concat(Integer.toString(i)).concat(")"), bbRes1[j], bbRes3[j]);
}
try {
kAgs[i].init(publKey, dhPs, random);
fail("InvalidKeyException expected");
} catch (InvalidKeyException e) {
//expected
}
try {
kAgs[i].init(privKey, aps, random);
fail("InvalidAlgorithmParameterException expected");
} catch (InvalidAlgorithmParameterException e) {
//expected
}
}
}
Aggregations