use of java.security.spec.X509EncodedKeySpec in project robovm by robovm.
the class SignatureTest method test18566.
// http://code.google.com/p/android/issues/detail?id=18566
// http://b/5038554
public void test18566() throws Exception {
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(PK_BYTES);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PublicKey pk = keyFactory.generatePublic(keySpec);
Signature sig = Signature.getInstance("SHA256withRSA");
sig.initVerify(pk);
sig.update(CONTENT);
assertTrue(sig.verify(SIGNATURE));
}
use of java.security.spec.X509EncodedKeySpec in project robovm by robovm.
the class X509EncodedKeySpecTest method testX509EncodedKeySpec.
//
// Test cases
//
/**
* Test for <code>X509EncodedKeySpec</code> constructor<br>
* Assertion: constructs new <code>X509EncodedKeySpec</code>
* object using valid parameter
*/
public final void testX509EncodedKeySpec() {
byte[] encodedKey = new byte[] { (byte) 1, (byte) 2, (byte) 3, (byte) 4 };
EncodedKeySpec eks = new X509EncodedKeySpec(encodedKey);
assertTrue(eks instanceof X509EncodedKeySpec);
try {
eks = new X509EncodedKeySpec(null);
fail("expected NullPointerException");
} catch (NullPointerException e) {
// ok
}
}
use of java.security.spec.X509EncodedKeySpec in project robovm by robovm.
the class X509EncodedKeySpecTest method testGetFormat.
/**
* Test for <code>getFormat()</code> method
* Assertion: returns format name (always "X.509")
*/
public final void testGetFormat() {
byte[] encodedKey = new byte[] { (byte) 1, (byte) 2, (byte) 3, (byte) 4 };
X509EncodedKeySpec meks = new X509EncodedKeySpec(encodedKey);
assertEquals("X.509", meks.getFormat());
}
use of java.security.spec.X509EncodedKeySpec in project android_frameworks_base by crdroidandroid.
the class PackageParser method parsePublicKey.
public static final PublicKey parsePublicKey(final String encodedPublicKey) {
if (encodedPublicKey == null) {
Slog.w(TAG, "Could not parse null public key");
return null;
}
EncodedKeySpec keySpec;
try {
final byte[] encoded = Base64.decode(encodedPublicKey, Base64.DEFAULT);
keySpec = new X509EncodedKeySpec(encoded);
} catch (IllegalArgumentException e) {
Slog.w(TAG, "Could not parse verifier public key; invalid Base64");
return null;
}
/* First try the key as an RSA key. */
try {
final KeyFactory keyFactory = KeyFactory.getInstance("RSA");
return keyFactory.generatePublic(keySpec);
} catch (NoSuchAlgorithmException e) {
Slog.wtf(TAG, "Could not parse public key: RSA KeyFactory not included in build");
} catch (InvalidKeySpecException e) {
// Not a RSA public key.
}
/* Now try it as a ECDSA key. */
try {
final KeyFactory keyFactory = KeyFactory.getInstance("EC");
return keyFactory.generatePublic(keySpec);
} catch (NoSuchAlgorithmException e) {
Slog.wtf(TAG, "Could not parse public key: EC KeyFactory not included in build");
} catch (InvalidKeySpecException e) {
// Not a ECDSA public key.
}
/* Now try it as a DSA key. */
try {
final KeyFactory keyFactory = KeyFactory.getInstance("DSA");
return keyFactory.generatePublic(keySpec);
} catch (NoSuchAlgorithmException e) {
Slog.wtf(TAG, "Could not parse public key: DSA KeyFactory not included in build");
} catch (InvalidKeySpecException e) {
// Not a DSA public key.
}
/* Not a supported key type */
return null;
}
use of java.security.spec.X509EncodedKeySpec in project chuidiang-ejemplos by chuidiang.
the class RSAAsymetricCrypto method loadPublicKey.
private static PublicKey loadPublicKey(String fileName) throws Exception {
FileInputStream fis = new FileInputStream(fileName);
int numBtyes = fis.available();
byte[] bytes = new byte[numBtyes];
fis.read(bytes);
fis.close();
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
KeySpec keySpec = new X509EncodedKeySpec(bytes);
PublicKey keyFromBytes = keyFactory.generatePublic(keySpec);
return keyFromBytes;
}
Aggregations