Search in sources :

Example 61 with KeyPairGenerator

use of java.security.KeyPairGenerator in project nhin-d by DirectProject.

the class CRLRevocationManager_getCrlFromUriTest method testGetCrlFromUri_notInCache_loadFromCacheFile_assertCRLFound.

public void testGetCrlFromUri_notInCache_loadFromCacheFile_assertCRLFound() throws Exception {
    CRLRevocationManager.initCRLCacheLocation();
    String uri = "http://localhost:8080/certs.crl";
    X509CRL crl = (X509CRL) TestUtils.loadCRL("certs.crl");
    KeyPairGenerator kpGen = KeyPairGenerator.getInstance("RSA", "BC");
    KeyPair pair = kpGen.generateKeyPair();
    Calendar cal = Calendar.getInstance();
    cal.set(Calendar.YEAR, cal.get(Calendar.YEAR) + 10);
    X509V2CRLGenerator crlGen = new X509V2CRLGenerator();
    crlGen.setIssuerDN(new X500Principal("CN=Test CRL"));
    crlGen.setNextUpdate(cal.getTime());
    crlGen.setSignatureAlgorithm("SHA256withRSAEncryption");
    crlGen.setThisUpdate(Calendar.getInstance().getTime());
    crlGen.addCRL(crl);
    crl = crlGen.generate(pair.getPrivate(), "BC");
    CRLRevocationManager.INSTANCE.writeCRLCacheFile(uri, crl);
    X509CRL retCrl = CRLRevocationManager.getInstance().getCrlFromUri(uri);
    assertNotNull(retCrl);
    assertEquals(crl, retCrl);
}
Also used : KeyPair(java.security.KeyPair) X509CRL(java.security.cert.X509CRL) Calendar(java.util.Calendar) X500Principal(javax.security.auth.x500.X500Principal) KeyPairGenerator(java.security.KeyPairGenerator) X509V2CRLGenerator(org.bouncycastle.x509.X509V2CRLGenerator)

Example 62 with KeyPairGenerator

use of java.security.KeyPairGenerator in project android_frameworks_base by DirtyUnicorns.

the class AndroidKeyPairGeneratorTest method testKeyPairGenerator_GenerateKeyPair_EC_Unencrypted_Success.

public void testKeyPairGenerator_GenerateKeyPair_EC_Unencrypted_Success() throws Exception {
    KeyPairGenerator generator = KeyPairGenerator.getInstance("EC", "AndroidKeyStore");
    generator.initialize(new KeyGenParameterSpec.Builder(TEST_ALIAS_1, KeyProperties.PURPOSE_SIGN | KeyProperties.PURPOSE_VERIFY).setCertificateSubject(TEST_DN_1).setCertificateSerialNumber(TEST_SERIAL_1).setCertificateNotBefore(NOW).setCertificateNotAfter(NOW_PLUS_10_YEARS).setDigests(KeyProperties.DIGEST_SHA256).build());
    final KeyPair pair = generator.generateKeyPair();
    assertNotNull("The KeyPair returned should not be null", pair);
    assertKeyPairCorrect(pair, TEST_ALIAS_1, "EC", 256, null, TEST_DN_1, TEST_SERIAL_1, NOW, NOW_PLUS_10_YEARS);
}
Also used : KeyPair(java.security.KeyPair) RSAKeyGenParameterSpec(java.security.spec.RSAKeyGenParameterSpec) KeyPairGenerator(java.security.KeyPairGenerator)

Example 63 with KeyPairGenerator

use of java.security.KeyPairGenerator in project jdk8u_jdk by JetBrains.

the class FinalizeHalf method test.

static void test(String algo, Provider provider, boolean priv, Consumer<Key> method) throws Exception {
    KeyPairGenerator generator;
    try {
        generator = KeyPairGenerator.getInstance(algo, provider);
    } catch (NoSuchAlgorithmException nsae) {
        return;
    }
    System.out.println("Checking " + provider.getName() + ", " + algo);
    KeyPair pair = generator.generateKeyPair();
    Key key = priv ? pair.getPrivate() : pair.getPublic();
    pair = null;
    for (int i = 0; i < 32; ++i) {
        System.gc();
    }
    try {
        method.accept(key);
    } catch (ProviderException pe) {
        failures++;
    }
}
Also used : KeyPair(java.security.KeyPair) ProviderException(java.security.ProviderException) KeyPairGenerator(java.security.KeyPairGenerator) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) Key(java.security.Key)

Example 64 with KeyPairGenerator

use of java.security.KeyPairGenerator in project kdeconnect-android by KDE.

the class RsaHelper method initialiseRsaKeys.

public static void initialiseRsaKeys(Context context) {
    SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);
    if (!settings.contains("publicKey") || !settings.contains("privateKey")) {
        KeyPair keyPair;
        try {
            KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
            keyGen.initialize(2048);
            keyPair = keyGen.genKeyPair();
        } catch (Exception e) {
            e.printStackTrace();
            Log.e("KDE/initializeRsaKeys", "Exception");
            return;
        }
        byte[] publicKey = keyPair.getPublic().getEncoded();
        byte[] privateKey = keyPair.getPrivate().getEncoded();
        SharedPreferences.Editor edit = settings.edit();
        edit.putString("publicKey", Base64.encodeToString(publicKey, 0).trim() + "\n");
        edit.putString("privateKey", Base64.encodeToString(privateKey, 0));
        edit.apply();
    }
}
Also used : KeyPair(java.security.KeyPair) SharedPreferences(android.content.SharedPreferences) KeyPairGenerator(java.security.KeyPairGenerator) JSONException(org.json.JSONException) GeneralSecurityException(java.security.GeneralSecurityException)

Example 65 with KeyPairGenerator

use of java.security.KeyPairGenerator in project jdk8u_jdk by JetBrains.

the class Offsets method init.

static Offsets init(String provider, String algorithm) throws NoSuchAlgorithmException, NoSuchProviderException, InvalidKeyException, SignatureException {
    // fill the cleartext data with random bytes
    byte[] cleartext = new byte[100];
    RandomFactory.getRandom().nextBytes(cleartext);
    // NONEwith requires input to be of 20 bytes
    int size = algorithm.contains("NONEwith") ? 20 : 100;
    // create signature instance
    Signature signature = Signature.getInstance(algorithm, provider);
    String keyAlgo;
    if (algorithm.contains("RSA")) {
        keyAlgo = "RSA";
    } else if (algorithm.contains("ECDSA")) {
        keyAlgo = "EC";
    } else if (algorithm.contains("DSA")) {
        keyAlgo = "DSA";
    } else {
        throw new RuntimeException("Test doesn't support this signature " + "algorithm: " + algorithm);
    }
    KeyPairGenerator kpg = KeyPairGenerator.getInstance(keyAlgo, provider);
    KeyPair kp = kpg.generateKeyPair();
    PublicKey pubkey = kp.getPublic();
    PrivateKey privkey = kp.getPrivate();
    return new Offsets(signature, pubkey, privkey, size, cleartext);
}
Also used : KeyPair(java.security.KeyPair) PrivateKey(java.security.PrivateKey) PublicKey(java.security.PublicKey) Signature(java.security.Signature) KeyPairGenerator(java.security.KeyPairGenerator)

Aggregations

KeyPairGenerator (java.security.KeyPairGenerator)197 KeyPair (java.security.KeyPair)145 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)43 SecureRandom (java.security.SecureRandom)39 PublicKey (java.security.PublicKey)27 PrivateKey (java.security.PrivateKey)26 X509Certificate (java.security.cert.X509Certificate)23 KeyFactory (java.security.KeyFactory)21 IOException (java.io.IOException)19 BigInteger (java.math.BigInteger)17 GeneralSecurityException (java.security.GeneralSecurityException)15 Signature (java.security.Signature)15 Date (java.util.Date)15 Cipher (javax.crypto.Cipher)15 KeyAgreement (javax.crypto.KeyAgreement)15 RSAPublicKey (java.security.interfaces.RSAPublicKey)14 X500Principal (javax.security.auth.x500.X500Principal)13 ECPrivateKey (java.security.interfaces.ECPrivateKey)12 ECPublicKey (java.security.interfaces.ECPublicKey)12 HashMap (java.util.HashMap)11