Example usage for java.security.spec RSAPublicKeySpec RSAPublicKeySpec

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

Introduction

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

Prototype

public RSAPublicKeySpec(BigInteger modulus, BigInteger publicExponent) 

Source Link

Document

Creates a new RSAPublicKeySpec.

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 PublicKey getPublicKey(String n, String publicExponent) {
    KeySpec publicKeySpec = new RSAPublicKeySpec(new BigInteger(n, 16), new BigInteger(publicExponent, 16));

    KeyFactory factory = null;//ww w  .j  a  v  a2s  .co  m
    PublicKey publicKey = null;
    try {
        factory = KeyFactory.getInstance(KEY_ALGORITHM);
    } catch (NoSuchAlgorithmException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return null;
    }

    try {
        publicKey = factory.generatePublic(publicKeySpec);
    } catch (InvalidKeySpecException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return null;
    }

    return publicKey;
}

From source file:Main.java

public static PrivateKey getPrivateKey(String modulus, String privateExponent)
        throws NoSuchAlgorithmException, InvalidKeySpecException {
    BigInteger bigIntModulus = new BigInteger(modulus);
    BigInteger bigIntPrivateExponent = new BigInteger(privateExponent);
    RSAPublicKeySpec keySpec = new RSAPublicKeySpec(bigIntModulus, bigIntPrivateExponent);
    KeyFactory keyFactory = KeyFactory.getInstance(RSA);
    PrivateKey privateKey = keyFactory.generatePrivate(keySpec);
    return privateKey;
}

From source file:Main.java

static PublicKey getRsaPublicKey(String n, String e) {
    BigInteger rsaN = null;//w  ww  .  j  av  a  2 s . c  o  m
    BigInteger rsaE = null;

    try {
        rsaN = new BigInteger(n);
        rsaE = new BigInteger(e);
    } catch (Exception ex) {
        ex.printStackTrace();
    }

    RSAPublicKeySpec pubRsaSpec = new RSAPublicKeySpec(rsaN, rsaE);
    try {
        KeyFactory keyfact = KeyFactory.getInstance("RSA", "SC");
        PublicKey pk = keyfact.generatePublic(pubRsaSpec);
        Log.d("getRsaPublicKey", "pubRsaKey OK " + pk.getFormat());
        return pk;
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static PublicKey pubKeyFromJwk(String jwkp) {
    PublicKey pubKey = null;//  ww  w  .  j a  v  a2s.  co  m

    try {
        JSONObject jk = new JSONObject(jwkp).getJSONArray("keys").getJSONObject(0);

        BigInteger n = new BigInteger(1, decodeB64(jk.getString("n")));
        BigInteger e = new BigInteger(1, decodeB64(jk.getString("e")));
        RSAPublicKeySpec pubRsaSpec = new RSAPublicKeySpec(n, e);
        KeyFactory keyfact = KeyFactory.getInstance("RSA", "SC");
        pubKey = keyfact.generatePublic(pubRsaSpec);
    } catch (Exception e) {
        e.printStackTrace();
    }

    return pubKey;
}

From source file:Main.java

public static byte[] getRSAEncryptedData(byte[] dataWithHash) {
    BigInteger modulus = new BigInteger(
            "C150023E2F70DB7985DED064759CFECF0AF328E69A41DAF4D6F01B538135A6F91F8F8B2A0EC9BA9720CE352EFCF6C5680FFC424BD634864902DE0B4BD6D49F4E580230E3AE97D95C8B19442B3C0A10D8F5633FECEDD6926A7F6DAB0DDB7D457F9EA81B8465FCD6FFFEED114011DF91C059CAEDAF97625F6C96ECC74725556934EF781D866B34F011FCE4D835A090196E9A5F0E4449AF7EB697DDB9076494CA5F81104A305B6DD27665722C46B60E5DF680FB16B210607EF217652E60236C255F6A28315F4083A96791D7214BF64C1DF4FD0DB1944FB26A2A57031B32EEE64AD15A8BA68885CDE74A5BFC920F6ABF59BA5C75506373E7130F9042DA922179251F",
            16);/*from w ww  .j  a  v a  2s. co m*/
    BigInteger pubExp = new BigInteger("010001", 16);

    KeyFactory keyFactory = null;
    try {
        keyFactory = KeyFactory.getInstance("RSA");
        RSAPublicKeySpec pubKeySpec = new RSAPublicKeySpec(modulus, pubExp);
        RSAPublicKey key = (RSAPublicKey) keyFactory.generatePublic(pubKeySpec);

        Cipher cipher = Cipher.getInstance("RSA/ECB/NoPadding");
        cipher.init(Cipher.ENCRYPT_MODE, key);
        byte[] cipherData = cipher.doFinal(dataWithHash);
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
    } catch (IllegalBlockSizeException e) {
        e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
    } catch (InvalidKeyException e) {
        e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
    } catch (BadPaddingException e) {
        e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
    } catch (InvalidKeySpecException e) {
        e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
    } catch (NoSuchPaddingException e) {
        e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
    }
    return null;
}

From source file:se.curity.examples.oauth.jwt.RsaPublicKeyCreator.java

public static PublicKey createPublicKey(String modulus, String exponent)
        throws InvalidKeySpecException, NoSuchAlgorithmException {
    Base64 decoder = new Base64(true);
    BigInteger bigModulus = new BigInteger(1, decoder.decode(modulus));
    BigInteger bigExponent = new BigInteger(1, decoder.decode(exponent));
    RSAPublicKeySpec publicKeySpec = new RSAPublicKeySpec(bigModulus, bigExponent);
    KeyFactory keyFactory = KeyFactory.getInstance("RSA");
    PublicKey publicKey = keyFactory.generatePublic(publicKeySpec);

    return publicKey;
}

From source file:Main.java

public static RSAPublicKeySpec getRecipientsPublicKey(String contactNum, Context context) {
    Log.w(TAG, "retrieving public key for contact " + contactNum);
    SharedPreferences prefs = context.getSharedPreferences(contactNum, Context.MODE_PRIVATE);

    String pubMod = prefs.getString(PREF_PUBLIC_MOD, DEFAULT_PREF);
    String pubExp = prefs.getString(PREF_PUBLIC_EXP, DEFAULT_PREF);
    Log.w(TAG, "the public modulus is " + pubMod + " and exponent is " + pubExp + " for " + contactNum);
    // String recipient = prefs.getString(PREF_RECIPIENT_NUM, DEFAULT_PREF);
    if (!pubMod.isEmpty() && !pubExp.isEmpty()) {
        Log.i(TAG, "great! public key found for " + contactNum + " with modulus " + pubMod + " and exponent "
                + pubExp);/*  w  w w.  ja v  a 2 s . c  o  m*/
        byte[] pubModBA = Base64.decode(pubMod, Base64.DEFAULT);
        byte[] pubExpBA = Base64.decode(pubExp, Base64.DEFAULT);
        BigInteger pubModBI = new BigInteger(1, pubModBA); // 1 is added as
        // an attempt to
        // deal with
        // com.android.org.bouncycastle.crypto.DataLengthException:
        // input too
        // large for RSA
        // cipher
        BigInteger pubExpBI = new BigInteger(1, pubExpBA);
        Log.i(TAG, "public modulus is " + pubModBI + " and public exponent is " + pubExpBI + " in base 256 "
                + pubModBA + " " + pubExpBA);

        // do I need to catch any exception for the following?
        RSAPublicKeySpec pubKeySpec = new RSAPublicKeySpec(pubModBI, pubExpBI);
        // X509EncodedKeySpec publicKeySpec = new
        // X509EncodedKeySpec(encodedKey);

        return pubKeySpec;
    }
    Log.w(TAG, "recipient's public key not found");
    return null;

}

From source file:com.cloud.utils.crypt.RSAHelper.java

private static RSAPublicKey readKey(String key) throws Exception {
    byte[] encKey = Base64.decodeBase64(key.split(" ")[1]);
    DataInputStream dis = new DataInputStream(new ByteArrayInputStream(encKey));

    byte[] header = readElement(dis);
    String pubKeyFormat = new String(header);
    if (!pubKeyFormat.equals("ssh-rsa"))
        throw new RuntimeException("Unsupported format");

    byte[] publicExponent = readElement(dis);
    byte[] modulus = readElement(dis);

    KeySpec spec = new RSAPublicKeySpec(new BigInteger(modulus), new BigInteger(publicExponent));
    KeyFactory keyFactory = KeyFactory.getInstance("RSA", BouncyCastleProvider.PROVIDER_NAME);
    RSAPublicKey pubKey = (RSAPublicKey) keyFactory.generatePublic(spec);

    return pubKey;
}

From source file:license.rsa.WakeRSA.java

/**
 * ?/*  ww w.  j  ava 2 s .  c o  m*/
 * 
 * @param keyFileName
 * @return
 * @throws Exception
 */
static PublicKey readPublicKeyFromFile() throws Exception {
    ObjectInputStream oin = new ObjectInputStream(new ByteArrayInputStream(KeyData.publicKey));
    try {
        BigInteger m = (BigInteger) oin.readObject();
        BigInteger e = (BigInteger) oin.readObject();
        System.out.println();
        System.out.println();
        System.out.println("=======mmm=====" + m);
        System.out.println("=======eeee=====" + e);
        System.out.println();
        System.out.println();
        RSAPublicKeySpec keySpec = new RSAPublicKeySpec(m, e);
        KeyFactory fact = KeyFactory.getInstance("RSA");
        return fact.generatePublic(keySpec);
    } finally {
        oin.close();
    }
}