Search in sources :

Example 96 with X509EncodedKeySpec

use of java.security.spec.X509EncodedKeySpec in project OpenAttestation by OpenAttestation.

the class RsaUtil method decodeDerPublicKey.

public static PublicKey decodeDerPublicKey(byte[] publicKeyBytes) throws CryptographyException {
    try {
        // throws NoSuchAlgorithmException
        KeyFactory factory = KeyFactory.getInstance("RSA");
        // throws InvalidKeySpecException
        PublicKey publicKey = factory.generatePublic(new X509EncodedKeySpec(publicKeyBytes));
        return publicKey;
    } catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
        throw new CryptographyException(e);
    }
}
Also used : X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) InvalidKeySpecException(java.security.spec.InvalidKeySpecException)

Example 97 with X509EncodedKeySpec

use of java.security.spec.X509EncodedKeySpec in project OpenAttestation by OpenAttestation.

the class X509Util method decodeDerPublicKey.

/**
     * @deprecated use decodeDerPublicKey in RsaUtil instead
     * @param publicKeyBytes
     * @return
     * @throws CryptographyException 
     */
public static PublicKey decodeDerPublicKey(byte[] publicKeyBytes) throws CryptographyException {
    try {
        // throws NoSuchAlgorithmException
        KeyFactory factory = KeyFactory.getInstance("RSA");
        // throws InvalidKeySpecException
        PublicKey publicKey = factory.generatePublic(new X509EncodedKeySpec(publicKeyBytes));
        return publicKey;
    } catch (Exception e) {
        throw new CryptographyException(e);
    }
}
Also used : CryptographyException(com.intel.mtwilson.util.crypto.CryptographyException) PublicKey(java.security.PublicKey) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) KeyFactory(java.security.KeyFactory) CertificateParsingException(java.security.cert.CertificateParsingException) KeyStoreException(java.security.KeyStoreException) UnrecoverableEntryException(java.security.UnrecoverableEntryException) IOException(java.io.IOException) KeyManagementException(java.security.KeyManagementException) CertificateException(java.security.cert.CertificateException) CryptographyException(com.intel.mtwilson.util.crypto.CryptographyException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) CertificateEncodingException(java.security.cert.CertificateEncodingException)

Example 98 with X509EncodedKeySpec

use of java.security.spec.X509EncodedKeySpec in project SeriesGuide by UweTrottmann.

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) {
        Timber.e("Invalid key specification.");
        throw new IllegalArgumentException(e);
    } catch (Base64DecoderException e) {
        Timber.e("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 99 with X509EncodedKeySpec

use of java.security.spec.X509EncodedKeySpec in project platformlayer by platformlayer.

the class RsaUtils method deserializePublicKey.

public static PublicKey deserializePublicKey(byte[] keyData) {
    X509EncodedKeySpec pubKeySpec = new X509EncodedKeySpec(keyData);
    KeyFactory keyFactory = getKeyFactory();
    PublicKey pubKey;
    try {
        pubKey = keyFactory.generatePublic(pubKeySpec);
    } catch (InvalidKeySpecException e) {
        throw new IllegalArgumentException("Error deserializing public key", e);
    }
    return pubKey;
}
Also used : PublicKey(java.security.PublicKey) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) KeyFactory(java.security.KeyFactory)

Example 100 with X509EncodedKeySpec

use of java.security.spec.X509EncodedKeySpec in project xabber-android by redsolution.

the class AccountTable method write.

public long write(Long id, AccountItem accountItem) {
    ConnectionSettings connectionSettings = accountItem.getConnectionSettings();
    ContentValues values = new ContentValues();
    values.put(Fields.PROTOCOL, connectionSettings.getProtocol().name());
    values.put(Fields.CUSTOM, connectionSettings.isCustomHostAndPort() ? 1 : 0);
    values.put(Fields.HOST, connectionSettings.getHost());
    values.put(Fields.PORT, connectionSettings.getPort());
    values.put(Fields.SERVER_NAME, connectionSettings.getServerName());
    values.put(Fields.USER_NAME, connectionSettings.getUserName());
    String password = connectionSettings.getPassword();
    if (!accountItem.isStorePassword()) {
        password = AccountItem.UNDEFINED_PASSWORD;
    }
    values.put(Fields.PASSWORD, password);
    values.put(Fields.RESOURCE, connectionSettings.getResource());
    values.put(Fields.COLOR_INDEX, accountItem.getColorIndex());
    values.put(Fields.PRIORITY, accountItem.getPriority());
    values.put(Fields.STATUS_MODE, accountItem.getRawStatusMode().ordinal());
    values.put(Fields.STATUS_TEXT, accountItem.getStatusText());
    values.put(Fields.ENABLED, accountItem.isEnabled() ? 1 : 0);
    values.put(Fields.SASL_ENABLED, connectionSettings.isSaslEnabled() ? 1 : 0);
    values.put(Fields.TLS_MODE, connectionSettings.getTlsMode().ordinal());
    values.put(Fields.COMPRESSION, connectionSettings.useCompression() ? 1 : 0);
    values.put(Fields.PROXY_TYPE, connectionSettings.getProxyType().ordinal());
    values.put(Fields.PROXY_HOST, connectionSettings.getProxyHost());
    values.put(Fields.PROXY_PORT, connectionSettings.getProxyPort());
    values.put(Fields.PROXY_USER, connectionSettings.getProxyUser());
    values.put(Fields.PROXY_PASSWORD, connectionSettings.getProxyPassword());
    values.put(Fields.SYNCABLE, accountItem.isSyncable() ? 1 : 0);
    values.put(Fields.STORE_PASSWORD, accountItem.isStorePassword() ? 1 : 0);
    final KeyPair keyPair = accountItem.getKeyPair();
    if (keyPair == null) {
        values.putNull(Fields.PUBLIC_KEY);
        values.putNull(Fields.PRIVATE_KEY);
    } else {
        PublicKey publicKey = keyPair.getPublic();
        X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(publicKey.getEncoded());
        PrivateKey privateKey = keyPair.getPrivate();
        PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(privateKey.getEncoded());
        byte[] publicKeyBytes = x509EncodedKeySpec.getEncoded();
        byte[] privateKeyBytes = pkcs8EncodedKeySpec.getEncoded();
        values.put(Fields.PUBLIC_KEY, publicKeyBytes);
        values.put(Fields.PRIVATE_KEY, privateKeyBytes);
    }
    if (accountItem.getLastSync() == null) {
        values.putNull(Fields.LAST_SYNC);
    } else {
        values.put(Fields.LAST_SYNC, accountItem.getLastSync().getTime());
    }
    values.put(Fields.ARCHIVE_MODE, accountItem.getArchiveMode().ordinal());
    SQLiteDatabase db = databaseManager.getWritableDatabase();
    if (id == null) {
        return db.insert(NAME, Fields.USER_NAME, values);
    }
    db.update(NAME, values, Fields._ID + " = ?", new String[] { String.valueOf(id) });
    return id;
}
Also used : ContentValues(android.content.ContentValues) KeyPair(java.security.KeyPair) PrivateKey(java.security.PrivateKey) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) PublicKey(java.security.PublicKey) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) ConnectionSettings(com.xabber.android.data.connection.ConnectionSettings)

Aggregations

X509EncodedKeySpec (java.security.spec.X509EncodedKeySpec)153 KeyFactory (java.security.KeyFactory)106 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)89 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)76 PublicKey (java.security.PublicKey)61 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)42 InvalidKeyException (java.security.InvalidKeyException)29 IOException (java.io.IOException)25 PrivateKey (java.security.PrivateKey)24 RSAPublicKey (java.security.interfaces.RSAPublicKey)18 Signature (java.security.Signature)13 ByteArrayInputStream (java.io.ByteArrayInputStream)12 BigInteger (java.math.BigInteger)11 CertificateException (java.security.cert.CertificateException)10 X509Certificate (java.security.cert.X509Certificate)10 EncodedKeySpec (java.security.spec.EncodedKeySpec)10 SecretKey (javax.crypto.SecretKey)9 ECPublicKey (java.security.interfaces.ECPublicKey)8 EncryptionException (edu.umass.cs.gnscommon.exceptions.client.EncryptionException)7 NoSuchProviderException (java.security.NoSuchProviderException)7