use of java.security.KeyFactory in project PushSms by koush.
the class Registration method parse.
static Registration parse(String data) {
try {
byte[] bytes = Base64.decode(data, Base64.NO_WRAP);
BEncodedDictionary dict = BEncodedDictionary.parseDictionary(ByteBuffer.wrap(bytes));
Registration ret = new Registration();
ret.date = dict.getLong("date");
ret.registrationId = dict.getString("registration_id");
ret.endpoint = dict.getString("endpoint");
ret.localSequenceNumber = dict.getInt("local_sequence_number");
ret.remoteSequenceNumber = dict.getInt("remote_sequence_number");
ret.state = dict.getInt("state");
byte[] publicModulus = dict.getBytes("public_modulus");
byte[] publicExponent = dict.getBytes("public_exponent");
if (publicModulus != null && publicExponent != null) {
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
RSAPublicKeySpec rsaPublicKeySpec = new RSAPublicKeySpec(new BigInteger(publicModulus), new BigInteger(publicExponent));
ret.remotePublicKey = keyFactory.generatePublic(rsaPublicKeySpec);
}
return ret;
} catch (Exception e) {
return null;
}
}
use of java.security.KeyFactory in project Shuttle by timusus.
the class Security method generatePublicKey.
/**
* Generates a PublicKey instance from a string containing the
* Base64-encoded public key.
*
* @param encodedPublicKey Base64-encoded public key
* @throws IllegalArgumentException if encodedPublicKey is invalid
*/
public static PublicKey generatePublicKey(String encodedPublicKey) {
try {
byte[] decodedKey = Base64.decode(encodedPublicKey);
KeyFactory keyFactory = KeyFactory.getInstance(KEY_FACTORY_ALGORITHM);
return keyFactory.generatePublic(new X509EncodedKeySpec(decodedKey));
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
} catch (InvalidKeySpecException e) {
Log.e(TAG, "Invalid key specification.");
throw new IllegalArgumentException(e);
} catch (Base64DecoderException e) {
Log.e(TAG, "Base64 decoding failed.");
throw new IllegalArgumentException(e);
}
}
use of java.security.KeyFactory in project baker-android by bakerframework.
the class LicenseChecker method generatePublicKey.
/**
* Generates a PublicKey instance from a string containing the
* Base64-encoded public key.
*
* @param encodedPublicKey Base64-encoded public key
* @throws IllegalArgumentException if encodedPublicKey is invalid
*/
private static PublicKey generatePublicKey(String encodedPublicKey) {
try {
byte[] decodedKey = Base64.decode(encodedPublicKey);
KeyFactory keyFactory = KeyFactory.getInstance(KEY_FACTORY_ALGORITHM);
return keyFactory.generatePublic(new X509EncodedKeySpec(decodedKey));
} catch (NoSuchAlgorithmException e) {
// This won't happen in an Android-compatible environment.
throw new RuntimeException(e);
} catch (Base64DecoderException e) {
Log.e(TAG, "Could not decode from Base64.");
throw new IllegalArgumentException(e);
} catch (InvalidKeySpecException e) {
Log.e(TAG, "Invalid key specification.");
throw new IllegalArgumentException(e);
}
}
use of java.security.KeyFactory in project bigbluebutton by bigbluebutton.
the class RSA_SHA1 method getPublicKeyFromPem.
private PublicKey getPublicKeyFromPem(String pem) throws GeneralSecurityException, IOException {
InputStream stream = new ByteArrayInputStream(pem.getBytes("UTF-8"));
PEMReader reader = new PEMReader(stream);
byte[] bytes = reader.getDerBytes();
PublicKey pubKey;
if (PEMReader.PUBLIC_X509_MARKER.equals(reader.getBeginMarker())) {
KeySpec keySpec = new X509EncodedKeySpec(bytes);
KeyFactory fac = KeyFactory.getInstance("RSA");
pubKey = fac.generatePublic(keySpec);
} else if (PEMReader.CERTIFICATE_X509_MARKER.equals(reader.getBeginMarker())) {
pubKey = getPublicKeyFromDerCert(bytes);
} else {
throw new IOException("Invalid PEM fileL: Unknown marker for " + " public key or cert " + reader.getBeginMarker());
}
return pubKey;
}
use of java.security.KeyFactory in project bigbluebutton by bigbluebutton.
the class RSA_SHA1 method getPrivateKeyFromPem.
private PrivateKey getPrivateKeyFromPem(String pem) throws GeneralSecurityException, IOException {
InputStream stream = new ByteArrayInputStream(pem.getBytes("UTF-8"));
PEMReader reader = new PEMReader(stream);
byte[] bytes = reader.getDerBytes();
KeySpec keySpec;
if (PEMReader.PRIVATE_PKCS1_MARKER.equals(reader.getBeginMarker())) {
keySpec = (new PKCS1EncodedKeySpec(bytes)).getKeySpec();
} else if (PEMReader.PRIVATE_PKCS8_MARKER.equals(reader.getBeginMarker())) {
keySpec = new PKCS8EncodedKeySpec(bytes);
} else {
throw new IOException("Invalid PEM file: Unknown marker " + "for private key " + reader.getBeginMarker());
}
KeyFactory fac = KeyFactory.getInstance("RSA");
return fac.generatePrivate(keySpec);
}
Aggregations