Search in sources :

Example 26 with RSAPrivateKeySpec

use of java.security.spec.RSAPrivateKeySpec in project j2objc by google.

the class IosRSAKeyFactory method engineTranslateKey.

@Override
protected Key engineTranslateKey(Key key) throws InvalidKeyException {
    if (key == null) {
        throw new InvalidKeyException("key == null");
    }
    if ((key instanceof IosRSAKey.IosRSAPublicKey) || (key instanceof IosRSAKey.IosRSAPrivateKey)) {
        return key;
    } else if (key instanceof RSAPublicKey) {
        RSAPublicKey rsaKey = (RSAPublicKey) key;
        try {
            return engineGeneratePublic(new RSAPublicKeySpec(rsaKey.getModulus(), rsaKey.getPublicExponent()));
        } catch (InvalidKeySpecException e) {
            throw new InvalidKeyException(e);
        }
    } else if (key instanceof RSAPrivateCrtKey) {
        RSAPrivateCrtKey rsaKey = (RSAPrivateCrtKey) key;
        BigInteger modulus = rsaKey.getModulus();
        BigInteger publicExponent = rsaKey.getPublicExponent();
        BigInteger privateExponent = rsaKey.getPrivateExponent();
        BigInteger primeP = rsaKey.getPrimeP();
        BigInteger primeQ = rsaKey.getPrimeQ();
        BigInteger primeExponentP = rsaKey.getPrimeExponentP();
        BigInteger primeExponentQ = rsaKey.getPrimeExponentQ();
        BigInteger crtCoefficient = rsaKey.getCrtCoefficient();
        try {
            return engineGeneratePrivate(new RSAPrivateCrtKeySpec(modulus, publicExponent, privateExponent, primeP, primeQ, primeExponentP, primeExponentQ, crtCoefficient));
        } catch (InvalidKeySpecException e) {
            throw new InvalidKeyException(e);
        }
    } else if (key instanceof RSAPrivateKey) {
        RSAPrivateKey rsaKey = (RSAPrivateKey) key;
        BigInteger modulus = rsaKey.getModulus();
        BigInteger privateExponent = rsaKey.getPrivateExponent();
        try {
            return engineGeneratePrivate(new RSAPrivateKeySpec(modulus, privateExponent));
        } catch (InvalidKeySpecException e) {
            throw new InvalidKeyException(e);
        }
    } else if ((key instanceof PrivateKey) && ("PKCS#8".equals(key.getFormat()))) {
        byte[] encoded = key.getEncoded();
        if (encoded == null) {
            throw new InvalidKeyException("Key does not support encoding");
        }
        try {
            return engineGeneratePrivate(new PKCS8EncodedKeySpec(encoded));
        } catch (InvalidKeySpecException e) {
            throw new InvalidKeyException(e);
        }
    } else if ((key instanceof PublicKey) && ("X.509".equals(key.getFormat()))) {
        byte[] encoded = key.getEncoded();
        if (encoded == null) {
            throw new InvalidKeyException("Key does not support encoding");
        }
        try {
            return engineGeneratePublic(new X509EncodedKeySpec(encoded));
        } catch (InvalidKeySpecException e) {
            throw new InvalidKeyException(e);
        }
    } else {
        throw new InvalidKeyException("Key must be an RSA public or private key; was " + key.getClass().getName());
    }
}
Also used : RSAPrivateCrtKeySpec(java.security.spec.RSAPrivateCrtKeySpec) RSAPrivateCrtKey(java.security.interfaces.RSAPrivateCrtKey) RSAPrivateKey(java.security.interfaces.RSAPrivateKey) PrivateKey(java.security.PrivateKey) PublicKey(java.security.PublicKey) RSAPublicKey(java.security.interfaces.RSAPublicKey) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) InvalidKeyException(java.security.InvalidKeyException) RSAPublicKey(java.security.interfaces.RSAPublicKey) RSAPrivateKeySpec(java.security.spec.RSAPrivateKeySpec) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) BigInteger(java.math.BigInteger) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) RSAPrivateKey(java.security.interfaces.RSAPrivateKey)

Example 27 with RSAPrivateKeySpec

use of java.security.spec.RSAPrivateKeySpec in project j2objc by google.

the class RSAKeyTest method test_getModulus.

/**
     * java.security.interfaces.RSAKey
     * #getModulus()
     * test covers following use cases
     *   Case 1: check private key
     *   Case 2: check public key
     */
public void test_getModulus() throws Exception {
    KeyFactory gen = KeyFactory.getInstance("RSA");
    final BigInteger n = BigInteger.valueOf(3233);
    final BigInteger d = BigInteger.valueOf(2753);
    final BigInteger e = BigInteger.valueOf(17);
    RSAKey key = null;
    // Case 1: check private key
    key = (RSAKey) gen.generatePrivate(new RSAPrivateKeySpec(n, d));
    assertEquals("invalid modulus", n, key.getModulus());
    // Case 2: check public key
    key = (RSAKey) gen.generatePublic(new RSAPublicKeySpec(n, e));
    assertEquals("invalid modulus", n, key.getModulus());
}
Also used : RSAKey(java.security.interfaces.RSAKey) RSAPrivateKeySpec(java.security.spec.RSAPrivateKeySpec) BigInteger(java.math.BigInteger) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) KeyFactory(java.security.KeyFactory)

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

Example 29 with RSAPrivateKeySpec

use of java.security.spec.RSAPrivateKeySpec in project remusic by aa112901.

the class RSAUtils method getPrivateKey.

/**
     * 使用模和指数生成RSA私钥
     * 注意:【此代码用了默认补位方式,为RSA/None/PKCS1Padding,不同JDK默认的补位方式可能不同,如Android默认是RSA
     * /None/NoPadding】
     *
     * @param modulus  模
     * @param exponent 指数
     * @return
     */
public static RSAPrivateKey getPrivateKey(String modulus, String exponent) {
    try {
        BigInteger b1 = new BigInteger(modulus);
        BigInteger b2 = new BigInteger(exponent);
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        RSAPrivateKeySpec keySpec = new RSAPrivateKeySpec(b1, b2);
        return (RSAPrivateKey) keyFactory.generatePrivate(keySpec);
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}
Also used : RSAPrivateKeySpec(java.security.spec.RSAPrivateKeySpec) BigInteger(java.math.BigInteger) RSAPrivateKey(java.security.interfaces.RSAPrivateKey) KeyFactory(java.security.KeyFactory) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 30 with RSAPrivateKeySpec

use of java.security.spec.RSAPrivateKeySpec in project Mycat-Server by MyCATApache.

the class DecryptUtil method decrypt.

public static String decrypt(PublicKey publicKey, String cipherText) throws Exception {
    Cipher cipher = Cipher.getInstance("RSA");
    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)52 KeyFactory (java.security.KeyFactory)31 PrivateKey (java.security.PrivateKey)28 RSAPrivateKey (java.security.interfaces.RSAPrivateKey)23 RSAPublicKeySpec (java.security.spec.RSAPublicKeySpec)17 PublicKey (java.security.PublicKey)16 BigInteger (java.math.BigInteger)11 Signature (java.security.Signature)10 Cipher (javax.crypto.Cipher)10 RSAPublicKey (java.security.interfaces.RSAPublicKey)9 SecretKeyFactory (javax.crypto.SecretKeyFactory)9 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)8 KeySpec (java.security.spec.KeySpec)7 RSAPrivateCrtKeySpec (java.security.spec.RSAPrivateCrtKeySpec)7 InvalidKeyException (java.security.InvalidKeyException)5 RSAPrivateCrtKey (java.security.interfaces.RSAPrivateCrtKey)5 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)5 IOException (java.io.IOException)4 KeyPairGenerator (java.security.KeyPairGenerator)4 X509EncodedKeySpec (java.security.spec.X509EncodedKeySpec)4