Search in sources :

Example 11 with DSAPrivateKey

use of java.security.interfaces.DSAPrivateKey in project keystore-explorer by kaikramer.

the class OpenSslPvkUtil method get.

/**
 * OpenSSL encode a private key.
 *
 * @return The encoding
 * @param privateKey
 *            The private key
 * @throws CryptoException
 *             Problem encountered while getting the encoded private key
 */
public static byte[] get(PrivateKey privateKey) throws CryptoException {
    // DER encoding for each key type is a sequence
    ASN1EncodableVector vec = new ASN1EncodableVector();
    if (privateKey instanceof ECPrivateKey) {
        try {
            ECPrivateKey ecPrivKey = (ECPrivateKey) privateKey;
            org.bouncycastle.asn1.sec.ECPrivateKey keyStructure = EccUtil.convertToECPrivateKeyStructure(ecPrivKey);
            return keyStructure.toASN1Primitive().getEncoded();
        } catch (IOException e) {
            throw new CryptoException(res.getString("NoDerEncodeOpenSslPrivateKey.exception.message"), e);
        }
    } else if (privateKey instanceof RSAPrivateCrtKey) {
        RSAPrivateCrtKey rsaPrivateKey = (RSAPrivateCrtKey) privateKey;
        vec.add(new ASN1Integer(VERSION));
        vec.add(new ASN1Integer(rsaPrivateKey.getModulus()));
        vec.add(new ASN1Integer(rsaPrivateKey.getPublicExponent()));
        vec.add(new ASN1Integer(rsaPrivateKey.getPrivateExponent()));
        vec.add(new ASN1Integer(rsaPrivateKey.getPrimeP()));
        vec.add(new ASN1Integer(rsaPrivateKey.getPrimeQ()));
        vec.add(new ASN1Integer(rsaPrivateKey.getPrimeExponentP()));
        vec.add(new ASN1Integer(rsaPrivateKey.getPrimeExponentQ()));
        vec.add(new ASN1Integer(rsaPrivateKey.getCrtCoefficient()));
    } else {
        DSAPrivateKey dsaPrivateKey = (DSAPrivateKey) privateKey;
        DSAParams dsaParams = dsaPrivateKey.getParams();
        BigInteger primeModulusP = dsaParams.getP();
        BigInteger primeQ = dsaParams.getQ();
        BigInteger generatorG = dsaParams.getG();
        BigInteger secretExponentX = dsaPrivateKey.getX();
        // Derive public key from private key parts, ie Y = G^X mod P
        BigInteger publicExponentY = generatorG.modPow(secretExponentX, primeModulusP);
        vec.add(new ASN1Integer(VERSION));
        vec.add(new ASN1Integer(primeModulusP));
        vec.add(new ASN1Integer(primeQ));
        vec.add(new ASN1Integer(generatorG));
        vec.add(new ASN1Integer(publicExponentY));
        vec.add(new ASN1Integer(secretExponentX));
    }
    DERSequence derSequence = new DERSequence(vec);
    try {
        return derSequence.getEncoded();
    } catch (IOException ex) {
        throw new CryptoException(res.getString("NoDerEncodeOpenSslPrivateKey.exception.message"), ex);
    }
}
Also used : ECPrivateKey(java.security.interfaces.ECPrivateKey) RSAPrivateCrtKey(java.security.interfaces.RSAPrivateCrtKey) IOException(java.io.IOException) ASN1Integer(org.bouncycastle.asn1.ASN1Integer) DSAParams(java.security.interfaces.DSAParams) DERSequence(org.bouncycastle.asn1.DERSequence) ASN1EncodableVector(org.bouncycastle.asn1.ASN1EncodableVector) DSAPrivateKey(java.security.interfaces.DSAPrivateKey) BigInteger(java.math.BigInteger) CryptoException(org.kse.crypto.CryptoException)

Example 12 with DSAPrivateKey

use of java.security.interfaces.DSAPrivateKey in project protools by SeanDragon.

the class ToolDSA method initKey.

/**
 * 生成密钥
 *
 * @return 密钥对象
 *
 * @throws Exception
 */
public static Map<String, Object> initKey() throws NoSuchAlgorithmException {
    // 初始化密钥对儿生成器
    KeyPairGenerator keygen = KeyPairGenerator.getInstance(ALGORITHM);
    // 实例化密钥对儿生成器
    keygen.initialize(KEY_SIZE, new SecureRandom());
    // 实例化密钥对儿
    KeyPair keys = keygen.genKeyPair();
    DSAPublicKey publicKey = (DSAPublicKey) keys.getPublic();
    DSAPrivateKey privateKey = (DSAPrivateKey) keys.getPrivate();
    // 封装密钥
    Map<String, Object> map = Maps.newHashMapWithExpectedSize(2);
    map.put(PUBLIC_KEY, publicKey);
    map.put(PRIVATE_KEY, privateKey);
    return map;
}
Also used : KeyPair(java.security.KeyPair) SecureRandom(java.security.SecureRandom) DSAPrivateKey(java.security.interfaces.DSAPrivateKey) KeyPairGenerator(java.security.KeyPairGenerator) DSAPublicKey(java.security.interfaces.DSAPublicKey)

Example 13 with DSAPrivateKey

use of java.security.interfaces.DSAPrivateKey in project xipki by xipki.

the class P12KeyGenerator method getContentSigner.

// method generateIdentity
private static ContentSigner getContentSigner(PrivateKey key) throws Exception {
    BcContentSignerBuilder builder;
    if (key instanceof RSAPrivateKey) {
        ASN1ObjectIdentifier hashOid = X509ObjectIdentifiers.id_SHA1;
        ASN1ObjectIdentifier sigOid = PKCSObjectIdentifiers.sha1WithRSAEncryption;
        builder = new BcRSAContentSignerBuilder(buildAlgId(sigOid), buildAlgId(hashOid));
    } else if (key instanceof DSAPrivateKey) {
        ASN1ObjectIdentifier hashOid = X509ObjectIdentifiers.id_SHA1;
        AlgorithmIdentifier sigId = new AlgorithmIdentifier(X9ObjectIdentifiers.id_dsa_with_sha1);
        builder = new BcDSAContentSignerBuilder(sigId, buildAlgId(hashOid));
    } else if (key instanceof ECPrivateKey) {
        HashAlgo hashAlgo;
        ASN1ObjectIdentifier sigOid;
        int keysize = ((ECPrivateKey) key).getParams().getOrder().bitLength();
        if (keysize > 384) {
            hashAlgo = HashAlgo.SHA512;
            sigOid = X9ObjectIdentifiers.ecdsa_with_SHA512;
        } else if (keysize > 256) {
            hashAlgo = HashAlgo.SHA384;
            sigOid = X9ObjectIdentifiers.ecdsa_with_SHA384;
        } else if (keysize > 224) {
            hashAlgo = HashAlgo.SHA224;
            sigOid = X9ObjectIdentifiers.ecdsa_with_SHA224;
        } else if (keysize > 160) {
            hashAlgo = HashAlgo.SHA256;
            sigOid = X9ObjectIdentifiers.ecdsa_with_SHA256;
        } else {
            hashAlgo = HashAlgo.SHA1;
            sigOid = X9ObjectIdentifiers.ecdsa_with_SHA1;
        }
        builder = new BcECContentSignerBuilder(new AlgorithmIdentifier(sigOid), buildAlgId(hashAlgo.getOid()));
    } else {
        throw new IllegalArgumentException("unknown type of key " + key.getClass().getName());
    }
    return builder.build(KeyUtil.generatePrivateKeyParameter(key));
}
Also used : BcRSAContentSignerBuilder(org.bouncycastle.operator.bc.BcRSAContentSignerBuilder) ECPrivateKey(java.security.interfaces.ECPrivateKey) HashAlgo(org.xipki.security.HashAlgo) DSAPrivateKey(java.security.interfaces.DSAPrivateKey) BcDSAContentSignerBuilder(org.bouncycastle.operator.bc.BcDSAContentSignerBuilder) BcECContentSignerBuilder(org.bouncycastle.operator.bc.BcECContentSignerBuilder) BcContentSignerBuilder(org.bouncycastle.operator.bc.BcContentSignerBuilder) RSAPrivateKey(java.security.interfaces.RSAPrivateKey) ASN1ObjectIdentifier(org.bouncycastle.asn1.ASN1ObjectIdentifier) AlgorithmIdentifier(org.bouncycastle.asn1.x509.AlgorithmIdentifier)

Example 14 with DSAPrivateKey

use of java.security.interfaces.DSAPrivateKey in project keystore-explorer by kaikramer.

the class DViewAsymmetricKeyFields method populateFields.

private void populateFields() {
    Field[] fields = null;
    if (key instanceof RSAPublicKey) {
        RSAPublicKey rsaPub = (RSAPublicKey) key;
        fields = new Field[] { new Field(res.getString("DViewAsymmetricKeyFields.jltFields.PubRsaPublicExponent.text"), rsaPub.getPublicExponent()), new Field(res.getString("DViewAsymmetricKeyFields.jltFields.PubRsaModulus.text"), rsaPub.getModulus()) };
    } else if (key instanceof DSAPublicKey) {
        DSAPublicKey dsaPub = (DSAPublicKey) key;
        DSAParams dsaParams = dsaPub.getParams();
        fields = new Field[] { new Field(res.getString("DViewAsymmetricKeyFields.jltFields.PubDsaPrimeModulusP.text"), dsaParams.getP()), new Field(res.getString("DViewAsymmetricKeyFields.jltFields.PubDsaPrimeQ.text"), dsaParams.getQ()), new Field(res.getString("DViewAsymmetricKeyFields.jltFields.PubDsaGeneratorG.text"), dsaParams.getG()), new Field(res.getString("DViewAsymmetricKeyFields.jltFields.PubDsaPublicKeyY.text"), dsaPub.getY()) };
    } else if (key instanceof RSAPrivateCrtKey) {
        RSAPrivateCrtKey rsaPvk = (RSAPrivateCrtKey) key;
        fields = new Field[] { new Field(res.getString("DViewAsymmetricKeyFields.jltFields.PrivRsaPublicExponent.text"), rsaPvk.getPublicExponent()), new Field(res.getString("DViewAsymmetricKeyFields.jltFields.PrivRsaModulus.text"), rsaPvk.getModulus()), new Field(res.getString("DViewAsymmetricKeyFields.jltFields.PrivRsaPrimeP.text"), rsaPvk.getPrimeP()), new Field(res.getString("DViewAsymmetricKeyFields.jltFields.PrivRsaPrimeQ.text"), rsaPvk.getPrimeQ()), new Field(res.getString("DViewAsymmetricKeyFields.jltFields.PrivRsaPrimeExponentP.text"), rsaPvk.getPrimeExponentP()), new Field(res.getString("DViewAsymmetricKeyFields.jltFields.PrivRsaPrimeExponentQ.text"), rsaPvk.getPrimeExponentQ()), new Field(res.getString("DViewAsymmetricKeyFields.jltFields.PrivRsaCrtCoefficient.text"), rsaPvk.getCrtCoefficient()), new Field(res.getString("DViewAsymmetricKeyFields.jltFields.PrivRsaPrivateExponent.text"), rsaPvk.getPrivateExponent()) };
    } else if (key instanceof RSAPrivateKey) {
        RSAPrivateKey rsaPvk = (RSAPrivateKey) key;
        fields = new Field[] { new Field(res.getString("DViewAsymmetricKeyFields.jltFields.PrivRsaModulus.text"), rsaPvk.getModulus()), new Field(res.getString("DViewAsymmetricKeyFields.jltFields.PrivRsaPrivateExponent.text"), rsaPvk.getPrivateExponent()) };
    } else if (key instanceof DSAPrivateKey) {
        DSAPrivateKey dsaPvk = (DSAPrivateKey) key;
        DSAParams dsaParams = dsaPvk.getParams();
        fields = new Field[] { new Field(res.getString("DViewAsymmetricKeyFields.jltFields.PrivDsaPrimeModulusP.text"), dsaParams.getP()), new Field(res.getString("DViewAsymmetricKeyFields.jltFields.PrivDsaPrimeQ.text"), dsaParams.getQ()), new Field(res.getString("DViewAsymmetricKeyFields.jltFields.PrivDsaGeneratorG.text"), dsaParams.getG()), new Field(res.getString("DViewAsymmetricKeyFields.jltFields.PrivDsaSecretExponentX.text"), dsaPvk.getX()) };
    }
    if (fields != null) {
        jltFields.setListData(fields);
        jltFields.setSelectedIndex(0);
    }
}
Also used : RSAPrivateCrtKey(java.security.interfaces.RSAPrivateCrtKey) RSAPublicKey(java.security.interfaces.RSAPublicKey) DSAPrivateKey(java.security.interfaces.DSAPrivateKey) DSAParams(java.security.interfaces.DSAParams) RSAPrivateKey(java.security.interfaces.RSAPrivateKey) DSAPublicKey(java.security.interfaces.DSAPublicKey)

Example 15 with DSAPrivateKey

use of java.security.interfaces.DSAPrivateKey in project keystore-explorer by kaikramer.

the class DViewPrivateKey method fieldsPressed.

private void fieldsPressed() {
    if (privateKey instanceof RSAPrivateKey) {
        RSAPrivateKey rsaPvk = (RSAPrivateKey) privateKey;
        DViewAsymmetricKeyFields dViewAsymmetricKeyFields = new DViewAsymmetricKeyFields(this, res.getString("DViewPrivateKey.RsaFields.Title"), rsaPvk);
        dViewAsymmetricKeyFields.setLocationRelativeTo(this);
        dViewAsymmetricKeyFields.setVisible(true);
    } else if (privateKey instanceof DSAPrivateKey) {
        DSAPrivateKey dsaPvk = (DSAPrivateKey) privateKey;
        DViewAsymmetricKeyFields dViewAsymmetricKeyFields = new DViewAsymmetricKeyFields(this, res.getString("DViewPrivateKey.DsaFields.Title"), dsaPvk);
        dViewAsymmetricKeyFields.setLocationRelativeTo(this);
        dViewAsymmetricKeyFields.setVisible(true);
    }
}
Also used : DSAPrivateKey(java.security.interfaces.DSAPrivateKey) RSAPrivateKey(java.security.interfaces.RSAPrivateKey)

Aggregations

DSAPrivateKey (java.security.interfaces.DSAPrivateKey)48 BigInteger (java.math.BigInteger)23 DSAParams (java.security.interfaces.DSAParams)18 DSAPublicKey (java.security.interfaces.DSAPublicKey)15 RSAPrivateKey (java.security.interfaces.RSAPrivateKey)14 KeyPair (java.security.KeyPair)12 RSAPrivateCrtKey (java.security.interfaces.RSAPrivateCrtKey)12 DSAPrivateKeySpec (java.security.spec.DSAPrivateKeySpec)11 IOException (java.io.IOException)10 KeyPairGenerator (java.security.KeyPairGenerator)8 DSAPublicKeySpec (java.security.spec.DSAPublicKeySpec)8 InvalidKeyException (java.security.InvalidKeyException)7 ECPrivateKey (java.security.interfaces.ECPrivateKey)7 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)7 Signature (java.security.Signature)6 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)6 X509EncodedKeySpec (java.security.spec.X509EncodedKeySpec)6 PrivateKey (java.security.PrivateKey)5 SlowTest (com.google.security.wycheproof.WycheproofRunner.SlowTest)4 Test (org.junit.Test)4