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);
}
}
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;
}
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));
}
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);
}
}
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);
}
}
Aggregations