Search in sources :

Example 81 with KeyFactory

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;
    }
}
Also used : BigInteger(java.math.BigInteger) BEncodedDictionary(org.cyanogenmod.pushsms.bencode.BEncodedDictionary) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) KeyFactory(java.security.KeyFactory)

Example 82 with KeyFactory

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);
    }
}
Also used : X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) KeyFactory(java.security.KeyFactory)

Example 83 with KeyFactory

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);
    }
}
Also used : Base64DecoderException(com.google.android.vending.licensing.util.Base64DecoderException) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) KeyFactory(java.security.KeyFactory)

Example 84 with KeyFactory

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;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) PEMReader(net.oauth.signature.pem.PEMReader) PublicKey(java.security.PublicKey) EncodedKeySpec(java.security.spec.EncodedKeySpec) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) KeySpec(java.security.spec.KeySpec) PKCS1EncodedKeySpec(net.oauth.signature.pem.PKCS1EncodedKeySpec) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) IOException(java.io.IOException) KeyFactory(java.security.KeyFactory)

Example 85 with KeyFactory

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);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) PEMReader(net.oauth.signature.pem.PEMReader) EncodedKeySpec(java.security.spec.EncodedKeySpec) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) KeySpec(java.security.spec.KeySpec) PKCS1EncodedKeySpec(net.oauth.signature.pem.PKCS1EncodedKeySpec) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) IOException(java.io.IOException) PKCS1EncodedKeySpec(net.oauth.signature.pem.PKCS1EncodedKeySpec) KeyFactory(java.security.KeyFactory)

Aggregations

KeyFactory (java.security.KeyFactory)407 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)180 PrivateKey (java.security.PrivateKey)177 PublicKey (java.security.PublicKey)120 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)114 X509EncodedKeySpec (java.security.spec.X509EncodedKeySpec)113 CertificateFactory (java.security.cert.CertificateFactory)103 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)99 ByteArrayInputStream (java.io.ByteArrayInputStream)93 Certificate (java.security.cert.Certificate)89 X509Certificate (java.security.cert.X509Certificate)87 RSAPublicKeySpec (java.security.spec.RSAPublicKeySpec)60 PrivateKeyEntry (java.security.KeyStore.PrivateKeyEntry)59 Entry (java.security.KeyStore.Entry)53 TrustedCertificateEntry (java.security.KeyStore.TrustedCertificateEntry)53 IOException (java.io.IOException)47 BigInteger (java.math.BigInteger)45 RSAPrivateKey (java.security.interfaces.RSAPrivateKey)43 RSAPublicKey (java.security.interfaces.RSAPublicKey)43 Signature (java.security.Signature)40