Example usage for java.security.spec RSAPrivateKeySpec RSAPrivateKeySpec

List of usage examples for java.security.spec RSAPrivateKeySpec RSAPrivateKeySpec

Introduction

In this page you can find the example usage for java.security.spec RSAPrivateKeySpec RSAPrivateKeySpec.

Prototype

public RSAPrivateKeySpec(BigInteger modulus, BigInteger privateExponent) 

Source Link

Document

Creates a new RSAPrivateKeySpec.

Usage

From source file:com.glaf.core.security.RSAUtils.java

/**
 * ?RSA?/*  ww w  . j a v a 2s . c  o  m*/
 * 
 * @param modulus
 *            
 * @param privateExponent
 *            
 * @return RSA?
 */
public static RSAPrivateKey generateRSAPrivateKey(byte[] modulus, byte[] privateExponent) {
    RSAPrivateKeySpec privateKeySpec = new RSAPrivateKeySpec(new BigInteger(modulus),
            new BigInteger(privateExponent));
    try {
        return (RSAPrivateKey) keyFactory.generatePrivate(privateKeySpec);
    } catch (InvalidKeySpecException ex) {
        LOGGER.error("RSAPrivateKeySpec is unavailable.", ex);
    } catch (Exception ex) {
        LOGGER.error("RSAUtils#KEY_FACTORY is null, can not generate KeyFactory instance.", ex);
    }
    return null;
}

From source file:com.thoughtworks.go.server.util.HttpTestUtil.java

private KeyPair generateKeyPair() {
    try {//from  w  w w .  jav  a 2s. co  m
        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);
    }
}

From source file:XmldapCertsAndKeys.java

public static RSAPrivateKey getXmldapPrivateKey1() throws InvalidKeySpecException, NoSuchAlgorithmException {
    byte[] modulusBytes = { (byte) 0x00, (byte) 0xe2, (byte) 0x94, (byte) 0x9f, (byte) 0xaf, (byte) 0xd0,
            (byte) 0xa9, (byte) 0x36, (byte) 0x63, (byte) 0xfc, (byte) 0x15, (byte) 0xa8, (byte) 0x41,
            (byte) 0x5c, (byte) 0x4d, (byte) 0x14, (byte) 0x8d, (byte) 0x19, (byte) 0xd8, (byte) 0x05,
            (byte) 0xc8, (byte) 0xd5, (byte) 0xac, (byte) 0xf0, (byte) 0xb3, (byte) 0xe4, (byte) 0x72,
            (byte) 0xb8, (byte) 0x97, (byte) 0xa4, (byte) 0xeb, (byte) 0x43, (byte) 0x62, (byte) 0x6b,
            (byte) 0x04, (byte) 0x2b, (byte) 0x14, (byte) 0x02, (byte) 0xa4, (byte) 0xd0, (byte) 0xf8,
            (byte) 0x7e, (byte) 0xfd, (byte) 0x89, (byte) 0xd4, (byte) 0x89, (byte) 0x00, (byte) 0x27,
            (byte) 0xcd, (byte) 0xbb, (byte) 0x13, (byte) 0xfa, (byte) 0x3b, (byte) 0x99, (byte) 0x57,
            (byte) 0x4f, (byte) 0x14, (byte) 0xa5, (byte) 0xa3, (byte) 0xda, (byte) 0xd8, (byte) 0x81,
            (byte) 0x37, (byte) 0x83, (byte) 0x4f, (byte) 0x91, (byte) 0x15, (byte) 0x9b, (byte) 0x4e,
            (byte) 0x0e, (byte) 0x5a, (byte) 0xa8, (byte) 0x18, (byte) 0x61, (byte) 0x9a, (byte) 0x91,
            (byte) 0x23, (byte) 0x9d, (byte) 0xf4, (byte) 0xd3, (byte) 0x67, (byte) 0x0e, (byte) 0xe3,
            (byte) 0x61, (byte) 0xb1, (byte) 0xdb, (byte) 0x57, (byte) 0x25, (byte) 0x68, (byte) 0x10,
            (byte) 0xab, (byte) 0xca, (byte) 0x5a, (byte) 0x33, (byte) 0xd4, (byte) 0x9c, (byte) 0xe2,
            (byte) 0x75, (byte) 0x2e, (byte) 0x7b, (byte) 0x7d, (byte) 0x62, (byte) 0xab, (byte) 0xb2,
            (byte) 0xef, (byte) 0x9a, (byte) 0x34, (byte) 0x86, (byte) 0x48, (byte) 0xbe, (byte) 0x70,
            (byte) 0xf3, (byte) 0x83, (byte) 0x60, (byte) 0x95, (byte) 0x3e, (byte) 0x3c, (byte) 0x01,
            (byte) 0xca, (byte) 0x95, (byte) 0x1a, (byte) 0xbf, (byte) 0xbf, (byte) 0xe6, (byte) 0xfc,
            (byte) 0xbc, (byte) 0x09, (byte) 0xf4, (byte) 0xff };
    byte[] exponentBytes = { (byte) 0x1d, (byte) 0xe6, (byte) 0xf1, (byte) 0x60, (byte) 0x19, (byte) 0x90,
            (byte) 0x8b, (byte) 0x4e, (byte) 0x0c, (byte) 0xb1, (byte) 0xaa, (byte) 0xff, (byte) 0xdd,
            (byte) 0x37, (byte) 0x8a, (byte) 0xf3, (byte) 0xc8, (byte) 0x2a, (byte) 0x5b, (byte) 0x31,
            (byte) 0x13, (byte) 0x09, (byte) 0xfc, (byte) 0xc6, (byte) 0x30, (byte) 0xea, (byte) 0xf6,
            (byte) 0xf3, (byte) 0x84, (byte) 0x5f, (byte) 0x4c, (byte) 0x08, (byte) 0x4c, (byte) 0x09,
            (byte) 0x43, (byte) 0xca, (byte) 0x23, (byte) 0x43, (byte) 0x2f, (byte) 0x14, (byte) 0xec,
            (byte) 0x65, (byte) 0x77, (byte) 0x70, (byte) 0x26, (byte) 0x18, (byte) 0x70, (byte) 0x28,
            (byte) 0x55, (byte) 0x7d, (byte) 0x20, (byte) 0x74, (byte) 0x07, (byte) 0x1b, (byte) 0x9f,
            (byte) 0xa3, (byte) 0x20, (byte) 0xed, (byte) 0x0b, (byte) 0xef, (byte) 0xb0, (byte) 0xb5,
            (byte) 0xeb, (byte) 0xcd, (byte) 0x2f, (byte) 0xcd, (byte) 0x4d, (byte) 0xde, (byte) 0x37,
            (byte) 0xe5, (byte) 0x86, (byte) 0x55, (byte) 0xf2, (byte) 0x34, (byte) 0xe7, (byte) 0xd9,
            (byte) 0xf7, (byte) 0xb3, (byte) 0x45, (byte) 0x2a, (byte) 0x92, (byte) 0x1b, (byte) 0x54,
            (byte) 0x49, (byte) 0x41, (byte) 0x81, (byte) 0xbd, (byte) 0xc0, (byte) 0x63, (byte) 0xd1,
            (byte) 0x86, (byte) 0x45, (byte) 0xe7, (byte) 0xe3, (byte) 0xb3, (byte) 0xf5, (byte) 0x77,
            (byte) 0x5f, (byte) 0x46, (byte) 0x93, (byte) 0x20, (byte) 0x19, (byte) 0x9a, (byte) 0x26,
            (byte) 0x9f, (byte) 0x48, (byte) 0x27, (byte) 0x4b, (byte) 0x93, (byte) 0xa7, (byte) 0x1c,
            (byte) 0xf2, (byte) 0x8a, (byte) 0x3b, (byte) 0xbe, (byte) 0x40, (byte) 0x85, (byte) 0x92,
            (byte) 0x8a, (byte) 0x3c, (byte) 0xfd, (byte) 0xeb, (byte) 0x18, (byte) 0x2e, (byte) 0x04,
            (byte) 0x69, (byte) 0xe5, (byte) 0xa1 };
    BigInteger exponent = new BigInteger(1, exponentBytes);
    BigInteger modulus = new BigInteger(1, modulusBytes);
    RSAPrivateKeySpec ks = new RSAPrivateKeySpec(modulus, exponent);
    KeyFactory kf = KeyFactory.getInstance("RSA");
    return (RSAPrivateKey) kf.generatePrivate(ks);
}

From source file:org.umit.icm.mobile.utils.RSACrypto.java

/**
* Reads an RSA {@link PrivateKey} from disk.
*    //from  w  w w . j av  a 2 s .  co m
*
                             
@param  fileName  An object of the type {@link String}
*                                  
        
@return  {@link PrivateKey}
* 
        
@see         SDCardReadWrite
*/
public static PrivateKey readPrivateKey(String fileName) throws IOException {

    File sdCard = Environment.getExternalStorageDirectory();
    File keyDir = new File(sdCard.getAbsolutePath() + Constants.KEYS_DIR);
    File file = new File(keyDir, fileName);
    InputStream inputStream = new FileInputStream(file.toString());
    ObjectInputStream objInputStream = new ObjectInputStream(new BufferedInputStream(inputStream));
    try {
        BigInteger modulus = (BigInteger) objInputStream.readObject();
        BigInteger exponential = (BigInteger) objInputStream.readObject();
        RSAPrivateKeySpec rsaKeySpec = new RSAPrivateKeySpec(modulus, exponential);
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        return keyFactory.generatePrivate(rsaKeySpec);
    } catch (Exception e) {
        throw new RuntimeException("readPrivateKey exception", e);
    } finally {
        objInputStream.close();
    }

}

From source file:org.umit.icm.mobile.utils.RSACrypto.java

public static PrivateKey generatePrivateKey(BigInteger modulus, BigInteger exponential)
        throws NoSuchAlgorithmException, InvalidKeySpecException {
    RSAPrivateKeySpec rsaKeySpec = new RSAPrivateKeySpec(modulus, exponential);
    KeyFactory keyFactory = KeyFactory.getInstance("RSA");
    return keyFactory.generatePrivate(rsaKeySpec);
}

From source file:org.xdi.oxauth.model.util.JwtUtil.java

public static byte[] getSignatureRS256(byte[] signingInput, RSAPrivateKey rsaPrivateKey)
        throws SignatureException, InvalidKeyException, NoSuchProviderException, InvalidKeySpecException,
        NoSuchAlgorithmException {
    RSAPrivateKeySpec rsaPrivateKeySpec = new RSAPrivateKeySpec(rsaPrivateKey.getModulus(),
            rsaPrivateKey.getPrivateExponent());

    KeyFactory keyFactory = KeyFactory.getInstance("RSA", "BC");
    PrivateKey privateKey = keyFactory.generatePrivate(rsaPrivateKeySpec);

    Signature signature = Signature.getInstance("SHA256withRSA", "BC");
    signature.initSign(privateKey);/*from w ww. j av  a 2s  . c om*/
    signature.update(signingInput);

    return signature.sign();
}

From source file:strat.mining.stratum.proxy.Launcher.java

/**
 * Check that a valid SSl certificate already exists. If not, create a new
 * one.//from   www  .j  a  v a  2  s .  c  o m
 * 
 * @throws Exception
 */
private static void checkCertificate() throws Exception {
    File storeFile = new File(ConfigurationManager.getInstance().getDatabaseDirectory(), KEYSTORE_FILE_NAME);
    KeyStore keyStore = KeyStore.getInstance("JKS");
    if (!storeFile.exists()) {
        LOGGER.info("KeyStore does not exist. Create {}", storeFile.getAbsolutePath());
        storeFile.getParentFile().mkdirs();
        storeFile.createNewFile();
        keyStore.load(null, null);

        LOGGER.info("Generating new SSL certificate.");
        AlgorithmIdentifier sigAlgId = new DefaultSignatureAlgorithmIdentifierFinder().find("SHA256withRSA");
        AlgorithmIdentifier digAlgId = new DefaultDigestAlgorithmIdentifierFinder().find(sigAlgId);

        RSAKeyPairGenerator keyGenerator = new RSAKeyPairGenerator();
        keyGenerator
                .init(new RSAKeyGenerationParameters(BigInteger.valueOf(101), new SecureRandom(), 2048, 14));
        AsymmetricCipherKeyPair keysPair = keyGenerator.generateKeyPair();

        RSAKeyParameters rsaPrivateKey = (RSAKeyParameters) keysPair.getPrivate();
        RSAPrivateKeySpec rsaPrivSpec = new RSAPrivateKeySpec(rsaPrivateKey.getModulus(),
                rsaPrivateKey.getExponent());
        RSAKeyParameters rsaPublicKey = (RSAKeyParameters) keysPair.getPublic();
        RSAPublicKeySpec rsaPublicSpec = new RSAPublicKeySpec(rsaPublicKey.getModulus(),
                rsaPublicKey.getExponent());
        KeyFactory kf = KeyFactory.getInstance("RSA");
        PrivateKey rsaPriv = kf.generatePrivate(rsaPrivSpec);
        PublicKey rsaPub = kf.generatePublic(rsaPublicSpec);

        X500Name issuerDN = new X500Name("CN=localhost, OU=None, O=None, L=None, C=None");
        Integer randomNumber = new SecureRandom().nextInt();
        BigInteger serialNumber = BigInteger.valueOf(randomNumber >= 0 ? randomNumber : randomNumber * -1);
        Date notBefore = new Date(System.currentTimeMillis() - 1000L * 60 * 60 * 24 * 30);
        Date notAfter = new Date(System.currentTimeMillis() + (1000L * 60 * 60 * 24 * 365 * 10));
        X500Name subjectDN = new X500Name("CN=localhost, OU=None, O=None, L=None, C=None");
        byte[] publickeyb = rsaPub.getEncoded();
        ASN1Sequence sequence = (ASN1Sequence) ASN1Primitive.fromByteArray(publickeyb);
        SubjectPublicKeyInfo subPubKeyInfo = new SubjectPublicKeyInfo(sequence);
        X509v3CertificateBuilder v3CertGen = new X509v3CertificateBuilder(issuerDN, serialNumber, notBefore,
                notAfter, subjectDN, subPubKeyInfo);

        ContentSigner contentSigner = new BcRSAContentSignerBuilder(sigAlgId, digAlgId)
                .build(keysPair.getPrivate());
        X509CertificateHolder certificateHolder = v3CertGen.build(contentSigner);

        Certificate certificate = new CertificateFactory()
                .engineGenerateCertificate(new ByteBufferBackedInputStream(
                        ByteBuffer.wrap(certificateHolder.toASN1Structure().getEncoded())));

        LOGGER.info("Certificate generated.");

        keyStore.setKeyEntry(KEYSTORE_KEY_ENTRY_ALIAS, rsaPriv, KEYSTORE_PASSWORD.toCharArray(),
                new java.security.cert.Certificate[] { certificate });

        keyStore.store(new FileOutputStream(storeFile), KEYSTORE_PASSWORD.toCharArray());
    }
}

From source file:org.xdi.oxauth.model.util.JwtUtil.java

public static byte[] getSignatureRS384(byte[] signingInput, RSAPrivateKey rsaPrivateKey)
        throws SignatureException, InvalidKeyException, NoSuchProviderException, InvalidKeySpecException,
        NoSuchAlgorithmException {
    RSAPrivateKeySpec rsaPrivateKeySpec = new RSAPrivateKeySpec(rsaPrivateKey.getModulus(),
            rsaPrivateKey.getPrivateExponent());

    KeyFactory keyFactory = KeyFactory.getInstance("RSA", "BC");
    PrivateKey privateKey = keyFactory.generatePrivate(rsaPrivateKeySpec);

    Signature signature = Signature.getInstance("SHA384withRSA", "BC");
    signature.initSign(privateKey);//from ww w. ja v  a 2  s.co m
    signature.update(signingInput);

    return signature.sign();
}

From source file:org.xdi.oxauth.model.util.JwtUtil.java

public static byte[] getSignatureRS512(byte[] signingInput, RSAPrivateKey rsaPrivateKey)
        throws SignatureException, InvalidKeyException, NoSuchProviderException, InvalidKeySpecException,
        NoSuchAlgorithmException {
    RSAPrivateKeySpec rsaPrivateKeySpec = new RSAPrivateKeySpec(rsaPrivateKey.getModulus(),
            rsaPrivateKey.getPrivateExponent());

    KeyFactory keyFactory = KeyFactory.getInstance("RSA", "BC");
    PrivateKey privateKey = keyFactory.generatePrivate(rsaPrivateKeySpec);

    Signature signature = Signature.getInstance("SHA512withRSA", "BC");
    signature.initSign(privateKey);/*from  ww  w.  j av  a 2 s . c  o  m*/
    signature.update(signingInput);

    return signature.sign();
}

From source file:com.orange.oidc.secproxy_service.KryptoUtils.java

public static RSAPrivateKey privKeyFromJwk(String jwkp) {
    RSAPrivateKey privKey = null;

    try {//from w w w  .  jav  a  2 s  .  c  o m
        JSONObject jk = new JSONObject(jwkp).getJSONArray("keys").getJSONObject(0);

        BigInteger n = new BigInteger(1, decodeB64(jk.getString("n")));
        BigInteger d = new BigInteger(1, decodeB64(jk.getString("d")));
        // BigInteger e = new BigInteger(1, decodeB64(jk.getString("e")));

        // Log.d("privKeyFromJwk","n "+n);
        // Log.d("privKeyFromJwk","d "+d);
        // Log.d("privKeyFromJwk","e "+e);

        RSAPrivateKeySpec privRsaSpec = new RSAPrivateKeySpec(n, d);
        KeyFactory keyfact = KeyFactory.getInstance("RSA", "SC");
        privKey = (RSAPrivateKey) keyfact.generatePrivate(privRsaSpec);
        // Log.d("privKeyFromJwk","priv key length "+privRsaSpec.getModulus().toByteArray().length);

    } catch (Exception e) {
        e.printStackTrace();
    }

    return privKey;
}