use of java.security.KeyFactory in project wycheproof by google.
the class EcdhTest method testDecode.
public void testDecode() throws Exception {
KeyFactory kf = KeyFactory.getInstance("EC");
ECPublicKey key1 = (ECPublicKey) kf.generatePublic(EC_VALID_PUBLIC_KEY.getSpec());
ECPublicKey key2 = (ECPublicKey) kf.generatePublic(EC_VALID_PUBLIC_KEY.getX509EncodedKeySpec());
ECParameterSpec params1 = key1.getParams();
ECParameterSpec params2 = key2.getParams();
assertEquals(params1.getCofactor(), params2.getCofactor());
assertEquals(params1.getCurve(), params2.getCurve());
assertEquals(params1.getGenerator(), params2.getGenerator());
assertEquals(params1.getOrder(), params2.getOrder());
assertEquals(key1.getW(), key2.getW());
}
use of java.security.KeyFactory in project wycheproof by google.
the class RsaSignatureTest method testVectors.
/**
* Tests an RSA signature implementation with a number of vectors. The test assumes that the first
* test vector is valid, but everything else is invalid. Many of the test vectors are derived by
* signing modified ASN encodings. Hence accepting an invalid signature does not mean by itself
* that the implementation can be broken, but often points to a bigger problem. The test expects
* that verifying an invalid signature either leads to a return value False or will result in a
* SignatureException. Verifying an RSA signature should not result in an RuntimeException, so
* that reasonably implementated applications can be expected to catch and treat invalid
* signatures appropriately. While RuntimeExceptions may not be exploitable, they often indicate
* an oversight in the implementation of the provider.
* https://docs.oracle.com/javase/tutorial/essential/exceptions/runtime.html
*/
public void testVectors(RSAPublicKeySpec key, String algorithm, String[] testvectors) throws Exception {
byte[] message = "Test".getBytes("UTF-8");
Signature verifier = Signature.getInstance(algorithm);
KeyFactory kf = KeyFactory.getInstance("RSA");
PublicKey pub = kf.generatePublic(key);
int errors = 0;
boolean first = true;
for (String signature : testvectors) {
byte[] signatureBytes = TestUtil.hexToBytes(signature);
verifier.initVerify(pub);
verifier.update(message);
boolean verified = false;
try {
verified = verifier.verify(signatureBytes);
} catch (SignatureException ex) {
// verify can throw SignatureExceptions if the signature is malformed.
}
if (first && !verified) {
System.out.println("Valid signature not verified:" + signature);
errors++;
} else if (!first && verified) {
System.out.println("Incorrect signature verified:" + signature);
errors++;
}
first = false;
}
assertEquals(0, errors);
}
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 pulsar by yahoo.
the class SecurityUtility method loadPrivateKeyFromPemFile.
public static PrivateKey loadPrivateKeyFromPemFile(String keyFilePath) throws KeyManagementException {
PrivateKey privateKey = null;
if (keyFilePath == null || keyFilePath.isEmpty()) {
return privateKey;
}
try (BufferedReader reader = new BufferedReader(new FileReader(keyFilePath))) {
StringBuilder sb = new StringBuilder();
String previousLine = "";
String currentLine = null;
// Skip the first line (-----BEGIN RSA PRIVATE KEY-----)
reader.readLine();
while ((currentLine = reader.readLine()) != null) {
sb.append(previousLine);
previousLine = currentLine;
}
// Skip the last line (-----END RSA PRIVATE KEY-----)
KeyFactory kf = KeyFactory.getInstance("RSA");
KeySpec keySpec = new PKCS8EncodedKeySpec(Base64.getDecoder().decode(sb.toString()));
privateKey = kf.generatePrivate(keySpec);
} catch (GeneralSecurityException | IOException e) {
throw new KeyManagementException("Private key loading error", e);
}
return privateKey;
}
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);
}
Aggregations