Search in sources :

Example 66 with RSAPublicKey

use of java.security.interfaces.RSAPublicKey in project BiglyBT by BiglySoftware.

the class AEVerifier method verifyData.

public static void verifyData(String data, byte[] signature) throws AEVerifierException, Exception {
    KeyFactory key_factory = KeyFactory.getInstance("RSA");
    RSAPublicKeySpec public_key_spec = new RSAPublicKeySpec(new BigInteger(modulus, 16), new BigInteger(pub_exp, 16));
    RSAPublicKey public_key = (RSAPublicKey) key_factory.generatePublic(public_key_spec);
    Signature sig = Signature.getInstance("MD5withRSA");
    sig.initVerify(public_key);
    sig.update(data.getBytes("UTF-8"));
    if (!sig.verify(signature)) {
        throw (new AEVerifierException(AEVerifierException.FT_SIGNATURE_BAD, "Data verification failed, signature doesn't match data"));
    }
}
Also used : RSAPublicKey(java.security.interfaces.RSAPublicKey) Signature(java.security.Signature) BigInteger(java.math.BigInteger) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) KeyFactory(java.security.KeyFactory)

Example 67 with RSAPublicKey

use of java.security.interfaces.RSAPublicKey in project Saiy-PS by brandall76.

the class DebugAction method getSignatures.

/**
 * Get the signatures for the application to be validated remotely
 *
 * @param ctx the application context
 * @return an Array List of {@link Signature}
 */
@SuppressLint("PackageManagerGetSignatures")
private static ArrayList<String> getSignatures(@NonNull final Context ctx) {
    if (DEBUG) {
        MyLog.i(CLS_NAME, "getSignatures");
    }
    final ArrayList<String> signatureArray = new ArrayList<>();
    try {
        final PackageManager pm = ctx.getPackageManager();
        final PackageInfo packageInfo = pm.getPackageInfo(ctx.getPackageName(), PackageManager.GET_SIGNATURES);
        final Signature[] signatures = packageInfo.signatures;
        CertificateFactory cf;
        X509Certificate cert;
        PublicKey key;
        String mhString;
        int modulusHash;
        if (signatures != null && signatures.length > 0) {
            for (final Signature signature : signatures) {
                cf = CertificateFactory.getInstance("X.509");
                cert = (X509Certificate) cf.generateCertificate(new ByteArrayInputStream(signature.toByteArray()));
                key = cert.getPublicKey();
                modulusHash = ((RSAPublicKey) key).getModulus().hashCode();
                mhString = String.valueOf(modulusHash) + String.valueOf(ctx.getResources().getInteger(R.integer.hash_version));
                if (DEBUG) {
                    MyLog.v(CLS_NAME, "hash: " + mhString);
                }
                signatureArray.add(mhString);
            }
        }
    } catch (final PackageManager.NameNotFoundException e) {
        if (DEBUG) {
            MyLog.w(CLS_NAME, "getSignature  NameNotFoundException");
            e.printStackTrace();
        }
    } catch (final CertificateException e) {
        if (DEBUG) {
            MyLog.w(CLS_NAME, "getSignature  CertificateException");
            e.printStackTrace();
        }
    } catch (final SecurityException e) {
        if (DEBUG) {
            MyLog.w(CLS_NAME, "getSignature  SecurityException");
            e.printStackTrace();
        }
    } catch (final NullPointerException e) {
        if (DEBUG) {
            MyLog.w(CLS_NAME, "getSignature  NullPointerException");
            e.printStackTrace();
        }
    } catch (final Exception e) {
        if (DEBUG) {
            MyLog.w(CLS_NAME, "getSignature  Exception");
            e.printStackTrace();
        }
    }
    return signatureArray;
}
Also used : PackageInfo(android.content.pm.PackageInfo) PublicKey(java.security.PublicKey) RSAPublicKey(java.security.interfaces.RSAPublicKey) ArrayList(java.util.ArrayList) CertificateException(java.security.cert.CertificateException) CertificateFactory(java.security.cert.CertificateFactory) X509Certificate(java.security.cert.X509Certificate) SuppressLint(android.annotation.SuppressLint) CertificateException(java.security.cert.CertificateException) PackageManager(android.content.pm.PackageManager) RSAPublicKey(java.security.interfaces.RSAPublicKey) ByteArrayInputStream(java.io.ByteArrayInputStream) Signature(android.content.pm.Signature) SuppressLint(android.annotation.SuppressLint)

Example 68 with RSAPublicKey

use of java.security.interfaces.RSAPublicKey in project certmgr by hdecarne.

the class KeyHelper method toString.

/**
 * Get the public key's string representation.
 *
 * @param publicKey The public key to format.
 * @return The public key's string representation.
 */
public static String toString(PublicKey publicKey) {
    StringBuilder buffer = new StringBuilder();
    buffer.append(publicKey.getAlgorithm());
    if (publicKey instanceof RSAPublicKey) {
        RSAPublicKey rsaPublicKey = (RSAPublicKey) publicKey;
        buffer.append("/").append(rsaPublicKey.getModulus().bitLength());
    } else if (publicKey instanceof ECPublicKey) {
        ECPublicKey ecPublicKey = (ECPublicKey) publicKey;
        buffer.append("/").append(ecPublicKey.getParams().getCurve().getField().getFieldSize());
    }
    return buffer.toString();
}
Also used : RSAPublicKey(java.security.interfaces.RSAPublicKey) ECPublicKey(java.security.interfaces.ECPublicKey)

Example 69 with RSAPublicKey

use of java.security.interfaces.RSAPublicKey in project apollo by apollo-rsps.

the class RsaKeyGenerator method main.

/**
 * The entry point of the RsaKeyGenerator.
 *
 * @param args The application arguments.
 */
public static void main(String[] args) throws Exception {
    Security.addProvider(new BouncyCastleProvider());
    KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA", "BC");
    keyPairGenerator.initialize(BIT_COUNT);
    KeyPair keyPair = keyPairGenerator.generateKeyPair();
    RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
    RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
    System.out.println("Place these keys in the client:");
    System.out.println("--------------------");
    System.out.println("public key: " + publicKey.getPublicExponent());
    System.out.println("modulus: " + publicKey.getModulus());
    try (PemWriter writer = new PemWriter(new FileWriter(PRIVATE_KEY_FILE))) {
        writer.writeObject(new PemObject("RSA PRIVATE KEY", privateKey.getEncoded()));
    } catch (Exception e) {
        System.err.println("Failed to write private key to " + PRIVATE_KEY_FILE);
        e.printStackTrace();
    }
}
Also used : PemObject(org.bouncycastle.util.io.pem.PemObject) KeyPair(java.security.KeyPair) RSAPublicKey(java.security.interfaces.RSAPublicKey) PemWriter(org.bouncycastle.util.io.pem.PemWriter) FileWriter(java.io.FileWriter) KeyPairGenerator(java.security.KeyPairGenerator) RSAPrivateKey(java.security.interfaces.RSAPrivateKey) BouncyCastleProvider(org.bouncycastle.jce.provider.BouncyCastleProvider)

Example 70 with RSAPublicKey

use of java.security.interfaces.RSAPublicKey in project registry by hortonworks.

the class TestCertificateUtil method testValidPEM.

@Test
public void testValidPEM() throws Exception {
    String pem = "MIICOjCCAaOgAwIBAgIJANXi/oWxvJNzMA0GCSqGSIb3DQEBBQUAMF8xCzAJBgNVBAYTAlVTMQ0w" + "CwYDVQQIEwRUZXN0MQ0wCwYDVQQHEwRUZXN0MQ8wDQYDVQQKEwZIYWRvb3AxDTALBgNVBAsTBFRl" + "c3QxEjAQBgNVBAMTCWxvY2FsaG9zdDAeFw0xNTAxMDIyMTE5MjRaFw0xNjAxMDIyMTE5MjRaMF8x" + "CzAJBgNVBAYTAlVTMQ0wCwYDVQQIEwRUZXN0MQ0wCwYDVQQHEwRUZXN0MQ8wDQYDVQQKEwZIYWRv" + "b3AxDTALBgNVBAsTBFRlc3QxEjAQBgNVBAMTCWxvY2FsaG9zdDCBnzANBgkqhkiG9w0BAQEFAAOB" + "jQAwgYkCgYEAwpfpLdi7dWTHNzETt+L7618/dWUQFb/C7o1jIxFgbKOVIB6d5YmvUbJck5PYxFkz" + "C25fmU5H71WGOI1Kle5TFDmIo+hqh5xqu1YNRZz9i6D94g+2AyYr9BpvH4ZfdHs7r9AU7c3kq68V" + "7OPuuaHb25J8isiOyA3RiWuJGQlXTdkCAwEAATANBgkqhkiG9w0BAQUFAAOBgQAdRUyCUqE9sdim" + "Fbll9BuZDKV16WXeWGq+kTd7ETe7l0fqXjq5EnrifOai0L/pXwVvS2jrFkKQRlRxRGUNaeEBZ2Wy" + "9aTyR+HGHCfvwoCegc9rAVw/DLaRriSO/jnEXzYK6XLVKH+hx5UXrJ7Oyc7JjZUc3g9kCWORThCX" + "Mzc1xA==";
    try {
        RSAPublicKey pk = CertificateUtil.parseRSAPublicKey(pem);
        assertNotNull(pk);
        assertEquals("RSA", pk.getAlgorithm());
    } catch (ServletException se) {
        fail("Should not have thrown ServletException");
    }
}
Also used : ServletException(javax.servlet.ServletException) RSAPublicKey(java.security.interfaces.RSAPublicKey) Test(org.junit.Test)

Aggregations

RSAPublicKey (java.security.interfaces.RSAPublicKey)240 RSAPrivateKey (java.security.interfaces.RSAPrivateKey)65 PublicKey (java.security.PublicKey)50 KeyPair (java.security.KeyPair)48 BigInteger (java.math.BigInteger)44 IOException (java.io.IOException)39 KeyPairGenerator (java.security.KeyPairGenerator)39 KeyFactory (java.security.KeyFactory)37 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)31 ECPublicKey (java.security.interfaces.ECPublicKey)30 X509Certificate (java.security.cert.X509Certificate)29 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)28 X509EncodedKeySpec (java.security.spec.X509EncodedKeySpec)27 Test (org.junit.Test)27 PrivateKey (java.security.PrivateKey)26 RSAPublicKeySpec (java.security.spec.RSAPublicKeySpec)26 CertificateException (java.security.cert.CertificateException)24 DSAPublicKey (java.security.interfaces.DSAPublicKey)24 InvalidKeyException (java.security.InvalidKeyException)22 ByteArrayInputStream (java.io.ByteArrayInputStream)21