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"));
}
}
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;
}
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();
}
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();
}
}
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");
}
}
Aggregations