Search in sources :

Example 51 with PrivateKey

use of java.security.PrivateKey in project robovm by robovm.

the class SignatureTest method testSign_SHA256withRSA_Key_Success.

public void testSign_SHA256withRSA_Key_Success() throws Exception {
    KeyFactory kf = KeyFactory.getInstance("RSA");
    RSAPrivateKeySpec keySpec = new RSAPrivateKeySpec(RSA_2048_modulus, RSA_2048_privateExponent);
    final PrivateKey privKey = kf.generatePrivate(keySpec);
    Signature sig = Signature.getInstance("SHA256withRSA");
    sig.initSign(privKey);
    sig.update(Vector2Data);
    byte[] signature = sig.sign();
    assertNotNull("Signature must not be null", signature);
    assertTrue("Signature should match expected", Arrays.equals(signature, SHA256withRSA_Vector2Signature));
    RSAPublicKeySpec pubKeySpec = new RSAPublicKeySpec(RSA_2048_modulus, RSA_2048_publicExponent);
    PublicKey pubKey = kf.generatePublic(pubKeySpec);
    sig.initVerify(pubKey);
    sig.update(Vector2Data);
    assertTrue("Signature must verify correctly", sig.verify(signature));
}
Also used : PrivateKey(java.security.PrivateKey) RSAPrivateKeySpec(java.security.spec.RSAPrivateKeySpec) PublicKey(java.security.PublicKey) Signature(java.security.Signature) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) KeyFactory(java.security.KeyFactory)

Example 52 with PrivateKey

use of java.security.PrivateKey in project robovm by robovm.

the class SignatureTest method testSign_SHA1withRSA_CrtKeyWithPublicExponent_Success.

public void testSign_SHA1withRSA_CrtKeyWithPublicExponent_Success() throws Exception {
    KeyFactory kf = KeyFactory.getInstance("RSA");
    RSAPrivateCrtKeySpec keySpec = new RSAPrivateCrtKeySpec(RSA_2048_modulus, RSA_2048_publicExponent, RSA_2048_privateExponent, null, null, null, null, null);
    // The RI fails on this key which is totally unreasonable.
    final PrivateKey privKey;
    try {
        privKey = kf.generatePrivate(keySpec);
    } catch (NullPointerException e) {
        if (StandardNames.IS_RI) {
            return;
        } else {
            fail("Private key should be created");
            return;
        }
    }
    Signature sig = Signature.getInstance("SHA1withRSA");
    sig.initSign(privKey);
    sig.update(Vector1Data);
    byte[] signature = sig.sign();
    assertNotNull("Signature must not be null", signature);
    assertTrue("Signature should match expected", Arrays.equals(signature, SHA1withRSA_Vector1Signature));
    RSAPublicKeySpec pubKeySpec = new RSAPublicKeySpec(RSA_2048_modulus, RSA_2048_publicExponent);
    PublicKey pubKey = kf.generatePublic(pubKeySpec);
    sig.initVerify(pubKey);
    sig.update(Vector1Data);
    assertTrue("Signature must verify correctly", sig.verify(signature));
}
Also used : RSAPrivateCrtKeySpec(java.security.spec.RSAPrivateCrtKeySpec) PrivateKey(java.security.PrivateKey) PublicKey(java.security.PublicKey) Signature(java.security.Signature) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) KeyFactory(java.security.KeyFactory)

Example 53 with PrivateKey

use of java.security.PrivateKey in project robovm by robovm.

the class SignatureTest method testVerify_SHA1withRSA_Key_InitSignThenInitVerify_Success.

public void testVerify_SHA1withRSA_Key_InitSignThenInitVerify_Success() throws Exception {
    KeyFactory kf = KeyFactory.getInstance("RSA");
    RSAPublicKeySpec pubKeySpec = new RSAPublicKeySpec(RSA_2048_modulus, RSA_2048_publicExponent);
    PublicKey pubKey = kf.generatePublic(pubKeySpec);
    RSAPrivateKeySpec privKeySpec = new RSAPrivateKeySpec(RSA_2048_modulus, RSA_2048_privateExponent);
    PrivateKey privKey = kf.generatePrivate(privKeySpec);
    Signature sig = Signature.getInstance("SHA1withRSA");
    // Start a signing operation
    sig.initSign(privKey);
    sig.update(Vector2Data);
    // Switch to verify
    sig.initVerify(pubKey);
    sig.update(Vector1Data);
    assertTrue("Signature must match expected signature", sig.verify(SHA1withRSA_Vector1Signature));
}
Also used : PrivateKey(java.security.PrivateKey) RSAPrivateKeySpec(java.security.spec.RSAPrivateKeySpec) PublicKey(java.security.PublicKey) Signature(java.security.Signature) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) KeyFactory(java.security.KeyFactory)

Example 54 with PrivateKey

use of java.security.PrivateKey in project robovm by robovm.

the class SignatureTest method testSign_MD5withRSA_Key_Success.

public void testSign_MD5withRSA_Key_Success() throws Exception {
    KeyFactory kf = KeyFactory.getInstance("RSA");
    RSAPrivateKeySpec keySpec = new RSAPrivateKeySpec(RSA_2048_modulus, RSA_2048_privateExponent);
    PrivateKey privKey = kf.generatePrivate(keySpec);
    Signature sig = Signature.getInstance("MD5withRSA");
    sig.initSign(privKey);
    sig.update(Vector2Data);
    byte[] signature = sig.sign();
    assertNotNull("Signature must not be null", signature);
    assertTrue("Signature should match expected", Arrays.equals(signature, MD5withRSA_Vector2Signature));
    RSAPublicKeySpec pubKeySpec = new RSAPublicKeySpec(RSA_2048_modulus, RSA_2048_publicExponent);
    PublicKey pubKey = kf.generatePublic(pubKeySpec);
    sig.initVerify(pubKey);
    sig.update(Vector2Data);
    assertTrue("Signature must verify correctly", sig.verify(signature));
}
Also used : PrivateKey(java.security.PrivateKey) RSAPrivateKeySpec(java.security.spec.RSAPrivateKeySpec) PublicKey(java.security.PublicKey) Signature(java.security.Signature) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) KeyFactory(java.security.KeyFactory)

Example 55 with PrivateKey

use of java.security.PrivateKey in project robovm by robovm.

the class SignatureTest method testSign_SHA1withRSA_CrtKey_NoPrivateExponent_Failure.

public void testSign_SHA1withRSA_CrtKey_NoPrivateExponent_Failure() throws Exception {
    KeyFactory kf = KeyFactory.getInstance("RSA");
    RSAPrivateCrtKeySpec keySpec = new RSAPrivateCrtKeySpec(RSA_2048_modulus, RSA_2048_publicExponent, null, RSA_2048_primeP, RSA_2048_primeQ, null, null, null);
    // Failing on this key early is okay.
    final PrivateKey privKey;
    try {
        privKey = kf.generatePrivate(keySpec);
    } catch (NullPointerException e) {
        return;
    } catch (InvalidKeySpecException e) {
        return;
    }
    Signature sig = Signature.getInstance("SHA1withRSA");
    try {
        sig.initSign(privKey);
        fail("Should throw error when private exponent is not available");
    } catch (InvalidKeyException expected) {
    }
}
Also used : RSAPrivateCrtKeySpec(java.security.spec.RSAPrivateCrtKeySpec) PrivateKey(java.security.PrivateKey) Signature(java.security.Signature) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) InvalidKeyException(java.security.InvalidKeyException) KeyFactory(java.security.KeyFactory)

Aggregations

PrivateKey (java.security.PrivateKey)517 X509Certificate (java.security.cert.X509Certificate)217 KeyFactory (java.security.KeyFactory)169 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)144 Certificate (java.security.cert.Certificate)127 PublicKey (java.security.PublicKey)120 ByteArrayInputStream (java.io.ByteArrayInputStream)118 KeyStore (java.security.KeyStore)93 CertificateFactory (java.security.cert.CertificateFactory)92 IOException (java.io.IOException)81 Key (java.security.Key)74 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)73 PrivateKeyEntry (java.security.KeyStore.PrivateKeyEntry)70 Entry (java.security.KeyStore.Entry)60 TrustedCertificateEntry (java.security.KeyStore.TrustedCertificateEntry)60 KeyPair (java.security.KeyPair)59 SecretKey (javax.crypto.SecretKey)48 InvalidKeyException (java.security.InvalidKeyException)47 KeyStoreException (java.security.KeyStoreException)46 RSAPrivateKey (java.security.interfaces.RSAPrivateKey)46