Search in sources :

Example 76 with X509EncodedKeySpec

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

the class AccountRealm method setKeyPair.

public void setKeyPair(KeyPair keyPair) {
    if (keyPair == null) {
        publicKeyBytes = null;
        privateKeyBytes = null;
    } else {
        PublicKey publicKey = keyPair.getPublic();
        X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(publicKey.getEncoded());
        PrivateKey privateKey = keyPair.getPrivate();
        PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(privateKey.getEncoded());
        this.publicKeyBytes = x509EncodedKeySpec.getEncoded();
        this.privateKeyBytes = pkcs8EncodedKeySpec.getEncoded();
    }
}
Also used : PrivateKey(java.security.PrivateKey) PublicKey(java.security.PublicKey) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec)

Example 77 with X509EncodedKeySpec

use of java.security.spec.X509EncodedKeySpec in project tech by ffyyhh995511.

the class RASUtil method loadPublicKeyByStr.

/**
 * 公钥字符串转换为公钥对象
 * @param publicKeyStr
 * @return
 * @throws Exception
 */
public static RSAPublicKey loadPublicKeyByStr(String publicKeyStr) throws Exception {
    try {
        byte[] buffer = decryptBASE64(publicKeyStr);
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        X509EncodedKeySpec keySpec = new X509EncodedKeySpec(buffer);
        return (RSAPublicKey) keyFactory.generatePublic(keySpec);
    } catch (NoSuchAlgorithmException e) {
        throw new Exception("无此算法");
    } catch (InvalidKeySpecException e) {
        throw new Exception("公钥非法");
    } catch (NullPointerException e) {
        throw new Exception("公钥数据为空");
    }
}
Also used : RSAPublicKey(java.security.interfaces.RSAPublicKey) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) KeyFactory(java.security.KeyFactory) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) BadPaddingException(javax.crypto.BadPaddingException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException)

Example 78 with X509EncodedKeySpec

use of java.security.spec.X509EncodedKeySpec in project Klyph by jonathangerbaud.

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 79 with X509EncodedKeySpec

use of java.security.spec.X509EncodedKeySpec in project JGroups by belaban.

the class ASYM_ENCRYPT method generatePubKey.

/**
 * Used to reconstitute public key sent in byte form from peer
 */
protected PublicKey generatePubKey(byte[] encodedKey) {
    PublicKey pubKey = null;
    try {
        KeyFactory KeyFac = KeyFactory.getInstance(getAlgorithm(asym_algorithm));
        X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(encodedKey);
        pubKey = KeyFac.generatePublic(x509KeySpec);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return pubKey;
}
Also used : X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec)

Example 80 with X509EncodedKeySpec

use of java.security.spec.X509EncodedKeySpec in project bnd by bndtools.

the class Settings method initKeys.

/*
	 * Initialize the keys.
	 */
private void initKeys() throws Exception {
    check();
    if (privateKey != null)
        return;
    if (data.id == null || data.secret == null) {
        generate();
    } else {
        PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(data.secret);
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        privateKey = keyFactory.generatePrivate(privateKeySpec);
        X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(data.id);
        publicKey = keyFactory.generatePublic(publicKeySpec);
    }
}
Also used : PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) KeyFactory(java.security.KeyFactory)

Aggregations

X509EncodedKeySpec (java.security.spec.X509EncodedKeySpec)162 KeyFactory (java.security.KeyFactory)112 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)93 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)80 PublicKey (java.security.PublicKey)65 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)45 InvalidKeyException (java.security.InvalidKeyException)30 PrivateKey (java.security.PrivateKey)27 IOException (java.io.IOException)26 RSAPublicKey (java.security.interfaces.RSAPublicKey)20 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 KeyPair (java.security.KeyPair)8 ECPublicKey (java.security.interfaces.ECPublicKey)8 EncryptionException (edu.umass.cs.gnscommon.exceptions.client.EncryptionException)7