use of java.security.KeyFactory in project zaproxy by zaproxy.
the class SslCertificateUtils method generatePrivateKeyFromDER.
private static RSAPrivateKey generatePrivateKeyFromDER(byte[] keyBytes) throws InvalidKeySpecException, NoSuchAlgorithmException {
PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(keyBytes);
KeyFactory factory = KeyFactory.getInstance("RSA");
return (RSAPrivateKey) factory.generatePrivate(spec);
}
use of java.security.KeyFactory in project translationstudio8 by heartsome.
the class InstallKeyEncrypt method decrypt.
public static byte[] decrypt(byte[] srcBytes) throws Exception {
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(privateKey);
KeyFactory kf = KeyFactory.getInstance(algorithm);
PrivateKey keyPrivate = kf.generatePrivate(keySpec);
Cipher cipher = Cipher.getInstance(algorithm, new org.bouncycastle.jce.provider.BouncyCastleProvider());
cipher.init(Cipher.DECRYPT_MODE, keyPrivate);
int blockSize = cipher.getBlockSize();
ByteArrayOutputStream bout = new ByteArrayOutputStream(blockSize);
int j = 0;
while (srcBytes.length - j * blockSize > 0) {
byte[] temp = cipher.doFinal(srcBytes, j * blockSize, blockSize);
bout.write(temp);
j++;
}
return bout.toByteArray();
}
use of java.security.KeyFactory in project error-prone by google.
the class InsecureCipherModePositiveCases method keyOperations.
public void keyOperations(StringProvider provider) {
KeyFactory keyFactory;
KeyAgreement keyAgreement;
KeyPairGenerator keyPairGenerator;
final String dh = "DH";
try {
// BUG: Diagnostic contains: compile-time constant
keyFactory = KeyFactory.getInstance(provider.get());
// BUG: Diagnostic contains: Diffie-Hellman on prime fields
keyFactory = KeyFactory.getInstance(dh);
// BUG: Diagnostic contains: DSA
keyAgreement = KeyAgreement.getInstance("DSA");
// BUG: Diagnostic contains: compile-time constant
keyAgreement = KeyAgreement.getInstance(provider.get());
// BUG: Diagnostic contains: Diffie-Hellman on prime fields
keyPairGenerator = KeyPairGenerator.getInstance(dh);
// BUG: Diagnostic contains: compile-time constant
keyPairGenerator = KeyPairGenerator.getInstance(provider.get());
} catch (NoSuchAlgorithmException e) {
// We don't handle any exception as this code is not meant to be executed.
}
}
use of java.security.KeyFactory in project platformlayer by platformlayer.
the class KeyParser method parse.
public Object parse(String s) {
Object key = null;
if (key == null) {
if (s.contains(BEGIN_PRIVATE_KEY)) {
String payload = s.substring(s.indexOf(BEGIN_PRIVATE_KEY) + BEGIN_PRIVATE_KEY.length());
if (payload.contains(END_PRIVATE_KEY)) {
payload = payload.substring(0, payload.indexOf(END_PRIVATE_KEY));
key = tryParsePemFormat(payload);
}
}
}
if (key == null) {
try {
PemReader reader = new PemReader(new StringReader(s));
PemObject pemObject = reader.readPemObject();
reader.close();
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(pemObject.getContent());
KeyFactory kf = KeyFactory.getInstance("RSA");
PrivateKey privateKey = kf.generatePrivate(keySpec);
if (privateKey instanceof RSAPrivateCrtKey) {
RSAPrivateCrtKey rsaPrivateCrtKey = (RSAPrivateCrtKey) privateKey;
RSAPublicKeySpec publicKeySpec = new java.security.spec.RSAPublicKeySpec(rsaPrivateCrtKey.getModulus(), rsaPrivateCrtKey.getPublicExponent());
PublicKey publicKey = kf.generatePublic(publicKeySpec);
key = new KeyPair(publicKey, privateKey);
} else {
key = privateKey;
}
} catch (Exception e) {
log.debug("Error reading pem data", e);
return null;
}
}
if (key == null) {
try {
// TODO: Check if looks like base64??
byte[] fromBase64 = Base64.decode(s);
key = parse(fromBase64);
} catch (Exception e) {
log.debug("Cannot decode as base64", e);
}
}
return key;
}
use of java.security.KeyFactory in project platform_frameworks_base by android.
the class PackageParser method parsePublicKey.
public static final PublicKey parsePublicKey(final String encodedPublicKey) {
if (encodedPublicKey == null) {
Slog.w(TAG, "Could not parse null public key");
return null;
}
EncodedKeySpec keySpec;
try {
final byte[] encoded = Base64.decode(encodedPublicKey, Base64.DEFAULT);
keySpec = new X509EncodedKeySpec(encoded);
} catch (IllegalArgumentException e) {
Slog.w(TAG, "Could not parse verifier public key; invalid Base64");
return null;
}
/* First try the key as an RSA key. */
try {
final KeyFactory keyFactory = KeyFactory.getInstance("RSA");
return keyFactory.generatePublic(keySpec);
} catch (NoSuchAlgorithmException e) {
Slog.wtf(TAG, "Could not parse public key: RSA KeyFactory not included in build");
} catch (InvalidKeySpecException e) {
// Not a RSA public key.
}
/* Now try it as a ECDSA key. */
try {
final KeyFactory keyFactory = KeyFactory.getInstance("EC");
return keyFactory.generatePublic(keySpec);
} catch (NoSuchAlgorithmException e) {
Slog.wtf(TAG, "Could not parse public key: EC KeyFactory not included in build");
} catch (InvalidKeySpecException e) {
// Not a ECDSA public key.
}
/* Now try it as a DSA key. */
try {
final KeyFactory keyFactory = KeyFactory.getInstance("DSA");
return keyFactory.generatePublic(keySpec);
} catch (NoSuchAlgorithmException e) {
Slog.wtf(TAG, "Could not parse public key: DSA KeyFactory not included in build");
} catch (InvalidKeySpecException e) {
// Not a DSA public key.
}
/* Not a supported key type */
return null;
}
Aggregations