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:MainClass.java

public static void main(String[] args) throws Exception {
    Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());

    byte[] input = new byte[] { (byte) 0xbe, (byte) 0xef };
    Cipher cipher = Cipher.getInstance("RSA/None/NoPadding", "BC");

    KeyFactory keyFactory = KeyFactory.getInstance("RSA", "BC");
    RSAPublicKeySpec pubKeySpec = new RSAPublicKeySpec(new BigInteger("12345678", 16),
            new BigInteger("11", 16));
    RSAPrivateKeySpec privKeySpec = new RSAPrivateKeySpec(new BigInteger("12345678", 16),
            new BigInteger("12345678", 16));

    RSAPublicKey pubKey = (RSAPublicKey) keyFactory.generatePublic(pubKeySpec);
    RSAPrivateKey privKey = (RSAPrivateKey) keyFactory.generatePrivate(privKeySpec);

    cipher.init(Cipher.ENCRYPT_MODE, pubKey);

    byte[] cipherText = cipher.doFinal(input);
    System.out.println("cipher: " + new String(cipherText));

    cipher.init(Cipher.DECRYPT_MODE, privKey);
    byte[] plainText = cipher.doFinal(cipherText);
    System.out.println("plain : " + new String(plainText));
}

From source file:Main.java

public static Key getRSAKey(String hexModulus, String hexPrivateExponent) throws Exception {
    BigInteger m = new BigInteger(hexModulus, 16);
    BigInteger e = new BigInteger(hexPrivateExponent, 16);
    RSAPrivateKeySpec keySpec = new RSAPrivateKeySpec(m, e);
    KeyFactory keyFactory = KeyFactory.getInstance("RSA");
    Key rsaKey = keyFactory.generatePrivate(keySpec);
    return rsaKey;
}

From source file:Main.java

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

    try {/*from  w ww. j a  v a  2s .  c om*/
        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")));

        RSAPrivateKeySpec privRsaSpec = new RSAPrivateKeySpec(n, d);
        KeyFactory keyfact = KeyFactory.getInstance("RSA", "SC");
        privKey = (RSAPrivateKey) keyfact.generatePrivate(privRsaSpec);
    } catch (Exception e) {
        e.printStackTrace();
    }

    return privKey;
}

From source file:org.xdi.oxauth.model.jws.RSASigner.java

@Override
public String generateSignature(String signingInput) throws SignatureException {
    if (getSignatureAlgorithm() == null) {
        throw new SignatureException("The signature algorithm is null");
    }//w w  w .  j a  v  a2  s .  co  m
    if (rsaPrivateKey == null) {
        throw new SignatureException("The RSA private key is null");
    }
    if (signingInput == null) {
        throw new SignatureException("The signing input is null");
    }

    try {
        RSAPrivateKeySpec rsaPrivateKeySpec = new RSAPrivateKeySpec(rsaPrivateKey.getModulus(),
                rsaPrivateKey.getPrivateExponent());

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

        Signature signature = Signature.getInstance(getSignatureAlgorithm().getAlgorithm(), "BC");
        signature.initSign(privateKey);
        signature.update(signingInput.getBytes(Util.UTF8_STRING_ENCODING));

        return JwtUtil.base64urlencode(signature.sign());
    } catch (InvalidKeySpecException e) {
        throw new SignatureException(e);
    } catch (InvalidKeyException e) {
        throw new SignatureException(e);
    } catch (NoSuchAlgorithmException e) {
        throw new SignatureException(e);
    } catch (NoSuchProviderException e) {
        throw new SignatureException(e);
    } catch (SignatureException e) {
        throw new SignatureException(e);
    } catch (UnsupportedEncodingException e) {
        throw new SignatureException(e);
    } catch (Exception e) {
        throw new SignatureException(e);
    }
}

From source file:org.infoscoop.util.RSAKeyManager.java

public RSAKeyManager(BigInteger modulus, BigInteger exponent)
        throws NoSuchAlgorithmException, NoSuchProviderException, InvalidKeySpecException {

    if (log.isInfoEnabled())
        log.info("### Using RSAProvider is " + keyFactory.getProvider().getClass());

    RSAPrivateKeySpec privateKeySpec = new RSAPrivateKeySpec(modulus, exponent);
    privateKey = (RSAPrivateKey) keyFactory.generatePrivate(privateKeySpec);

    RSAPublicKeySpec publicKeySpec = new RSAPublicKeySpec(modulus, RSAKeyGenParameterSpec.F4);
    publicKey = (RSAPublicKey) keyFactory.generatePublic(publicKeySpec);

}

From source file:de.mpg.escidoc.services.aa.crypto.RSAEncoder.java

public static Key readKeyFromFile(String keyFileName, boolean publ) throws Exception {
    InputStream in = ResourceUtil.getResourceAsStream(keyFileName, RSAEncoder.class.getClassLoader());
    ObjectInputStream oin = new ObjectInputStream(new BufferedInputStream(in));
    try {/*from   www  . j a  va 2  s .  c o m*/
        BigInteger m = (BigInteger) oin.readObject();
        BigInteger e = (BigInteger) oin.readObject();
        KeySpec keySpec;
        if (publ) {
            keySpec = new RSAPublicKeySpec(m, e);
        } else {
            keySpec = new RSAPrivateKeySpec(m, e);
        }
        KeyFactory fact = KeyFactory.getInstance("RSA");
        if (publ) {
            PublicKey pubKey = fact.generatePublic(keySpec);
            return pubKey;
        } else {
            PrivateKey privKey = fact.generatePrivate(keySpec);
            return privKey;
        }
    } catch (Exception e) {
        throw new RuntimeException("Error reading key from file", e);
    } finally {
        oin.close();
    }
}

From source file:org.opentravel.schemacompiler.security.PasswordHelper.java

/**
 * Returns an decryption cipher that is based on the private encryption key file located on the
 * application's classpath./*ww  w .j  av  a 2  s .  c o  m*/
 * 
 * @return Cipher
 * @throws GeneralSecurityException
 *             thrown if encryption key is not valid
 * @throws IOException
 *             thrown if the contents of the private key file cannot be loaded
 */
private static Cipher loadDecryptionCipher() throws GeneralSecurityException, IOException {
    BigInteger[] keyComponents = loadKeyFile(PRIVATE_KEYFILE);
    RSAPrivateKeySpec keySpec = new RSAPrivateKeySpec(keyComponents[0], keyComponents[1]);
    KeyFactory factory = KeyFactory.getInstance(ENCRYPTION_ALGORITHM);
    PrivateKey privateKey = factory.generatePrivate(keySpec);
    Cipher cipher = Cipher.getInstance(CIPHER_TRANSFORMATION);

    cipher.init(Cipher.PRIVATE_KEY, privateKey);
    return cipher;
}

From source file:com.cliqset.magicsig.MagicKey.java

public PrivateKey getPrivateKey() {
    try {//from   w  w  w  . j  a va  2 s. co  m
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        return keyFactory
                .generatePrivate(new RSAPrivateKeySpec(new BigInteger(1, getN()), new BigInteger(1, getD())));
    } catch (NoSuchAlgorithmException e) {
        return null;
    } catch (InvalidKeySpecException ex) {
        return null;
    }
}

From source file:com.github.aynu.yukar.framework.util.SecurityHelper.java

/**
 * RSA???//from   ww  w . ja  va 2s  .c  o  m
 * <dl>
 * <dt>?
 * <dd>RSA?????????????
 * </dl>
 * @param modulus 
 * @param exponent ??
 * @return RSA?
 */
public static RSAPrivateKey createPrivateKey(final BigInteger modulus, final BigInteger exponent) {
    try {
        final KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        return (RSAPrivateKey) keyFactory.generatePrivate(new RSAPrivateKeySpec(modulus, exponent));
    } catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
        throw new StandardRuntimeException(e);
    }
}

From source file:license.regist.ProjectInfo.java

private static PrivateKey readPrivateKeyFromFile() throws Exception {
    ObjectInputStream oin = new ObjectInputStream(
            new BufferedInputStream(ProjectInfo.class.getClassLoader().getResourceAsStream("private.key")));
    try {/*from   w w  w .  j  a  v  a 2 s  .  co  m*/
        BigInteger m = (BigInteger) oin.readObject();
        BigInteger e = (BigInteger) oin.readObject();
        RSAPrivateKeySpec keySpec = new RSAPrivateKeySpec(m, e);
        KeyFactory fact = KeyFactory.getInstance("RSA");
        return fact.generatePrivate(keySpec);
    } finally {
        oin.close();
    }
}