List of usage examples for java.security KeyFactory getInstance
public static KeyFactory getInstance(String algorithm, Provider provider) throws NoSuchAlgorithmException
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:MainClass.java
public static void main(String[] args) throws Exception { Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider()); KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA", "BC"); generator.initialize(128, new SecureRandom()); KeyPair pair = generator.generateKeyPair(); ASN1InputStream aIn = new ASN1InputStream(pair.getPublic().getEncoded()); SubjectPublicKeyInfo info = SubjectPublicKeyInfo.getInstance(aIn.readObject()); System.out.println(ASN1Dump.dumpAsString(info)); System.out.println(ASN1Dump.dumpAsString(info.getPublicKey())); X509EncodedKeySpec x509Spec = new X509EncodedKeySpec(pair.getPublic().getEncoded()); KeyFactory keyFact = KeyFactory.getInstance("RSA", "BC"); PublicKey pubKey = keyFact.generatePublic(x509Spec); System.out.println(pubKey.equals(pair.getPublic())); }
From source file:Main.java
static Key base64ToRsaPublicKey(String b64) { if (b64 != null) { try {//from w w w. j a v a2s . co m byte[] keyBytes = decodeB64(b64); if (keyBytes != null) { KeyFactory keyFactory = KeyFactory.getInstance("RSA", "SC"); EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(keyBytes); return keyFactory.generatePublic(publicKeySpec); } } catch (Exception e) { e.printStackTrace(); } } return null; }
From source file:Main.java
static PublicKey getRsaPublicKey(String n, String e) { BigInteger rsaN = null;//from w w w .j ava 2s . co 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;//from ww w.j a v a2s .com 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 RSAPrivateKey privKeyFromJwk(String jwkp) { RSAPrivateKey privKey = null; try {// w w w. jav a 2 s .co 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"))); 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:com.alliander.osgp.shared.security.CertificateHelper.java
/** * Create private key from private key file on disk * * @param keyPath//from www. j a v a2 s. c o m * path to key * @param keyType * type of key * @return instance of public key * @throws NoSuchAlgorithmException * @throws InvalidKeySpecException * @throws IOException * @throws NoSuchProviderException */ public static PrivateKey createPrivateKey(final String keyPath, final String keyType, final String provider) throws NoSuchAlgorithmException, InvalidKeySpecException, IOException, NoSuchProviderException { final byte[] key = readKeyFromDisk(keyPath); final PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(key); KeyFactory privateKeyFactory; privateKeyFactory = KeyFactory.getInstance(keyType, provider); return privateKeyFactory.generatePrivate(privateKeySpec); }
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:com.alliander.osgp.shared.security.CertificateHelper.java
/** * Create public key from public key file on disk * * @param keyPath//from w w w .jav a 2 s . c om * path to key * @param keyType * type of key * @return instance of public key * @throws NoSuchAlgorithmException * @throws InvalidKeySpecException * @throws IOException * @throws NoSuchProviderException */ public static PublicKey createPublicKey(final String keyPath, final String keyType, final String provider) throws NoSuchAlgorithmException, InvalidKeySpecException, IOException, NoSuchProviderException { final byte[] key = readKeyFromDisk(keyPath); final X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(key); final KeyFactory publicKeyFactory = KeyFactory.getInstance(keyType, provider); return publicKeyFactory.generatePublic(publicKeySpec); }
From source file:cn.mrdear.pay.util.RSAUtils.java
/** * ??//from w w w . ja v a 2 s .co m * * @param encodedKey * ? * @return ? */ public static PrivateKey generatePrivateKey(byte[] encodedKey) { try { KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM, PROVIDER); return keyFactory.generatePrivate(new PKCS8EncodedKeySpec(encodedKey)); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e.getMessage(), e); } catch (InvalidKeySpecException e) { throw new RuntimeException(e.getMessage(), e); } }