List of usage examples for java.security KeyFactory generatePublic
public final PublicKey generatePublic(KeySpec keySpec) throws InvalidKeySpecException
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.completetrsst.crypto.Common.java
/** * Converts a X509-encoded EC key to a PublicKey. *//*from w ww . java2s . co m*/ public static PublicKey toPublicKeyFromX509(String stored) throws GeneralSecurityException { KeyFactory factory = KeyFactory.getInstance("EC"); byte[] data = Base64.decodeBase64(stored); X509EncodedKeySpec spec = new X509EncodedKeySpec(data); return factory.generatePublic(spec); }
From source file:Main.java
static Key base64ToRsaPublicKey(String b64) { if (b64 != null) { try {/*from w w w .j a va2s .c om*/ 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:pl.kotcrab.crypto.CryptoUtils.java
/** Generates PublicKey from encoded bytes of X509EncodedKeySpec. Bytes must be in Base64 string. * @param base64 Key from encoded bytes of {@link X509EncodedKeySpec#getEncoded()}. Encoded in Base64 string. * @return created public key *//*from ww w . j a va 2s .com*/ public static PublicKey getRSAPublicKeyFromString64(String base64) { try { X509EncodedKeySpec keySpec = new X509EncodedKeySpec(Base64.decodeBase64(base64)); KeyFactory keyFactory = KeyFactory.getInstance("RSA", "BC"); return keyFactory.generatePublic(keySpec); } catch (GeneralSecurityException e) { e.printStackTrace(); } return null; }
From source file:zerogame.info.javapay.web.RSASignature.java
/** * *//* ww w .j a v a 2 s .c om*/ public static PublicKey getPublicKey() throws Exception { KeyFactory keyFactory = KeyFactory.getInstance("RSA"); byte[] encodedKey = Base64.decodeBase64(RSA_PUBLIC); PublicKey pubKey = keyFactory.generatePublic(new X509EncodedKeySpec(encodedKey)); return pubKey; }
From source file:com.emc.vipr.services.s3.S3ClientFactory.java
/** * Creates an EncryptionClient for testing. Loads the public and private keys from * the properties file (not suitable for production). * * @return/*from w w w . java 2 s .co m*/ * @throws IOException */ public static AmazonS3EncryptionClient getEncryptionClient() throws IOException { try { Properties props = ViprConfig.getProperties(); String accessKey = ViprConfig.getPropertyNotEmpty(props, ViprConfig.PROP_S3_ACCESS_KEY_ID); String secretKey = ViprConfig.getPropertyNotEmpty(props, ViprConfig.PROP_S3_SECRET_KEY); String endpoint = ViprConfig.getPropertyNotEmpty(props, ViprConfig.PROP_S3_ENDPOINT); String publicKey = ViprConfig.getPropertyNotEmpty(props, ViprConfig.PROP_PUBLIC_KEY); String privateKey = ViprConfig.getPropertyNotEmpty(props, ViprConfig.PROP_PRIVATE_KEY); byte[] pubKeyBytes = Base64.decodeBase64(publicKey.getBytes("US-ASCII")); byte[] privKeyBytes = Base64.decodeBase64(privateKey.getBytes("US-ASCII")); X509EncodedKeySpec pubKeySpec = new X509EncodedKeySpec(pubKeyBytes); PKCS8EncodedKeySpec privKeySpec = new PKCS8EncodedKeySpec(privKeyBytes); PublicKey pubKey; PrivateKey privKey; try { KeyFactory keyFactory = KeyFactory.getInstance("RSA"); pubKey = keyFactory.generatePublic(pubKeySpec); privKey = keyFactory.generatePrivate(privKeySpec); } catch (GeneralSecurityException e) { throw new RuntimeException("Could not load key pair: " + e, e); } EncryptionMaterials keys = new EncryptionMaterials(new KeyPair(pubKey, privKey)); BasicAWSCredentials creds = new BasicAWSCredentials(accessKey, secretKey); AmazonS3EncryptionClient client = new AmazonS3EncryptionClient(creds, keys); client.setEndpoint(endpoint); checkProxyConfig(client, props); return client; } catch (Exception e) { log.info("Could not load configuration: " + e); return null; } }
From source file:com.kuya.cordova.plugin.util.Security.java
/** * Generates a PublicKey instance from a string containing the * Base64-encoded public key.//from w w w .j av a2 s . c om * * @param encodedPublicKey Base64-encoded public key * @throws IllegalArgumentException if encodedPublicKey is invalid */ public static PublicKey generatePublicKey(String encodedPublicKey) { Log.d(TAG, "generatePublicKey " + encodedPublicKey); try { byte[] decodedKey = Base64.decode(encodedPublicKey); KeyFactory keyFactory = KeyFactory.getInstance(KEY_FACTORY_ALGORITHM); return keyFactory.generatePublic(new X509EncodedKeySpec(decodedKey)); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); } catch (InvalidKeySpecException e) { Log.e(TAG, "Invalid key specification."); throw new IllegalArgumentException(e); } catch (Base64DecoderException e) { Log.e(TAG, "Base64 decoding failed."); throw new IllegalArgumentException(e); } }
From source file:hh.learnj.test.license.test.rsa.RSATest.java
/** * ?//from www .j av a2 s . c o m * * @return * @throws Exception */ static PublicKey getPublicKey() throws Exception { X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(decodeBase64(publicKey)); KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM_RSA); return keyFactory.generatePublic(publicKeySpec); }
From source file:com.github.ibole.infrastructure.security.key.PemUtils.java
private static PublicKey getPublicKey(byte[] keyBytes, String algorithm) { PublicKey publicKey = null;//from ww w .java 2 s . c o m try { KeyFactory kf = KeyFactory.getInstance(algorithm); EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes); publicKey = kf.generatePublic(keySpec); } catch (NoSuchAlgorithmException | InvalidKeySpecException e) { MoreThrowables.throwIfUnchecked(e); } return publicKey; }
From source file:Main.java
public static KeyPair loadKeyPair(Context context, String name) throws IOException, NoSuchAlgorithmException, InvalidKeySpecException { // Read Public Key. File filePublicKey = new File(context.getFilesDir(), name + "_public.key"); FileInputStream fis = new FileInputStream(filePublicKey); byte[] encodedPublicKey = new byte[(int) filePublicKey.length()]; fis.read(encodedPublicKey);//from ww w . ja v a2 s . c o m fis.close(); // Read Private Key. File filePrivateKey = new File(context.getFilesDir(), name + "_private.key"); fis = new FileInputStream(filePrivateKey); byte[] encodedPrivateKey = new byte[(int) filePrivateKey.length()]; fis.read(encodedPrivateKey); fis.close(); // Generate KeyPair. KeyFactory keyFactory = KeyFactory.getInstance("RSA"); X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(encodedPublicKey); PublicKey publicKey = keyFactory.generatePublic(publicKeySpec); PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(encodedPrivateKey); PrivateKey privateKey = keyFactory.generatePrivate(privateKeySpec); return new KeyPair(publicKey, privateKey); }