use of java.security.interfaces.DSAPrivateKey in project i2pplus by vituperative.
the class SigUtil method toJavaDSAKey.
public static DSAPrivateKey toJavaDSAKey(SigningPrivateKey pk) throws GeneralSecurityException {
KeyFactory kf = KeyFactory.getInstance("DSA");
// x p q g
KeySpec ks = new DSAPrivateKeySpec(new BigInteger(1, pk.getData()), // KeySpec ks = new DSAPrivateKeySpec(new NativeBigInteger(1, pk.getData()),
CryptoConstants.dsap, CryptoConstants.dsaq, CryptoConstants.dsag);
return (DSAPrivateKey) kf.generatePrivate(ks);
}
use of java.security.interfaces.DSAPrivateKey in project i2pplus by vituperative.
the class SigUtil method fromJavaKey.
/**
* Use if SigType is unknown.
* For efficiency, use fromJavakey(pk, type) if type is known.
*
* @param pk JAVA key!
* @throws IllegalArgumentException on unknown type
* @since 0.9.18
*/
public static SigningPrivateKey fromJavaKey(PrivateKey pk) throws GeneralSecurityException {
if (pk instanceof DSAPrivateKey) {
return fromJavaKey((DSAPrivateKey) pk);
}
if (pk instanceof ECPrivateKey) {
ECPrivateKey k = (ECPrivateKey) pk;
AlgorithmParameterSpec spec = k.getParams();
SigType type;
if (spec.equals(SigType.ECDSA_SHA256_P256.getParams()))
type = SigType.ECDSA_SHA256_P256;
else if (spec.equals(SigType.ECDSA_SHA384_P384.getParams()))
type = SigType.ECDSA_SHA384_P384;
else if (spec.equals(SigType.ECDSA_SHA512_P521.getParams()))
type = SigType.ECDSA_SHA512_P521;
else {
// failing on Android (ticket #2296)
throw new IllegalArgumentException("Unknown EC type: " + pk.getClass() + " spec: " + spec.getClass());
}
return fromJavaKey(k, type);
}
if (pk instanceof EdDSAPrivateKey) {
return fromJavaKey((EdDSAPrivateKey) pk, SigType.EdDSA_SHA512_Ed25519);
}
if (pk instanceof RSAPrivateKey) {
RSAPrivateKey k = (RSAPrivateKey) pk;
int sz = k.getModulus().bitLength();
SigType type;
if (sz <= ((RSAKeyGenParameterSpec) SigType.RSA_SHA256_2048.getParams()).getKeysize())
type = SigType.RSA_SHA256_2048;
else if (sz <= ((RSAKeyGenParameterSpec) SigType.RSA_SHA384_3072.getParams()).getKeysize())
type = SigType.RSA_SHA384_3072;
else if (sz <= ((RSAKeyGenParameterSpec) SigType.RSA_SHA512_4096.getParams()).getKeysize())
type = SigType.RSA_SHA512_4096;
else
throw new IllegalArgumentException("Unknown RSA type");
return fromJavaKey(k, type);
}
throw new IllegalArgumentException("Unknown type: " + pk.getClass());
}
use of java.security.interfaces.DSAPrivateKey in project connectbot by connectbot.
the class SSH method retrieveIdentities.
@Override
public Map<String, byte[]> retrieveIdentities() {
Map<String, byte[]> pubKeys = new HashMap<>(manager.loadedKeypairs.size());
for (Entry<String, KeyHolder> entry : manager.loadedKeypairs.entrySet()) {
KeyPair pair = entry.getValue().pair;
try {
PrivateKey privKey = pair.getPrivate();
if (privKey instanceof RSAPrivateKey) {
RSAPublicKey pubkey = (RSAPublicKey) pair.getPublic();
pubKeys.put(entry.getKey(), RSASHA1Verify.get().encodePublicKey(pubkey));
} else if (privKey instanceof DSAPrivateKey) {
DSAPublicKey pubkey = (DSAPublicKey) pair.getPublic();
pubKeys.put(entry.getKey(), DSASHA1Verify.get().encodePublicKey(pubkey));
} else if (privKey instanceof ECPrivateKey) {
ECPublicKey pubkey = (ECPublicKey) pair.getPublic();
pubKeys.put(entry.getKey(), ECDSASHA2Verify.getVerifierForKey(pubkey).encodePublicKey(pubkey));
} else if (privKey instanceof Ed25519PrivateKey) {
Ed25519PublicKey pubkey = (Ed25519PublicKey) pair.getPublic();
pubKeys.put(entry.getKey(), Ed25519Verify.get().encodePublicKey(pubkey));
}
} catch (IOException ignored) {
}
}
return pubKeys;
}
use of java.security.interfaces.DSAPrivateKey in project SpringRemote by HaleyWang.
the class SSH2KeyPairFile method writeKeyPair.
public static byte[] writeKeyPair(ASCIIArmour armour, String password, SecureRandom random, KeyPair keyPair) throws SSH2FatalException {
ASN1Object pem;
PublicKey publicKey = keyPair.getPublic();
int headType;
if (publicKey instanceof DSAPublicKey) {
DSAPublicKey pubKey = (DSAPublicKey) keyPair.getPublic();
DSAPrivateKey prvKey = (DSAPrivateKey) keyPair.getPrivate();
DSAParams params = pubKey.getParams();
pem = new PEMDSAPrivate(0, params.getP(), params.getQ(), params.getG(), pubKey.getY(), prvKey.getX());
headType = TYPE_PEM_DSA;
} else if (publicKey instanceof RSAPublicKey) {
RSAPublicKey pubKey = (RSAPublicKey) keyPair.getPublic();
RSAPrivateCrtKey prvKey = (RSAPrivateCrtKey) keyPair.getPrivate();
pem = new PEMRSAPrivate(0, pubKey.getModulus(), pubKey.getPublicExponent(), prvKey.getPrivateExponent(), prvKey.getPrimeP(), prvKey.getPrimeQ(), prvKey.getCrtCoefficient());
headType = TYPE_PEM_RSA;
} else if (publicKey instanceof ECPublicKey) {
ECPublicKey pubKey = (ECPublicKey) keyPair.getPublic();
ECPrivateKey prvKey = (ECPrivateKey) keyPair.getPrivate();
pem = new PEMECPrivate(pubKey, prvKey);
headType = TYPE_PEM_EC;
} else {
throw new SSH2FatalException("Unsupported key type: " + publicKey);
}
armour.setHeaderLine(BEGIN_PRV_KEY[headType]);
armour.setTailLine(END_PRV_KEY[headType]);
ByteArrayOutputStream enc = new ByteArrayOutputStream(128);
ASN1DER der = new ASN1DER();
try {
der.encode(enc, pem);
} catch (IOException e) {
throw new SSH2FatalException("Error while DER encoding");
}
byte[] keyBlob = enc.toByteArray();
if (password != null && password.length() > 0) {
byte[] iv = new byte[16];
random.setSeed(keyBlob);
for (int i = 0; i < iv.length; i++) {
byte[] r = new byte[1];
do {
random.nextBytes(r);
iv[i] = r[0];
} while (iv[i] == 0x00);
}
armour.setHeaderField(PRV_PROCTYPE, "4,ENCRYPTED");
armour.setHeaderField(PRV_DEKINFO, "AES-128-CBC," + HexDump.toString(iv).toUpperCase());
int encLen = (16 - (keyBlob.length % 16)) + keyBlob.length;
byte[] encBuf = new byte[encLen];
doCipher(Cipher.ENCRYPT_MODE, "AES/CBC/PKCS5Padding", password, keyBlob, keyBlob.length, encBuf, iv);
keyBlob = encBuf;
}
return keyBlob;
}
use of java.security.interfaces.DSAPrivateKey in project SpringRemote by HaleyWang.
the class DSAWithSHA1 method sign.
protected byte[] sign(byte[] data) {
DSAPrivateKey key = (DSAPrivateKey) privateKey;
DSAParams parm = key.getParams();
BigInteger x = key.getX();
BigInteger p = parm.getP();
BigInteger q = parm.getQ();
BigInteger g = parm.getG();
BigInteger[] sign = DSAAlgorithm.sign(x, p, q, g, data);
if (sign == null || sign.length != 2) {
return null;
}
BigInteger r = sign[0];
BigInteger s = sign[1];
// Encode
DSASIG dsasig = new DSASIG(r, s);
ByteArrayOutputStream enc = new ByteArrayOutputStream(128);
ASN1DER der = new ASN1DER();
try {
der.encode(enc, dsasig);
} catch (IOException e) {
// This should not happen
}
return enc.toByteArray();
}
Aggregations