use of java.security.interfaces.DSAParams in project keystore-explorer by kaikramer.
the class MsPvkUtil method dsaPrivateKeyToBlob.
private static byte[] dsaPrivateKeyToBlob(DSAPrivateKey dsaPrivKey) throws CryptoException {
try {
DSAParams dsaParams = dsaPrivKey.getParams();
// 328 sufficient for a 1024 bit DSA key
ByteBuffer bb = ByteBuffer.wrap(new byte[512]);
bb.order(ByteOrder.LITTLE_ENDIAN);
// Write out the blob fields
// dsspubkey.magic
UnsignedUtil.putInt(bb, DSS_PRIV_MAGIC);
BigInteger prime = dsaParams.getP();
int bitLength = prime.toString(2).length();
// dsspubkey.bitlen
UnsignedUtil.putInt(bb, bitLength);
/*
* Unlike RSA there are no bit length remainders (ie DSA bit length
* always divisible by 8 as they are multiples of 64)
*/
// modulus
writeBigInteger(bb, dsaParams.getP(), (bitLength / 8));
// prime
writeBigInteger(bb, dsaParams.getQ(), 20);
// generator
writeBigInteger(bb, dsaParams.getG(), (bitLength / 8));
// secret exponent
writeBigInteger(bb, dsaPrivKey.getX(), 20);
// dssseed.counter - none, fill 0xff
UnsignedUtil.putInt(bb, 0xffffffff);
for (// dssseed.seed - none, fill 0xff
int i = 0; // dssseed.seed - none, fill 0xff
i < 20; // dssseed.seed - none, fill 0xff
i++) {
bb.put((byte) 0xff);
}
return getBufferBytes(bb);
} catch (IOException ex) {
throw new CryptoException(res.getString("NoConvertKeyToBlob.exception.message"), ex);
}
}
use of java.security.interfaces.DSAParams in project keystore-explorer by kaikramer.
the class Spkac method createPublicKeyAndChallenge.
private ASN1Sequence createPublicKeyAndChallenge() throws SpkacException {
ASN1EncodableVector publicKeyAlgorithm = new ASN1EncodableVector();
publicKeyAlgorithm.add(new ASN1ObjectIdentifier(getPublicKeyAlg().oid()));
if (getPublicKey() instanceof RSAPublicKey) {
publicKeyAlgorithm.add(DERNull.INSTANCE);
} else {
DSAParams dsaParams = ((DSAPublicKey) getPublicKey()).getParams();
ASN1EncodableVector dssParams = new ASN1EncodableVector();
dssParams.add(new ASN1Integer(dsaParams.getP()));
dssParams.add(new ASN1Integer(dsaParams.getQ()));
dssParams.add(new ASN1Integer(dsaParams.getG()));
publicKeyAlgorithm.add(new DERSequence(dssParams));
}
ASN1EncodableVector spki = new ASN1EncodableVector();
spki.add(new DERSequence(publicKeyAlgorithm));
spki.add(encodePublicKeyAsBitString(getPublicKey()));
ASN1EncodableVector publicKeyAndChallenge = new ASN1EncodableVector();
publicKeyAndChallenge.add(new DERSequence(spki));
publicKeyAndChallenge.add(new DERIA5String(getChallenge()));
return new DERSequence(publicKeyAndChallenge);
}
use of java.security.interfaces.DSAParams in project keystore-explorer by kaikramer.
the class DProperties method createPrivateKeyNodes.
private void createPrivateKeyNodes(DefaultMutableTreeNode parentNode, PrivateKey privateKey) throws CryptoException {
DefaultMutableTreeNode privateKeyNode = new DefaultMutableTreeNode(res.getString("DProperties.properties.PrivateKey"));
parentNode.add(privateKeyNode);
currentState.getKeyStore();
KeyInfo keyInfo = KeyPairUtil.getKeyInfo(privateKey);
String keyAlg = keyInfo.getAlgorithm();
privateKeyNode.add(new DefaultMutableTreeNode(MessageFormat.format(res.getString("DProperties.properties.Algorithm"), keyAlg)));
Integer keySize = keyInfo.getSize();
if (keySize != null) {
privateKeyNode.add(new DefaultMutableTreeNode(MessageFormat.format(res.getString("DProperties.properties.KeySize"), "" + keyInfo.getSize())));
} else {
privateKeyNode.add(new DefaultMutableTreeNode(MessageFormat.format(res.getString("DProperties.properties.KeySize"), "?")));
}
String keyFormat = privateKey.getFormat();
privateKeyNode.add(new DefaultMutableTreeNode(MessageFormat.format(res.getString("DProperties.properties.Format"), keyFormat)));
String keyEncoded;
byte[] encodedKey = privateKey.getEncoded();
if (encodedKey != null) {
keyEncoded = "0x" + new BigInteger(1, privateKey.getEncoded()).toString(16).toUpperCase();
} else {
keyEncoded = "*****";
}
privateKeyNode.add(new DefaultMutableTreeNode(MessageFormat.format(res.getString("DProperties.properties.Encoded"), keyEncoded)));
if (privateKey instanceof RSAPrivateCrtKey) {
RSAPrivateCrtKey rsaPrivateKey = (RSAPrivateCrtKey) privateKey;
String publicExponent = MessageFormat.format(res.getString("DProperties.properties.private.rsa.PublicExponent"), "0x" + rsaPrivateKey.getPublicExponent().toString(16).toUpperCase());
privateKeyNode.add(new DefaultMutableTreeNode(publicExponent));
String modulus = MessageFormat.format(res.getString("DProperties.properties.private.rsa.Modulus"), "0x" + rsaPrivateKey.getModulus().toString(16).toUpperCase());
privateKeyNode.add(new DefaultMutableTreeNode(modulus));
String primeP = MessageFormat.format(res.getString("DProperties.properties.private.rsa.PrimeP"), "0x" + rsaPrivateKey.getPrimeP().toString(16).toUpperCase());
privateKeyNode.add(new DefaultMutableTreeNode(primeP));
String primeQ = MessageFormat.format(res.getString("DProperties.properties.private.rsa.PrimeQ"), "0x" + rsaPrivateKey.getPrimeQ().toString(16).toUpperCase());
privateKeyNode.add(new DefaultMutableTreeNode(primeQ));
String primeExponentP = MessageFormat.format(res.getString("DProperties.properties.private.rsa.PrimeExponentP"), "0x" + rsaPrivateKey.getPrimeExponentP().toString(16).toUpperCase());
privateKeyNode.add(new DefaultMutableTreeNode(primeExponentP));
String primeExponentQ = MessageFormat.format(res.getString("DProperties.properties.private.rsa.PrimeExponentQ"), "0x" + rsaPrivateKey.getPrimeExponentQ().toString(16).toUpperCase());
privateKeyNode.add(new DefaultMutableTreeNode(primeExponentQ));
String crtCoefficient = MessageFormat.format(res.getString("DProperties.properties.private.rsa.CrtCoefficient"), "0x" + rsaPrivateKey.getCrtCoefficient().toString(16).toUpperCase());
privateKeyNode.add(new DefaultMutableTreeNode(crtCoefficient));
String privateExponent = MessageFormat.format(res.getString("DProperties.properties.private.rsa.PrivateExponent"), "0x" + rsaPrivateKey.getPrivateExponent().toString(16).toUpperCase());
privateKeyNode.add(new DefaultMutableTreeNode(privateExponent));
} else if (privateKey instanceof DSAPrivateKey) {
DSAPrivateKey dsaPrivateKey = (DSAPrivateKey) privateKey;
DSAParams dsaParams = dsaPrivateKey.getParams();
String primeModulusP = MessageFormat.format(res.getString("DProperties.properties.private.dsa.PrimeModulusP"), "0x" + dsaParams.getP().toString(16).toUpperCase());
privateKeyNode.add(new DefaultMutableTreeNode(primeModulusP));
String primeQ = MessageFormat.format(res.getString("DProperties.properties.private.dsa.PrimeQ"), "0x" + dsaParams.getQ().toString(16).toUpperCase());
privateKeyNode.add(new DefaultMutableTreeNode(primeQ));
String generatorG = MessageFormat.format(res.getString("DProperties.properties.private.dsa.GeneratorG"), "0x" + dsaParams.getG().toString(16).toUpperCase());
privateKeyNode.add(new DefaultMutableTreeNode(generatorG));
String secretExponentX = MessageFormat.format(res.getString("DProperties.properties.private.dsa.SecretExponentX"), "0x" + dsaPrivateKey.getX().toString(16).toUpperCase());
privateKeyNode.add(new DefaultMutableTreeNode(secretExponentX));
}
}
use of java.security.interfaces.DSAParams in project j2objc by google.
the class DSAParamsTest method test_getP.
/**
* java.security.interfaces.DSAParams
* #getP()
*/
public void test_getP() {
DSAParams params = new DSAParameterSpec(p, q, g);
assertEquals("Invalid P", p, params.getP());
}
use of java.security.interfaces.DSAParams in project jruby-openssl by jruby.
the class PKey method toDerDSAKey.
public static byte[] toDerDSAKey(DSAPublicKey pubKey, DSAPrivateKey privKey) throws IOException {
if (pubKey != null && privKey == null) {
return pubKey.getEncoded();
}
if (privKey != null && pubKey != null) {
ASN1EncodableVector vec = new ASN1EncodableVector();
final DSAParams params = privKey.getParams();
vec.add(new ASN1Integer(BigInteger.ZERO));
vec.add(new ASN1Integer(params.getP()));
vec.add(new ASN1Integer(params.getQ()));
vec.add(new ASN1Integer(params.getG()));
vec.add(new ASN1Integer(pubKey.getY()));
vec.add(new ASN1Integer(privKey.getX()));
return new DLSequence(vec).getEncoded();
}
if (privKey == null) {
throw new IllegalArgumentException("private key as well as public key are null");
}
return privKey.getEncoded();
}
Aggregations