Search in sources :

Example 96 with KeyFactory

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());
}
Also used : ECPublicKey(java.security.interfaces.ECPublicKey) ECParameterSpec(java.security.spec.ECParameterSpec) KeyFactory(java.security.KeyFactory)

Example 97 with KeyFactory

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);
}
Also used : RSAPublicKey(java.security.interfaces.RSAPublicKey) PublicKey(java.security.PublicKey) Signature(java.security.Signature) SignatureException(java.security.SignatureException) KeyFactory(java.security.KeyFactory)

Example 98 with KeyFactory

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();
}
Also used : PrivateKey(java.security.PrivateKey) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) Cipher(javax.crypto.Cipher) ByteArrayOutputStream(java.io.ByteArrayOutputStream) KeyFactory(java.security.KeyFactory)

Example 99 with KeyFactory

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;
}
Also used : PrivateKey(java.security.PrivateKey) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) KeySpec(java.security.spec.KeySpec) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) GeneralSecurityException(java.security.GeneralSecurityException) KeyFactory(java.security.KeyFactory) KeyManagementException(java.security.KeyManagementException)

Example 100 with KeyFactory

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);
}
Also used : PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) RSAPrivateKey(java.security.interfaces.RSAPrivateKey) KeyFactory(java.security.KeyFactory)

Aggregations

KeyFactory (java.security.KeyFactory)407 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)180 PrivateKey (java.security.PrivateKey)177 PublicKey (java.security.PublicKey)120 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)114 X509EncodedKeySpec (java.security.spec.X509EncodedKeySpec)113 CertificateFactory (java.security.cert.CertificateFactory)103 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)99 ByteArrayInputStream (java.io.ByteArrayInputStream)93 Certificate (java.security.cert.Certificate)89 X509Certificate (java.security.cert.X509Certificate)87 RSAPublicKeySpec (java.security.spec.RSAPublicKeySpec)60 PrivateKeyEntry (java.security.KeyStore.PrivateKeyEntry)59 Entry (java.security.KeyStore.Entry)53 TrustedCertificateEntry (java.security.KeyStore.TrustedCertificateEntry)53 IOException (java.io.IOException)47 BigInteger (java.math.BigInteger)45 RSAPrivateKey (java.security.interfaces.RSAPrivateKey)43 RSAPublicKey (java.security.interfaces.RSAPublicKey)43 Signature (java.security.Signature)40