Search in sources :

Example 71 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 72 with RSAPrivateKeySpec

use of java.security.spec.RSAPrivateKeySpec 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 73 with RSAPrivateKeySpec

use of java.security.spec.RSAPrivateKeySpec in project dble by actiontech.

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 not support Private key encryption, public key decryption
        // so fake an PrivateKey for it
        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) RSAPublicKey(java.security.interfaces.RSAPublicKey) RSAPrivateKey(java.security.interfaces.RSAPrivateKey)

Example 74 with RSAPrivateKeySpec

use of java.security.spec.RSAPrivateKeySpec in project jmulticard by ctt-gob-es.

the class EncodedPrinter method printPrivate.

/**
 * Imprime la codificación de una clave privada RSA.
 * @throws Exception en cualquier error.
 */
@SuppressWarnings("static-method")
@Test
public void printPrivate() throws Exception {
    final RSAPrivateKeySpec privateSpec = new RSAPrivateKeySpec(new BigInteger(N), new BigInteger(D));
    // $NON-NLS-1$
    final KeyFactory factory = KeyFactory.getInstance("RSA");
    final PrivateKey priv = factory.generatePrivate(privateSpec);
    final byte[] enc = priv.getEncoded();
    System.out.println(// $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
    "(byte) 0x" + HexUtils.hexify(enc, true).replace("\n", "-").replace("-", ", (byte) 0x"));
}
Also used : PrivateKey(java.security.PrivateKey) RSAPrivateKeySpec(java.security.spec.RSAPrivateKeySpec) BigInteger(java.math.BigInteger) KeyFactory(java.security.KeyFactory) Test(org.junit.Test)

Example 75 with RSAPrivateKeySpec

use of java.security.spec.RSAPrivateKeySpec in project android2 by aqi00.

the class RSAUtil method getPrivateKey.

// 使用N、d值还原私钥
private static PrivateKey getPrivateKey(String modulus, String privateExponent, int radix) throws NoSuchAlgorithmException, InvalidKeySpecException {
    BigInteger bigIntModulus = new BigInteger(modulus, radix);
    BigInteger bigIntPrivateExponent = new BigInteger(privateExponent, radix);
    RSAPrivateKeySpec keySpec = new RSAPrivateKeySpec(bigIntModulus, bigIntPrivateExponent);
    KeyFactory keyFactory = KeyFactory.getInstance("RSA");
    PrivateKey privateKey = keyFactory.generatePrivate(keySpec);
    return privateKey;
}
Also used : RSAPrivateKey(java.security.interfaces.RSAPrivateKey) PrivateKey(java.security.PrivateKey) RSAPrivateKeySpec(java.security.spec.RSAPrivateKeySpec) BigInteger(java.math.BigInteger) KeyFactory(java.security.KeyFactory)

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