Search in sources :

Example 86 with KeyPair

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

the class AndroidKeyPairGeneratorTest method testKeyPairGenerator_GenerateKeyPair_EC_P521_Unencrypted_Success.

public void testKeyPairGenerator_GenerateKeyPair_EC_P521_Unencrypted_Success() throws Exception {
    mGenerator.initialize(new KeyPairGeneratorSpec.Builder(getContext()).setAlias(TEST_ALIAS_1).setKeyType("EC").setKeySize(521).setSubject(TEST_DN_1).setSerialNumber(TEST_SERIAL_1).setStartDate(NOW).setEndDate(NOW_PLUS_10_YEARS).build());
    final KeyPair pair = mGenerator.generateKeyPair();
    assertNotNull("The KeyPair returned should not be null", pair);
    assertKeyPairCorrect(pair, TEST_ALIAS_1, "EC", 521, null, TEST_DN_1, TEST_SERIAL_1, NOW, NOW_PLUS_10_YEARS);
}
Also used : KeyPairGeneratorSpec(android.security.KeyPairGeneratorSpec) KeyPair(java.security.KeyPair)

Example 87 with KeyPair

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

the class AndroidKeyPairGeneratorTest method testKeyPairGenerator_GenerateKeyPair_RSA_Unencrypted_Success.

public void testKeyPairGenerator_GenerateKeyPair_RSA_Unencrypted_Success() throws Exception {
    mGenerator.initialize(new KeyPairGeneratorSpec.Builder(getContext()).setAlias(TEST_ALIAS_1).setSubject(TEST_DN_1).setSerialNumber(TEST_SERIAL_1).setStartDate(NOW).setEndDate(NOW_PLUS_10_YEARS).build());
    final KeyPair pair = mGenerator.generateKeyPair();
    assertNotNull("The KeyPair returned should not be null", pair);
    assertKeyPairCorrect(pair, TEST_ALIAS_1, "RSA", 2048, null, TEST_DN_1, TEST_SERIAL_1, NOW, NOW_PLUS_10_YEARS);
}
Also used : KeyPairGeneratorSpec(android.security.KeyPairGeneratorSpec) KeyPair(java.security.KeyPair)

Example 88 with KeyPair

use of java.security.KeyPair 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 89 with KeyPair

use of java.security.KeyPair 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 90 with KeyPair

use of java.security.KeyPair 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

KeyPair (java.security.KeyPair)313 KeyPairGenerator (java.security.KeyPairGenerator)146 X509Certificate (java.security.cert.X509Certificate)61 PrivateKey (java.security.PrivateKey)60 PublicKey (java.security.PublicKey)59 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)36 IOException (java.io.IOException)34 SecureRandom (java.security.SecureRandom)31 Test (org.junit.Test)26 KeyFactory (java.security.KeyFactory)23 KeyStore (java.security.KeyStore)23 Date (java.util.Date)22 BigInteger (java.math.BigInteger)21 Signature (java.security.Signature)19 File (java.io.File)17 Cipher (javax.crypto.Cipher)17 KeyPairGeneratorSpec (android.security.KeyPairGeneratorSpec)16 RSAPrivateKey (java.security.interfaces.RSAPrivateKey)16 RSAPublicKey (java.security.interfaces.RSAPublicKey)16 HashMap (java.util.HashMap)16