Search in sources :

Example 46 with RSAPrivateKey

use of java.security.interfaces.RSAPrivateKey in project JustAndroid by chinaltz.

the class AbRsa method loadPrivateKey.

/**
     * 从字符串中加载私钥

     * 加载时使用的是PKCS8EncodedKeySpec(PKCS#8编码的Key指令)。
     *
     * @param privateKey
     * @return
     * @throws Exception
     */
public static PrivateKey loadPrivateKey(String privateKey) throws Exception {
    try {
        byte[] buffer = AbBase64.decode(privateKey);
        // X509EncodedKeySpec keySpec = new X509EncodedKeySpec(buffer);
        PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(buffer);
        KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM);
        return (RSAPrivateKey) keyFactory.generatePrivate(keySpec);
    } catch (NoSuchAlgorithmException e) {
        throw new Exception("无此算法");
    } catch (InvalidKeySpecException e) {
        throw new Exception("私钥非法");
    } catch (NullPointerException e) {
        throw new Exception("私钥数据为空");
    }
}
Also used : PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) RSAPrivateKey(java.security.interfaces.RSAPrivateKey) KeyFactory(java.security.KeyFactory) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Aggregations

RSAPrivateKey (java.security.interfaces.RSAPrivateKey)46 RSAPublicKey (java.security.interfaces.RSAPublicKey)24 KeyFactory (java.security.KeyFactory)13 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)13 InvalidKeyException (java.security.InvalidKeyException)12 PrivateKey (java.security.PrivateKey)12 KeyPair (java.security.KeyPair)11 PublicKey (java.security.PublicKey)11 RSAPrivateCrtKey (java.security.interfaces.RSAPrivateCrtKey)11 RSAPrivateKeySpec (java.security.spec.RSAPrivateKeySpec)9 KeyPairGenerator (java.security.KeyPairGenerator)8 RSAPublicKeySpec (java.security.spec.RSAPublicKeySpec)7 BigInteger (java.math.BigInteger)6 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)6 IOException (java.io.IOException)5 Key (java.security.Key)5 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)5 RSAPrivateCrtKeySpec (java.security.spec.RSAPrivateCrtKeySpec)5 X509EncodedKeySpec (java.security.spec.X509EncodedKeySpec)5 Signature (java.security.Signature)4