use of java.security.interfaces.DSAPrivateKey in project churchkey by tomitribe.
the class BeginPrivateKey method decodeDsaKey.
private static Key decodeDsaKey(final byte[] bytes) throws IOException {
final Dsa.Private.Builder dsa = Dsa.Private.builder();
final DerParser d1 = new DerParser(bytes);
final Asn1Object d1o1 = d1.readObject().assertType(Asn1Type.SEQUENCE);
{
final DerParser d2 = new DerParser(d1o1.getValue());
final Asn1Object d2o1 = d2.readObject().assertType(Asn1Type.INTEGER);
final Asn1Object d2o2 = d2.readObject().assertType(Asn1Type.SEQUENCE);
{
final DerParser d3 = new DerParser(d2o2.getValue());
final Asn1Object d3o1 = d3.readObject().assertType(Asn1Type.OBJECT_IDENTIFIER);
final Asn1Object d3o2 = d3.readObject().assertType(Asn1Type.SEQUENCE);
{
final DerParser d4 = new DerParser(d3o2.getValue());
dsa.p(d4.readBigInteger());
dsa.q(d4.readBigInteger());
dsa.g(d4.readBigInteger());
}
}
final Asn1Object d2o3 = d2.readObject().assertType(Asn1Type.OCTET_STRING);
{
final DerParser d3 = new DerParser(d2o3.getValue());
dsa.x(d3.readBigInteger());
final Dsa.Private build = dsa.build();
final DSAPrivateKey privateKey = build.toKey();
final DSAPublicKey publicKey = build.toPublic().toKey();
return new Key(privateKey, publicKey, Key.Type.PRIVATE, DSA, Key.Format.PEM);
}
}
}
use of java.security.interfaces.DSAPrivateKey in project churchkey by tomitribe.
the class OpenSSHPrivateKey method writePrivateDssKey.
private static byte[] writePrivateDssKey(final Key key, final KeyOutput out) throws IOException {
final DSAPublicKey publicKey = (DSAPublicKey) key.getPublicKey().getKey();
final DSAPrivateKey privateKey = (DSAPrivateKey) key.getKey();
out.writeBigInteger(privateKey.getParams().getP());
out.writeBigInteger(privateKey.getParams().getQ());
out.writeBigInteger(privateKey.getParams().getG());
out.writeBigInteger(publicKey.getY());
out.writeBigInteger(privateKey.getX());
out.writeString(getComment(key));
return out.toByteArray();
}
use of java.security.interfaces.DSAPrivateKey in project churchkey by tomitribe.
the class Keys method of.
/**
* Creates a {@link Key} instance that encompasses both the public and private keys. If this Key
* instance is exported via {@link Key#encode(Key.Format)}} the resulting file will be a private key
* file that includes both the public and private key data.
*
* The key Algorithm (RSA, DSA, EC) will be discovered automatically. The Format will default
* to PEM. The key Type will be set as PRIVATE. The corresponding public key can be obtained
* via {@link Key#getPublicKey()}
*
* This method is largely a convenience method for formatting {@link java.security.KeyPair} instances.
* For example:
* <code>
* final KeyPairGenerator generator = KeyPairGenerator.getInstance("EC");
* final KeyPair pair = generator.generateKeyPair();
*
* final byte[] openssh = Keys.of(pair).format(OPENSSH);
* </code>
*/
public static Key of(final KeyPair pair) {
Objects.requireNonNull(pair);
final PrivateKey key = pair.getPrivate();
if (key instanceof DSAPrivateKey) {
return new Key(pair.getPrivate(), pair.getPublic(), Key.Type.PRIVATE, Key.Algorithm.DSA, Key.Format.PEM);
}
if (key instanceof ECPrivateKey) {
return new Key(pair.getPrivate(), pair.getPublic(), Key.Type.PRIVATE, Key.Algorithm.EC, Key.Format.PEM);
}
if (key instanceof RSAPrivateCrtKey) {
return new Key(pair.getPrivate(), pair.getPublic(), Key.Type.PRIVATE, Key.Algorithm.RSA, Key.Format.PEM);
}
if (key instanceof RSAPrivateKey) {
return new Key(pair.getPrivate(), pair.getPublic(), Key.Type.PRIVATE, Key.Algorithm.RSA, Key.Format.PEM);
}
throw new UnsupportedOperationException("Unsupported key type: " + key.getClass().getName());
}
use of java.security.interfaces.DSAPrivateKey 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, new org.bouncycastle.jce.provider.BouncyCastleProvider());
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, new org.bouncycastle.jce.provider.BouncyCastleProvider());
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.interfaces.DSAPrivateKey in project YAPION by yoyosource.
the class DSAKeySpecSerializer method deserializePrivateKey.
@Override
public DSAPrivateKey deserializePrivateKey(DeserializeData<YAPIONObject> deserializeData, String algorithm) throws GeneralSecurityException {
BigInteger x = deserializeData.object.getValue("x", BigInteger.class).get();
BigInteger p = deserializeData.object.getValue("p", BigInteger.class).get();
BigInteger q = deserializeData.object.getValue("q", BigInteger.class).get();
BigInteger g = deserializeData.object.getValue("g", BigInteger.class).get();
KeyFactory keyFactory = KeyFactory.getInstance(algorithm);
return (DSAPrivateKey) keyFactory.generatePrivate(new DSAPrivateKeySpec(x, p, q, g));
}
Aggregations