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, "BC");
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, "BC");
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 keystore-explorer by kaikramer.
the class KeyStoreState method isEntryPrivateKeyEqual.
protected boolean isEntryPrivateKeyEqual(KeyStoreState targetState, String alias, Password password) throws GeneralSecurityException {
Key currentKey = keyStore.getKey(alias, password.toCharArray());
Key targetKey = targetState.getKeyStore().getKey(alias, password.toCharArray());
// JDKDSAPrivateKey has no equals method defined
if ((currentKey instanceof JDKDSAPrivateKey) || (targetKey instanceof JDKDSAPrivateKey)) {
DSAPrivateKey currentDsaKey = (DSAPrivateKey) currentKey;
DSAPrivateKey targetDsaKey = (DSAPrivateKey) targetKey;
return currentDsaKey.getX().equals(targetDsaKey.getX()) && currentDsaKey.getParams().getG().equals(targetDsaKey.getParams().getG()) && currentDsaKey.getParams().getP().equals(targetDsaKey.getParams().getP()) && currentDsaKey.getParams().getQ().equals(targetDsaKey.getParams().getQ());
} else {
return currentKey.equals(targetKey);
}
}
use of java.security.interfaces.DSAPrivateKey in project keystore-explorer by kaikramer.
the class MsPvkUtil method blobToDsaPrivateKey.
private static DSAPrivateKey blobToDsaPrivateKey(byte[] dsaPrivateKeyBlob) throws CryptoException {
try {
ByteBuffer bb = ByteBuffer.wrap(dsaPrivateKeyBlob);
bb.order(ByteOrder.LITTLE_ENDIAN);
// Get each blob field
// dsspubkey.magic
long magic = UnsignedUtil.getInt(bb);
// Check magic field is valid
if (magic != DSS_PRIV_MAGIC) {
throw new CryptoException(MessageFormat.format(res.getString("InvalidDsaMagicField.exception.message"), Long.toHexString(magic), Long.toHexString(DSS_PRIV_MAGIC)));
}
// dsspubkey.bitlen
long bitLength = UnsignedUtil.getInt(bb);
// modulus
BigInteger p = readBigInteger(bb, (int) (bitLength / 8));
// prime
BigInteger q = readBigInteger(bb, 20);
// generator
BigInteger g = readBigInteger(bb, (int) (bitLength / 8));
// secret exponent
BigInteger x = readBigInteger(bb, 20);
// Ignore 24 bytes of dssseed (only applicable to public keys)
for (int i = 0; i < 24; i++) {
bb.get();
}
DSAPrivateKeySpec dsaPrivateKeySpec = new DSAPrivateKeySpec(x, p, q, g);
KeyFactory keyFactory = KeyFactory.getInstance("DSA");
return (DSAPrivateKey) keyFactory.generatePrivate(dsaPrivateKeySpec);
} catch (IOException ex) {
throw new CryptoException(res.getString("NoConvertBlobToDsaKey.exception.message"), ex);
} catch (GeneralSecurityException ex) {
throw new CryptoException(res.getString("NoConvertBlobToDsaKey.exception.message"), ex);
}
}
use of java.security.interfaces.DSAPrivateKey in project keystore-explorer by kaikramer.
the class MsPvkUtil method getEncryptedInternal.
private static byte[] getEncryptedInternal(PrivateKey privateKey, int keyType, Password password, boolean strong) throws CryptoException {
try {
// Write PVK to a byte buffer set up to write little endian
ByteBuffer bb = ByteBuffer.wrap(new byte[PVK_BUFFER_LENGTH]);
bb.order(ByteOrder.LITTLE_ENDIAN);
// Write magic number, reserved and and key type fields
writeReservedMagicKeyType(bb, keyType);
// Get password as bytes
byte[] passwordBytes = new String(password.toCharArray()).getBytes();
// Generate salt for encryption
byte[] salt = generate16ByteSalt();
// Concatenate the salt and password
byte[] saltAndPassword = new byte[salt.length + passwordBytes.length];
System.arraycopy(salt, 0, saltAndPassword, 0, salt.length);
System.arraycopy(passwordBytes, 0, saltAndPassword, salt.length, passwordBytes.length);
// Digest the salt and password to create the encryption key
MessageDigest messageDigest = MessageDigest.getInstance("SHA1");
byte[] key = messageDigest.digest(saltAndPassword);
// Get private key blob
byte[] privateKeyBlob = null;
if (privateKey instanceof RSAPrivateCrtKey) {
privateKeyBlob = rsaPrivateKeyToBlob((RSAPrivateCrtKey) privateKey);
} else {
privateKeyBlob = dsaPrivateKeyToBlob((DSAPrivateKey) privateKey);
}
// Encrypt private key blob
byte[] encryptedPrivateKeyBlob = null;
if (strong) {
// Strong version uses all 16 bytes of the key
byte[] strongKey = new byte[16];
System.arraycopy(key, 0, strongKey, 0, strongKey.length);
encryptedPrivateKeyBlob = encryptPrivateKeyBlob(privateKeyBlob, strongKey);
} else {
// The weak version uses only 5 bytes of the key followed by 11 zero bytes
byte[] weakKey = new byte[16];
System.arraycopy(key, 0, weakKey, 0, 5);
for (int i = 5; i < weakKey.length; i++) {
weakKey[i] = 0;
}
encryptedPrivateKeyBlob = encryptPrivateKeyBlob(privateKeyBlob, weakKey);
}
// Write type field - encrypted
UnsignedUtil.putInt(bb, PVK_ENCRYPTED);
// Write salt length field
UnsignedUtil.putInt(bb, salt.length);
// Write key length field - length of the blob plus length blob header
int keyLength = encryptedPrivateKeyBlob.length + BLOB_HEADER_LENGTH;
UnsignedUtil.putInt(bb, keyLength);
// Write salt
bb.put(salt);
// Write private key blob header
writePrivateKeyBlobHeader(bb, keyType, privateKey);
// Write blob
bb.put(encryptedPrivateKeyBlob);
byte[] encryptedPvk = getBufferBytes(bb);
return encryptedPvk;
} catch (IOException ex) {
throw new CryptoException(res.getString("NoGetMsPvk.exception.message"), ex);
} catch (NoSuchAlgorithmException ex) {
throw new CryptoException(res.getString("NoGetMsPvk.exception.message"), ex);
}
}
use of java.security.interfaces.DSAPrivateKey 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));
}
}
Aggregations