use of java.security.spec.DSAPublicKeySpec in project robovm by robovm.
the class DSAKeyFactoryImpl method engineTranslateKey.
/**
* The method generates a DSAPublicKey object from the provided key.
*
* @param
* key - a DSAPublicKey object or DSAPrivateKey object.
*
* @return
* object of the same type as the "key" argument
*
* @throws InvalidKeyException
* if "key" is neither DSAPublicKey nor DSAPrivateKey
*/
protected Key engineTranslateKey(Key key) throws InvalidKeyException {
if (key != null) {
if (key instanceof DSAPrivateKey) {
DSAPrivateKey privateKey = (DSAPrivateKey) key;
DSAParams params = privateKey.getParams();
try {
return engineGeneratePrivate(new DSAPrivateKeySpec(privateKey.getX(), params.getP(), params.getQ(), params.getG()));
} catch (InvalidKeySpecException e) {
// Actually this exception shouldn't be thrown
throw new InvalidKeyException("ATTENTION: InvalidKeySpecException: " + e);
}
}
if (key instanceof DSAPublicKey) {
DSAPublicKey publicKey = (DSAPublicKey) key;
DSAParams params = publicKey.getParams();
try {
return engineGeneratePublic(new DSAPublicKeySpec(publicKey.getY(), params.getP(), params.getQ(), params.getG()));
} catch (InvalidKeySpecException e) {
// Actually this exception shouldn't be thrown
throw new InvalidKeyException("ATTENTION: InvalidKeySpecException: " + e);
}
}
}
throw new InvalidKeyException("'key' is neither DSAPublicKey nor DSAPrivateKey");
}
use of java.security.spec.DSAPublicKeySpec in project Conversations by siacs.
the class OtrService method loadKey.
private KeyPair loadKey(final JSONObject keys) {
if (keys == null) {
return null;
}
synchronized (keys) {
try {
BigInteger x = new BigInteger(keys.getString("otr_x"), 16);
BigInteger y = new BigInteger(keys.getString("otr_y"), 16);
BigInteger p = new BigInteger(keys.getString("otr_p"), 16);
BigInteger q = new BigInteger(keys.getString("otr_q"), 16);
BigInteger g = new BigInteger(keys.getString("otr_g"), 16);
KeyFactory keyFactory = KeyFactory.getInstance("DSA");
DSAPublicKeySpec pubKeySpec = new DSAPublicKeySpec(y, p, q, g);
DSAPrivateKeySpec privateKeySpec = new DSAPrivateKeySpec(x, p, q, g);
PublicKey publicKey = keyFactory.generatePublic(pubKeySpec);
PrivateKey privateKey = keyFactory.generatePrivate(privateKeySpec);
return new KeyPair(publicKey, privateKey);
} catch (JSONException e) {
return null;
} catch (NoSuchAlgorithmException e) {
return null;
} catch (InvalidKeySpecException e) {
return null;
}
}
}
use of java.security.spec.DSAPublicKeySpec in project robovm by robovm.
the class KeyFactoryTest method testGeneratePrivate.
@SuppressWarnings("unchecked")
public void testGeneratePrivate() {
KeyFactory factory = null;
try {
factory = KeyFactory.getInstance(TEST_KEYFACTORY_NAME);
} catch (NoSuchAlgorithmException e) {
fail("unexpected exception: " + e);
}
assertNotNull(factory);
try {
TestPrivateKey key = new TestPrivateKey();
TestPrivateKeySpec keySpec = new TestPrivateKeySpec(key);
PrivateKey privateKey = factory.generatePrivate(keySpec);
assertNotNull(privateKey);
assertTrue(Arrays.equals(key.getEncoded(), privateKey.getEncoded()));
} catch (InvalidKeySpecException e) {
fail("unexpected exception: " + e);
}
KeySpec[] keySpecs = { new TestPublicKeySpec(new TestPublicKey()), null, new DSAPublicKeySpec(null, null, null, null) };
Class[] exceptions = { InvalidKeySpecException.class, NullPointerException.class, InvalidKeySpecException.class };
for (int i = 0; i < keySpecs.length; i++) {
KeySpec keySpec = keySpecs[i];
exceptionThrown = false;
String message = "generatePrivate(" + (keySpec == null ? "null" : keySpec.toString()) + ")";
try {
factory.generatePrivate(keySpec);
} catch (Exception e) {
checkException(message, e, exceptions[i]);
} finally {
checkException(message, null, exceptions[i]);
}
}
}
use of java.security.spec.DSAPublicKeySpec in project robovm by robovm.
the class DSAPublicKeySpecTest method testGetY.
/**
* Test for <code>getY</code> method
*/
public final void testGetY() {
DSAPublicKeySpec dpks = new DSAPublicKeySpec(// y
new BigInteger("1"), // p
new BigInteger("2"), // q
new BigInteger("3"), // g
new BigInteger("4"));
assertEquals(1, dpks.getY().intValue());
}
use of java.security.spec.DSAPublicKeySpec in project robovm by robovm.
the class DSAPublicKeySpecTest method testGetG.
/**
* Test for <code>getG</code> method
*/
public final void testGetG() {
DSAPublicKeySpec dpks = new DSAPublicKeySpec(// y
new BigInteger("1"), // p
new BigInteger("2"), // q
new BigInteger("3"), // g
new BigInteger("4"));
assertEquals(4, dpks.getG().intValue());
}
Aggregations