Search in sources :

Example 31 with KeyPair

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

the class Copy method main.

public static void main(String[] args) throws Exception {
    KeyPairGenerator kg = KeyPairGenerator.getInstance(DSA);
    kg.initialize(KEY_SIZE);
    KeyPair kp = kg.genKeyPair();
    Signature signature = Signature.getInstance(DSA);
    Test original = new Test();
    SignedObject so = new SignedObject(original, kp.getPrivate(), signature);
    System.out.println("Signature algorithm: " + so.getAlgorithm());
    signature = Signature.getInstance(DSA, "SUN");
    if (!so.verify(kp.getPublic(), signature)) {
        throw new RuntimeException("Verification failed");
    }
    kg = KeyPairGenerator.getInstance(DSA);
    kg.initialize(KEY_SIZE);
    kp = kg.genKeyPair();
    if (so.verify(kp.getPublic(), signature)) {
        throw new RuntimeException("Unexpected success");
    }
    Object copy = so.getObject();
    if (!original.equals(copy)) {
        throw new RuntimeException("Signed object is not equal " + "to original one: " + copy);
    }
    /*
         * The signed object is a copy of an original one.
         * Once the copy is made, further manipulation
         * of the original object shouldn't has any effect on the copy.
         */
    original.set(MAGIC - 1);
    copy = so.getObject();
    if (original.equals(copy)) {
        throw new RuntimeException("Signed object is not a copy " + "of original one: " + copy);
    }
    System.out.println("Test passed");
}
Also used : KeyPair(java.security.KeyPair) Signature(java.security.Signature) SignedObject(java.security.SignedObject) KeyPairGenerator(java.security.KeyPairGenerator) SignedObject(java.security.SignedObject)

Example 32 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 33 with KeyPair

use of java.security.KeyPair in project GNS by MobilityFirst.

the class GuidImport method parse.

@Override
public void parse(String commandText) throws Exception {
    try {
        StringTokenizer st = new StringTokenizer(commandText.trim());
        if (st.countTokens() != 1) {
            wrongArguments();
            return;
        }
        String filename = st.nextToken();
        File f = new File(filename);
        GuidEntry guidEntry;
        try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(f))) {
            guidEntry = new GuidEntry(ois);
            ois.close();
        }
        KeyPairUtils.saveKeyPair(module.getGnsInstance(), guidEntry.getEntityName(), guidEntry.getGuid(), new KeyPair(guidEntry.getPublicKey(), guidEntry.getPrivateKey()));
        console.printString("Keys for " + guidEntry.getEntityName() + " read from " + filename + " and saved in local preferences.");
        console.printNewline();
    } catch (IOException | EncryptionException e) {
        console.printString("Failed to load keys ( " + e + ")\n");
    }
}
Also used : StringTokenizer(java.util.StringTokenizer) KeyPair(java.security.KeyPair) EncryptionException(edu.umass.cs.gnscommon.exceptions.client.EncryptionException) IOException(java.io.IOException) File(java.io.File) GuidEntry(edu.umass.cs.gnsclient.client.util.GuidEntry) FileInputStream(java.io.FileInputStream) ObjectInputStream(java.io.ObjectInputStream)

Example 34 with KeyPair

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

the class AndroidKeyStoreTest method generateCertificate.

@SuppressWarnings("deprecation")
private static X509Certificate generateCertificate(android.security.KeyStore keyStore, String alias, BigInteger serialNumber, X500Principal subjectDN, Date notBefore, Date notAfter) throws Exception {
    final String privateKeyAlias = Credentials.USER_PRIVATE_KEY + alias;
    KeyPair keyPair = AndroidKeyStoreProvider.loadAndroidKeyStoreKeyPairFromKeystore(keyStore, privateKeyAlias, KeyStore.UID_SELF);
    final X509V3CertificateGenerator certGen = new X509V3CertificateGenerator();
    certGen.setPublicKey(keyPair.getPublic());
    certGen.setSerialNumber(serialNumber);
    certGen.setSubjectDN(subjectDN);
    certGen.setIssuerDN(subjectDN);
    certGen.setNotBefore(notBefore);
    certGen.setNotAfter(notAfter);
    certGen.setSignatureAlgorithm("sha1WithRSA");
    final X509Certificate cert = certGen.generate(keyPair.getPrivate());
    return cert;
}
Also used : KeyPair(java.security.KeyPair) X509V3CertificateGenerator(com.android.org.bouncycastle.x509.X509V3CertificateGenerator) X509Certificate(java.security.cert.X509Certificate)

Example 35 with KeyPair

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

the class AndroidKeyPairGeneratorTest method testKeyPairGenerator_GenerateKeyPair_Encrypted_Success.

public void testKeyPairGenerator_GenerateKeyPair_Encrypted_Success() throws Exception {
    setupPassword();
    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).setEncryptionRequired().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 : KeyPair(java.security.KeyPair)

Aggregations

KeyPair (java.security.KeyPair)903 KeyPairGenerator (java.security.KeyPairGenerator)345 Test (org.junit.Test)235 PrivateKey (java.security.PrivateKey)189 X509Certificate (java.security.cert.X509Certificate)185 PublicKey (java.security.PublicKey)167 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)127 IOException (java.io.IOException)121 BigInteger (java.math.BigInteger)87 Date (java.util.Date)78 SecureRandom (java.security.SecureRandom)75 KeyStore (java.security.KeyStore)74 GeneralSecurityException (java.security.GeneralSecurityException)63 RSAPublicKey (java.security.interfaces.RSAPublicKey)55 X500Principal (javax.security.auth.x500.X500Principal)53 File (java.io.File)52 KeyFactory (java.security.KeyFactory)52 ECPrivateKey (java.security.interfaces.ECPrivateKey)52 ECPublicKey (java.security.interfaces.ECPublicKey)52 InvalidKeyException (java.security.InvalidKeyException)40