Search in sources :

Example 71 with DSAPrivateKey

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);
        }
    }
}
Also used : Dsa(io.churchkey.dsa.Dsa) DSAPrivateKey(java.security.interfaces.DSAPrivateKey) DerParser(io.churchkey.asn1.DerParser) DSAPrivateKey(java.security.interfaces.DSAPrivateKey) Key(io.churchkey.Key) RSAPublicKey(java.security.interfaces.RSAPublicKey) ECPrivateKey(java.security.interfaces.ECPrivateKey) DSAPublicKey(java.security.interfaces.DSAPublicKey) RSAPrivateCrtKey(java.security.interfaces.RSAPrivateCrtKey) ECPublicKey(java.security.interfaces.ECPublicKey) Asn1Object(io.churchkey.asn1.Asn1Object) DSAPublicKey(java.security.interfaces.DSAPublicKey)

Example 72 with DSAPrivateKey

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();
}
Also used : DSAPrivateKey(java.security.interfaces.DSAPrivateKey) DSAPublicKey(java.security.interfaces.DSAPublicKey)

Example 73 with DSAPrivateKey

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());
}
Also used : ECPrivateKey(java.security.interfaces.ECPrivateKey) DSAPrivateKey(java.security.interfaces.DSAPrivateKey) RSAPrivateKey(java.security.interfaces.RSAPrivateKey) PrivateKey(java.security.PrivateKey) ECPrivateKey(java.security.interfaces.ECPrivateKey) RSAPrivateCrtKey(java.security.interfaces.RSAPrivateCrtKey) DSAPrivateKey(java.security.interfaces.DSAPrivateKey) RSAPrivateKey(java.security.interfaces.RSAPrivateKey) DSAPrivateKey(java.security.interfaces.DSAPrivateKey) RSAPrivateKey(java.security.interfaces.RSAPrivateKey) DSAPublicKey(java.security.interfaces.DSAPublicKey) RSAPublicKey(java.security.interfaces.RSAPublicKey) PrivateKey(java.security.PrivateKey) ECPrivateKey(java.security.interfaces.ECPrivateKey) RSAPrivateCrtKey(java.security.interfaces.RSAPrivateCrtKey) ECPublicKey(java.security.interfaces.ECPublicKey)

Example 74 with DSAPrivateKey

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);
}
Also used : KeyPair(java.security.KeyPair) DSAPrivateKey(java.security.interfaces.DSAPrivateKey) PrivateKey(java.security.PrivateKey) RSAPrivateCrtKey(java.security.interfaces.RSAPrivateCrtKey) RSAPublicKey(java.security.interfaces.RSAPublicKey) PublicKey(java.security.PublicKey) DSAPublicKey(java.security.interfaces.DSAPublicKey) SecretKeySpec(javax.crypto.spec.SecretKeySpec) KeySpec(java.security.spec.KeySpec) PBEKeySpec(javax.crypto.spec.PBEKeySpec) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) DSAPublicKeySpec(java.security.spec.DSAPublicKeySpec) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) DSAParams(java.security.interfaces.DSAParams) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) BigInteger(java.math.BigInteger) DSAPrivateKey(java.security.interfaces.DSAPrivateKey) SecretKeyFactory(javax.crypto.SecretKeyFactory) KeyFactory(java.security.KeyFactory) DSAPublicKeySpec(java.security.spec.DSAPublicKeySpec)

Example 75 with DSAPrivateKey

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));
}
Also used : DSAPrivateKeySpec(java.security.spec.DSAPrivateKeySpec) BigInteger(java.math.BigInteger) DSAPrivateKey(java.security.interfaces.DSAPrivateKey) KeyFactory(java.security.KeyFactory)

Aggregations

DSAPrivateKey (java.security.interfaces.DSAPrivateKey)86 BigInteger (java.math.BigInteger)35 DSAPublicKey (java.security.interfaces.DSAPublicKey)31 DSAParams (java.security.interfaces.DSAParams)26 RSAPrivateCrtKey (java.security.interfaces.RSAPrivateCrtKey)25 RSAPrivateKey (java.security.interfaces.RSAPrivateKey)25 ECPrivateKey (java.security.interfaces.ECPrivateKey)23 IOException (java.io.IOException)18 KeyPair (java.security.KeyPair)18 DSAPrivateKeySpec (java.security.spec.DSAPrivateKeySpec)15 PrivateKey (java.security.PrivateKey)14 DSAPublicKeySpec (java.security.spec.DSAPublicKeySpec)14 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)13 KeyFactory (java.security.KeyFactory)12 RSAPublicKey (java.security.interfaces.RSAPublicKey)12 PublicKey (java.security.PublicKey)11 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)10 InvalidKeyException (java.security.InvalidKeyException)9 KeyPairGenerator (java.security.KeyPairGenerator)9 ECPublicKey (java.security.interfaces.ECPublicKey)9