Search in sources :

Example 76 with RSAPrivateKeySpec

use of java.security.spec.RSAPrivateKeySpec in project santuario-java by apache.

the class TestUtils method getPrivateKey.

public static PrivateKey getPrivateKey(String algo, int keysize) throws InvalidKeySpecException, NoSuchAlgorithmException {
    KeyFactory kf = KeyFactory.getInstance(algo);
    KeySpec kspec;
    if (algo.equalsIgnoreCase("DSA")) {
        if (keysize == 1024) {
            kspec = new DSAPrivateKeySpec(new BigInteger(DSA_X), new BigInteger(DSA_P), new BigInteger(DSA_Q), new BigInteger(DSA_G));
        } else if (keysize == 2048) {
            kspec = new DSAPrivateKeySpec(new BigInteger(DSA_2048_X), new BigInteger(DSA_2048_P), new BigInteger(DSA_2048_Q), new BigInteger(DSA_2048_G));
        } else {
            throw new RuntimeException("Unsupported keysize:" + keysize);
        }
    } else if (algo.equalsIgnoreCase("RSA")) {
        if (keysize == 512) {
            kspec = new RSAPrivateKeySpec(new BigInteger(RSA_MOD), new BigInteger(RSA_PRIV));
        } else {
            throw new RuntimeException("Unsupported keysize:" + keysize);
        }
    } else {
        throw new RuntimeException("Unsupported key algorithm " + algo);
    }
    return kf.generatePrivate(kspec);
}
Also used : DSAPrivateKeySpec(java.security.spec.DSAPrivateKeySpec) RSAPrivateKeySpec(java.security.spec.RSAPrivateKeySpec) KeySpec(java.security.spec.KeySpec) DSAPrivateKeySpec(java.security.spec.DSAPrivateKeySpec) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) DSAPublicKeySpec(java.security.spec.DSAPublicKeySpec) RSAPrivateKeySpec(java.security.spec.RSAPrivateKeySpec) BigInteger(java.math.BigInteger) KeyFactory(java.security.KeyFactory)

Example 77 with RSAPrivateKeySpec

use of java.security.spec.RSAPrivateKeySpec in project i2p.i2p by i2p.

the class SigUtil method toJavaRSAKey.

/**
 *  As of 0.9.31, if pk is a RSASigningPrivateCrtKey,
 *  this will return a RSAPrivateCrtKey.
 */
public static RSAPrivateKey toJavaRSAKey(SigningPrivateKey pk) throws GeneralSecurityException {
    if (pk instanceof RSASigningPrivateCrtKey)
        return ((RSASigningPrivateCrtKey) pk).toJavaKey();
    KeyFactory kf = KeyFactory.getInstance("RSA");
    // private key is modulus (pubkey) + exponent
    BigInteger[] nd = split(pk.getData());
    // modulus exponent
    KeySpec ks = new RSAPrivateKeySpec(nd[0], nd[1]);
    return (RSAPrivateKey) kf.generatePrivate(ks);
}
Also used : RSAPrivateKeySpec(java.security.spec.RSAPrivateKeySpec) ECPublicKeySpec(java.security.spec.ECPublicKeySpec) EdDSAPublicKeySpec(net.i2p.crypto.eddsa.spec.EdDSAPublicKeySpec) ECPrivateKeySpec(java.security.spec.ECPrivateKeySpec) KeySpec(java.security.spec.KeySpec) DSAPrivateKeySpec(java.security.spec.DSAPrivateKeySpec) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) EdDSAPrivateKeySpec(net.i2p.crypto.eddsa.spec.EdDSAPrivateKeySpec) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) DSAPublicKeySpec(java.security.spec.DSAPublicKeySpec) RSAPrivateKeySpec(java.security.spec.RSAPrivateKeySpec) BigInteger(java.math.BigInteger) NativeBigInteger(net.i2p.util.NativeBigInteger) RSAPrivateKey(java.security.interfaces.RSAPrivateKey) KeyFactory(java.security.KeyFactory)

Example 78 with RSAPrivateKeySpec

use of java.security.spec.RSAPrivateKeySpec in project druid by alibaba.

the class ConfigTools method decrypt.

public static String decrypt(PublicKey publicKey, String cipherText) throws Exception {
    Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
    try {
        cipher.init(Cipher.DECRYPT_MODE, publicKey);
    } catch (InvalidKeyException e) {
        // 因为 IBM JDK 不支持私钥加密, 公钥解密, 所以要反转公私钥
        // 也就是说对于解密, 可以通过公钥的参数伪造一个私钥对象欺骗 IBM JDK
        RSAPublicKey rsaPublicKey = (RSAPublicKey) publicKey;
        RSAPrivateKeySpec spec = new RSAPrivateKeySpec(rsaPublicKey.getModulus(), rsaPublicKey.getPublicExponent());
        Key fakePrivateKey = KeyFactory.getInstance("RSA").generatePrivate(spec);
        // It is a stateful object. so we need to get new one.
        cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.DECRYPT_MODE, fakePrivateKey);
    }
    if (cipherText == null || cipherText.length() == 0) {
        return cipherText;
    }
    byte[] cipherBytes = Base64.base64ToByteArray(cipherText);
    byte[] plainBytes = cipher.doFinal(cipherBytes);
    return new String(plainBytes);
}
Also used : RSAPublicKey(java.security.interfaces.RSAPublicKey) RSAPrivateKeySpec(java.security.spec.RSAPrivateKeySpec) Cipher(javax.crypto.Cipher) InvalidKeyException(java.security.InvalidKeyException) RSAPrivateKey(java.security.interfaces.RSAPrivateKey) PublicKey(java.security.PublicKey) Key(java.security.Key) RSAPublicKey(java.security.interfaces.RSAPublicKey) PrivateKey(java.security.PrivateKey)

Aggregations

RSAPrivateKeySpec (java.security.spec.RSAPrivateKeySpec)78 KeyFactory (java.security.KeyFactory)49 PrivateKey (java.security.PrivateKey)41 RSAPrivateKey (java.security.interfaces.RSAPrivateKey)30 RSAPublicKeySpec (java.security.spec.RSAPublicKeySpec)27 BigInteger (java.math.BigInteger)25 PublicKey (java.security.PublicKey)19 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)17 InvalidKeyException (java.security.InvalidKeyException)14 Cipher (javax.crypto.Cipher)14 RSAPublicKey (java.security.interfaces.RSAPublicKey)13 Signature (java.security.Signature)12 KeyPair (java.security.KeyPair)10 KeySpec (java.security.spec.KeySpec)10 SecretKeyFactory (javax.crypto.SecretKeyFactory)9 IOException (java.io.IOException)8 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)8 SignatureException (java.security.SignatureException)8 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)7 SecureRandom (java.security.SecureRandom)6