Search in sources :

Example 51 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)

Example 52 with RSAPrivateKey

use of java.security.interfaces.RSAPrivateKey in project gocd by gocd.

the class HttpTestUtil method generateKeyPair.

private KeyPair generateKeyPair() {
    try {
        KeyPair seed = KeyPairGenerator.getInstance("RSA", "BC").generateKeyPair();
        RSAPrivateKey privateSeed = (RSAPrivateKey) seed.getPrivate();
        RSAPublicKey publicSeed = (RSAPublicKey) seed.getPublic();
        KeyFactory fact = KeyFactory.getInstance("RSA", "BC");
        RSAPrivateKeySpec privateKeySpec = new RSAPrivateKeySpec(privateSeed.getModulus(), privateSeed.getPrivateExponent());
        RSAPublicKeySpec publicKeySpec = new RSAPublicKeySpec(publicSeed.getModulus(), publicSeed.getPublicExponent());
        return new KeyPair(fact.generatePublic(publicKeySpec), fact.generatePrivate(privateKeySpec));
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : RSAPublicKey(java.security.interfaces.RSAPublicKey) RSAPrivateKeySpec(java.security.spec.RSAPrivateKeySpec) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) RSAPrivateKey(java.security.interfaces.RSAPrivateKey) ServletException(javax.servlet.ServletException) IOException(java.io.IOException)

Example 53 with RSAPrivateKey

use of java.security.interfaces.RSAPrivateKey in project tech by ffyyhh995511.

the class RASUtil method loadPrivateKeyByStr.

/**
 * 私钥字符串转换为私钥对象
 * @param privateKeyStr
 * @return
 * @throws Exception
 */
public static RSAPrivateKey loadPrivateKeyByStr(String privateKeyStr) throws Exception {
    try {
        byte[] buffer = decryptBASE64(privateKeyStr);
        PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(buffer);
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        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) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) BadPaddingException(javax.crypto.BadPaddingException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException)

Example 54 with RSAPrivateKey

use of java.security.interfaces.RSAPrivateKey in project tech by ffyyhh995511.

the class RASUtil method main.

public static void main(String[] args) {
    Map<String, Object> keyMap;
    try {
        keyMap = initKey();
        String publicKey = getPublicKey(keyMap);
        System.out.println("公钥");
        System.out.println(publicKey);
        String privateKey = getPrivateKey(keyMap);
        System.out.println("私钥");
        System.out.println(privateKey);
        String str = "方云鹤测试rsa加密";
        System.out.println("明文数据:" + str);
        RSAPublicKey loadPublicKeyByStr = loadPublicKeyByStr(publicKey);
        byte[] encrypt = encrypt(loadPublicKeyByStr, str.getBytes("UTF-8"));
        String encryptBASE64 = encryptBASE64(encrypt);
        System.out.println("加密后的密文:" + encryptBASE64);
        RSAPrivateKey loadPrivateKeyByStr = loadPrivateKeyByStr(privateKey);
        byte[] decrypt = decrypt(loadPrivateKeyByStr, decryptBASE64(encryptBASE64));
        String a = new String(decrypt, "UTF-8");
        System.out.println("密文解密:" + a);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : RSAPublicKey(java.security.interfaces.RSAPublicKey) RSAPrivateKey(java.security.interfaces.RSAPrivateKey) 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 55 with RSAPrivateKey

use of java.security.interfaces.RSAPrivateKey in project cxf by apache.

the class JwkUtilsTest method testToPrivateRsaKeyWithoutE.

@Test
public void testToPrivateRsaKeyWithoutE() throws Exception {
    RSAPrivateKey privateKey1 = (RSAPrivateKey) KeyManagementUtils.loadPrivateKey("org/apache/cxf/rs/security/jose/jws/alice.jks", "password", "alice", "password", null);
    JsonWebKey jwk1 = JwkUtils.fromRSAPrivateKey(privateKey1, KeyAlgorithm.RSA_OAEP_256.getJwaName());
    assertNotNull(jwk1.getProperty(JsonWebKey.RSA_PUBLIC_EXP));
    jwk1.asMap().remove(JsonWebKey.RSA_PUBLIC_EXP);
    try {
        JwkUtils.toRSAPrivateKey(jwk1);
        fail("JWK without the public exponent can not be converted to RSAPrivateKey");
    } catch (JoseException ex) {
    // expected
    }
}
Also used : JoseException(org.apache.cxf.rs.security.jose.common.JoseException) RSAPrivateKey(java.security.interfaces.RSAPrivateKey) Test(org.junit.Test)

Aggregations

RSAPrivateKey (java.security.interfaces.RSAPrivateKey)58 RSAPublicKey (java.security.interfaces.RSAPublicKey)29 KeyFactory (java.security.KeyFactory)15 InvalidKeyException (java.security.InvalidKeyException)14 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)14 KeyPair (java.security.KeyPair)12 PrivateKey (java.security.PrivateKey)12 PublicKey (java.security.PublicKey)11 RSAPrivateCrtKey (java.security.interfaces.RSAPrivateCrtKey)11 RSAPrivateKeySpec (java.security.spec.RSAPrivateKeySpec)11 KeyPairGenerator (java.security.KeyPairGenerator)9 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)9 BigInteger (java.math.BigInteger)8 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)7 RSAPublicKeySpec (java.security.spec.RSAPublicKeySpec)7 IOException (java.io.IOException)6 Key (java.security.Key)5 RSAPrivateCrtKeySpec (java.security.spec.RSAPrivateCrtKeySpec)5 X509EncodedKeySpec (java.security.spec.X509EncodedKeySpec)5 HashMap (java.util.HashMap)5