List of usage examples for java.security KeyFactory generatePublic
public final PublicKey generatePublic(KeySpec keySpec) throws InvalidKeySpecException
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 ww w . jav a 2 s . co 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:license.regist.ReadProjectInfo.java
static PublicKey readPublicKeyFromFile() throws Exception { ObjectInputStream oin = new ObjectInputStream(new ByteArrayInputStream(KeyData.publicKey)); try {//from w w w . j a va2s.co m BigInteger m = (BigInteger) oin.readObject(); BigInteger e = (BigInteger) oin.readObject(); RSAPublicKeySpec keySpec = new RSAPublicKeySpec(m, e); KeyFactory fact = KeyFactory.getInstance("RSA"); return fact.generatePublic(keySpec); } finally { oin.close(); } }
From source file:be.fedict.eid.idp.model.CryptoUtil.java
public static PublicKey getPublicKey(byte[] publicKeyBytes) throws KeyLoadException { try {//www. j a v a 2 s . com X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(publicKeyBytes); KeyFactory rsaKeyFactory = KeyFactory.getInstance("RSA"); return rsaKeyFactory.generatePublic(publicKeySpec); } catch (InvalidKeySpecException e) { throw new KeyLoadException(e); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); } }
From source file:org.oscarehr.common.hl7.v2.oscar_to_oscar.SendingUtils.java
public static PublicKey getPublicOscarKey(String publicOscarKeyString) throws NoSuchAlgorithmException, InvalidKeySpecException { X509EncodedKeySpec pubKeySpec = new X509EncodedKeySpec(Base64.decodeBase64(publicOscarKeyString)); KeyFactory pubKeyFactory = KeyFactory.getInstance("RSA"); PublicKey publicOscarKey = pubKeyFactory.generatePublic(pubKeySpec); return publicOscarKey; }
From source file:com.sharky.Security.java
public static PublicKey generatePublicKey(String encodedPublicKey) { try {//from ww w . j a v a 2s. c o m 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) { throw new IllegalArgumentException(e); } catch (Base64DecoderException e) { throw new IllegalArgumentException(e); } }
From source file:com.vexsoftware.votifier.crypto.RSAIO.java
/** * Loads an RSA key pair from a directory. The directory must have the files * "public.key" and "private.key".//www. j a va 2s.c om * * @param directory * The directory to load from * @return The key pair * @throws Exception * If an error occurs */ public static KeyPair load(File directory) throws Exception { // Read the public key file. File publicKeyFile = new File(directory + "/public.key"); byte[] encodedPublicKey = FileUtils.readFileToByteArray(publicKeyFile); encodedPublicKey = DatatypeConverter.parseBase64Binary(new String(encodedPublicKey)); // Read the private key file. File privateKeyFile = new File(directory + "/private.key"); byte[] encodedPrivateKey = FileUtils.readFileToByteArray(privateKeyFile); encodedPrivateKey = DatatypeConverter.parseBase64Binary(new String(encodedPrivateKey)); // Instantiate and return the key pair. 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); }
From source file:org.ebayopensource.fido.uaf.crypto.KeyCodec.java
public static PublicKey getPubKey(byte[] bytes) throws InvalidKeySpecException, NoSuchAlgorithmException, NoSuchProviderException { KeyFactory kf = KeyFactory.getInstance("ECDSA", "SC"); return kf.generatePublic(new X509EncodedKeySpec(bytes)); }
From source file:net.arccotangent.pacchat.filesystem.KeyManager.java
public static PublicKey loadKeyByIP(String ip_address) { km_log.i("Loading public key for " + ip_address); try {// ww w . j a v a 2 s. c o m File pubFile = new File(installationPath + File.separator + ip_address + ".pub"); byte[] pubEncoded = Files.readAllBytes(pubFile.toPath()); X509EncodedKeySpec pubSpec = new X509EncodedKeySpec(Base64.decodeBase64(pubEncoded)); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); return keyFactory.generatePublic(pubSpec); } catch (IOException | NoSuchAlgorithmException | InvalidKeySpecException e) { km_log.e("Error while loading public key for " + ip_address + "!"); e.printStackTrace(); } return null; }
From source file:de.alpharogroup.crypto.key.PrivateKeyExtensions.java
/** * Generate the corresponding {@link PublicKey} object from the given {@link PrivateKey} object. * * @param privateKey//from w w w. j a va 2s. c om * the private key * @return the corresponding {@link PublicKey} object or null if generation failed. * @throws NoSuchAlgorithmException * the no such algorithm exception * @throws InvalidKeySpecException * the invalid key spec exception */ public static PublicKey generatePublicKey(final PrivateKey privateKey) throws NoSuchAlgorithmException, InvalidKeySpecException { if (privateKey instanceof RSAPrivateKey) { final RSAPrivateCrtKey privk = (RSAPrivateCrtKey) privateKey; final RSAPublicKeySpec publicKeySpec = new RSAPublicKeySpec(privk.getModulus(), privk.getPublicExponent()); final KeyFactory keyFactory = KeyFactory.getInstance(KeyPairGeneratorAlgorithm.RSA.getAlgorithm()); final PublicKey publicKey = keyFactory.generatePublic(publicKeySpec); return publicKey; } return null; }
From source file:org.opentravel.schemacompiler.security.PasswordHelper.java
/** * Returns an encryption cipher that is based on the public encryption key file located on the * application's classpath.//from ww w . j av a 2 s . c om * * @return Cipher * @throws GeneralSecurityException * thrown if encryption key is not valid * @throws IOException * thrown if the contents of the public key file cannot be loaded */ private static Cipher loadEncryptionCipher() throws GeneralSecurityException, IOException { BigInteger[] keyComponents = loadKeyFile(PUBLIC_KEYFILE); RSAPublicKeySpec keySpec = new RSAPublicKeySpec(keyComponents[0], keyComponents[1]); KeyFactory factory = KeyFactory.getInstance(ENCRYPTION_ALGORITHM); PublicKey publicKey = factory.generatePublic(keySpec); Cipher cipher = Cipher.getInstance(CIPHER_TRANSFORMATION); cipher.init(Cipher.PUBLIC_KEY, publicKey); return cipher; }