use of java.security.spec.DSAPublicKeySpec in project remote-desktop-clients by iiordanov.
the class PubkeyUtils method recoverKeyPair.
public static KeyPair recoverKeyPair(byte[] encoded) throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchProviderException {
KeySpec privKeySpec = new PKCS8EncodedKeySpec(encoded);
KeySpec pubKeySpec;
PrivateKey priv;
PublicKey pub;
KeyFactory kf;
try {
kf = KeyFactory.getInstance(PubkeyDatabase.KEY_TYPE_RSA, "BC");
priv = kf.generatePrivate(privKeySpec);
pubKeySpec = new RSAPublicKeySpec(((RSAPrivateCrtKey) priv).getModulus(), ((RSAPrivateCrtKey) priv).getPublicExponent());
pub = kf.generatePublic(pubKeySpec);
} catch (ClassCastException e) {
kf = KeyFactory.getInstance(PubkeyDatabase.KEY_TYPE_DSA, "BC");
priv = kf.generatePrivate(privKeySpec);
DSAParams params = ((DSAPrivateKey) priv).getParams();
// Calculate public key Y
BigInteger y = params.getG().modPow(((DSAPrivateKey) priv).getX(), params.getP());
pubKeySpec = new DSAPublicKeySpec(y, params.getP(), params.getQ(), params.getG());
pub = kf.generatePublic(pubKeySpec);
}
return new KeyPair(pub, priv);
}
use of java.security.spec.DSAPublicKeySpec in project keystore-explorer by kaikramer.
the class KeyPairUtil method getKeyInfo.
/**
* Get the information about the supplied public key.
*
* @param publicKey
* The public key
* @return Key information
* @throws CryptoException
* If there is a problem getting the information
*/
public static KeyInfo getKeyInfo(PublicKey publicKey) throws CryptoException {
try {
String algorithm = publicKey.getAlgorithm();
if (algorithm.equals(RSA.jce())) {
KeyFactory keyFact = KeyFactory.getInstance(algorithm, BOUNCY_CASTLE.jce());
RSAPublicKeySpec keySpec = keyFact.getKeySpec(publicKey, RSAPublicKeySpec.class);
BigInteger modulus = keySpec.getModulus();
return new KeyInfo(ASYMMETRIC, algorithm, modulus.toString(2).length());
} else if (algorithm.equals(DSA.jce())) {
KeyFactory keyFact = KeyFactory.getInstance(algorithm);
DSAPublicKeySpec keySpec = keyFact.getKeySpec(publicKey, DSAPublicKeySpec.class);
BigInteger prime = keySpec.getP();
return new KeyInfo(ASYMMETRIC, algorithm, prime.toString(2).length());
} else if (algorithm.equals(EC.jce())) {
ECPublicKey pubk = (ECPublicKey) publicKey;
int size = pubk.getParams().getOrder().bitLength();
return new KeyInfo(ASYMMETRIC, algorithm, size);
}
// size unknown
return new KeyInfo(ASYMMETRIC, algorithm);
} catch (GeneralSecurityException ex) {
throw new CryptoException(res.getString("NoPublicKeysize.exception.message"), ex);
}
}
use of java.security.spec.DSAPublicKeySpec in project j2objc by google.
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());
}
use of java.security.spec.DSAPublicKeySpec in project j2objc by google.
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 j2objc by google.
the class DSAPublicKeySpecTest method testGetP.
/**
* Test for <code>getP</code> method
*/
public final void testGetP() {
DSAPublicKeySpec dpks = new DSAPublicKeySpec(// y
new BigInteger("1"), // p
new BigInteger("2"), // q
new BigInteger("3"), // g
new BigInteger("4"));
assertEquals(2, dpks.getP().intValue());
}
Aggregations