List of usage examples for java.security KeyFactory generatePublic
public final PublicKey generatePublic(KeySpec keySpec) throws InvalidKeySpecException
From source file:com.vexsoftware.votifier.util.rsa.RSAIO.java
/** * Loads an RSA public key from a URL./*from ww w. ja v a 2 s . com*/ * * @param url * The URL that has the public key * @return * The public key * @throws Exception * If an error occurs */ public static PublicKey loadPublicKey(URL url) throws Exception { String publicKey = new String(IOUtils.toByteArray(url), "UTF-8") .replaceAll("(-+BEGIN PUBLIC KEY-+\\r?\\n|-+END PUBLIC KEY-+\\r?\\n?)", ""); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(DatatypeConverter.parseBase64Binary(publicKey)); return keyFactory.generatePublic(publicKeySpec); }
From source file:com.turo.pushy.apns.auth.ApnsVerificationKey.java
/** * Loads a verification key from the given input stream. * * @param inputStream the input stream from which to load the key * @param teamId the ten-character, Apple-issued identifier for the team to which the key belongs * @param keyId the ten-character, Apple-issued identitifier for the key itself * * @return an APNs verification key with the given key ID and associated with the given team * * @throws IOException if a key could not be loaded from the given file for any reason * @throws NoSuchAlgorithmException if the JVM does not support elliptic curve keys * @throws InvalidKeyException if the loaded key is invalid for any reason *///from ww w . j a v a 2 s. co m public static ApnsVerificationKey loadFromInputStream(final InputStream inputStream, final String teamId, final String keyId) throws IOException, NoSuchAlgorithmException, InvalidKeyException { final ECPublicKey verificationKey; { final String base64EncodedPublicKey; { final StringBuilder publicKeyBuilder = new StringBuilder(); final BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); boolean haveReadHeader = false; boolean haveReadFooter = false; for (String line; (line = reader.readLine()) != null;) { if (!haveReadHeader) { if (line.contains("BEGIN PUBLIC KEY")) { haveReadHeader = true; } } else { if (line.contains("END PUBLIC KEY")) { haveReadFooter = true; break; } else { publicKeyBuilder.append(line); } } } if (!(haveReadHeader && haveReadFooter)) { throw new IOException("Could not find public key header/footer"); } base64EncodedPublicKey = publicKeyBuilder.toString(); } final byte[] keyBytes = Base64.decodeBase64(base64EncodedPublicKey); final X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes); final KeyFactory keyFactory = KeyFactory.getInstance("EC"); try { verificationKey = (ECPublicKey) keyFactory.generatePublic(keySpec); } catch (InvalidKeySpecException e) { throw new InvalidKeyException(e); } } return new ApnsVerificationKey(keyId, teamId, verificationKey); }
From source file:org.nick.ghettounlock.GhettoTrustAgent.java
public static RSAPublicKey getPublicKey(Context ctx) { String pubKeyStr = PreferenceManager.getDefaultSharedPreferences(ctx).getString(PREF_PUB_KEY, null); if (pubKeyStr == null) { return null; }//from ww w .j a v a2 s .co m try { X509EncodedKeySpec keySpec = new X509EncodedKeySpec( Base64.decode(pubKeyStr.getBytes("UTF-8"), Base64.DEFAULT)); KeyFactory kf = KeyFactory.getInstance("RSA"); RSAPublicKey pubKey = (RSAPublicKey) kf.generatePublic(keySpec); return pubKey; } catch (Exception e) { throw new RuntimeException(e); } }
From source file:com.github.aynu.mosir.core.standard.util.SecurityHelper.java
/** * RSA???//from w ww. j ava 2 s. com * <dl> * <dt>? * <dd>RSA????????????? * </dl> * @param modulus * @param exponent ?? * @return RSA? */ public static RSAPublicKey createPublicKey(final BigInteger modulus, final BigInteger exponent) { try { final KeyFactory keyFactory = KeyFactory.getInstance(ALGO_KEY); return (RSAPublicKey) keyFactory.generatePublic(new RSAPublicKeySpec(modulus, exponent)); } catch (NoSuchAlgorithmException | InvalidKeySpecException e) { throw new StandardRuntimeException(e); } }
From source file:net.unicon.cas.support.wsfederation.WsFederationUtils.java
/** * getSigningCredential loads up an X509Credential from a file. * * @param resource the signing certificate file * @return an X509 credential//from w ww . j ava2s .co m */ public static X509Credential getSigningCredential(final Resource resource) { try (final InputStream inputStream = resource.getInputStream()) { //grab the certificate file final CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509"); final X509Certificate certificate = (X509Certificate) certificateFactory .generateCertificate(inputStream); //get the public key from the certificate final X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec( certificate.getPublicKey().getEncoded()); //generate public key to validate signatures final KeyFactory keyFactory = KeyFactory.getInstance("RSA"); final PublicKey publicKey = keyFactory.generatePublic(publicKeySpec); //add the public key final BasicX509Credential publicCredential = new BasicX509Credential(); publicCredential.setPublicKey(publicKey); LOGGER.debug("getSigningCredential: key retrieved."); return publicCredential; } catch (final Exception ex) { LOGGER.error("I/O error retrieving the signing cert: {}", ex); return null; } }
From source file:com.vexsoftware.votifier.util.rsa.RSAIO.java
/** * Loads an RSA key pair from a directory. The directory must have the files * "public.key" and "private.key"./* w ww. j a v a 2 s . 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"); FileInputStream in = null; byte[] encodedPublicKey; try { in = new FileInputStream(directory + "/public.key"); encodedPublicKey = new byte[(int) publicKeyFile.length()]; in.read(encodedPublicKey); encodedPublicKey = DatatypeConverter.parseBase64Binary(new String(encodedPublicKey)); } finally { try { in.close(); } catch (Exception exception) { // ignore } } // Read the private key file. File privateKeyFile = new File(directory + "/private.key"); byte[] encodedPrivateKey; try { in = new FileInputStream(directory + "/private.key"); encodedPrivateKey = new byte[(int) privateKeyFile.length()]; in.read(encodedPrivateKey); encodedPrivateKey = DatatypeConverter.parseBase64Binary(new String(encodedPrivateKey)); } finally { try { in.close(); } catch (Exception exception) { // ignore } } // 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:net.oauth.jsontoken.crypto.MagicRsaPublicKey.java
private static PublicKey parseKey(String magicKey) { String[] pieces = magicKey.split(Pattern.quote(".")); if (pieces.length != 3) { throw new IllegalStateException("not a valid magic key: " + magicKey); }//w ww . j ava2 s.co m if (!pieces[0].equals("RSA")) { throw new IllegalStateException("unkown key type for magic key: " + pieces[0]); } String modulusString = pieces[1]; String exponentString = pieces[2]; byte[] modulusBytes = Base64.decodeBase64(modulusString); byte[] exponentBytes = Base64.decodeBase64(exponentString); BigInteger modulus = new BigInteger(modulusBytes); BigInteger exponent = new BigInteger(exponentBytes); RSAPublicKeySpec spec = new RSAPublicKeySpec(modulus, exponent); KeyFactory fac; try { fac = KeyFactory.getInstance("RSA"); } catch (NoSuchAlgorithmException e) { throw new RuntimeException("RSA key factory missing on platform", e); } try { return fac.generatePublic(spec); } catch (InvalidKeySpecException e) { throw new IllegalStateException("bad key in descripor doc: " + magicKey, e); } }
From source file:com.security.ch08_rsa.RSACoderTextKey.java
/** * //from ww w . j a v a 2s. c o m * * @param data * ? * @param key * * @return byte[] ? * @throws Exception */ private static byte[] encryptByPublicKey(byte[] data, byte[] key) throws Exception { // ? X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(key); KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM); PublicKey publicKey = keyFactory.generatePublic(x509KeySpec); // ? Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm()); cipher.init(Cipher.ENCRYPT_MODE, publicKey); return cipher.doFinal(data); }
From source file:org.javaweb.utils.RSAUtils.java
/** * RSA???//from ww w .ja v a 2 s . co m * * @param data ? * @param key * @param sign ??Base64 * @return * @throws Exception */ public static boolean verify(byte[] data, Key key, String sign) throws Exception { X509EncodedKeySpec keySpec = new X509EncodedKeySpec(key.getEncoded()); KeyFactory keyFactory = KeyFactory.getInstance(key.getAlgorithm()); PublicKey publicK = keyFactory.generatePublic(keySpec); Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM); signature.initVerify(publicK); signature.update(data); return signature.verify(Base64.decodeBase64(sign)); }
From source file:eu.dety.burp.joseph.utilities.Converter.java
/** * Build {@link RSAPublicKey} from PublicKey PEM string * /* w ww. ja v a 2 s . co m*/ * @param pemInput * PublicKey PEM string * @return {@link RSAPublicKey} or null */ public static RSAPublicKey getRsaPublicKeyByPemString(String pemInput) { RSAPublicKey publicKey = null; String pubKey = pemInput.replaceAll("(-+BEGIN PUBLIC KEY-+\\r?\\n|-+END PUBLIC KEY-+\\r?\\n?)", ""); // PKCS8 try { byte[] keyBytes = Base64.decodeBase64(pubKey); X509EncodedKeySpec spec = new X509EncodedKeySpec(keyBytes); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); publicKey = (RSAPublicKey) keyFactory.generatePublic(spec); } catch (Exception e) { } // PKCS1 try { byte[] keyBytes = Base64.decodeBase64(pubKey); keyBytes = Arrays.copyOfRange(keyBytes, 24, keyBytes.length); X509EncodedKeySpec spec = new X509EncodedKeySpec(keyBytes); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); publicKey = (RSAPublicKey) keyFactory.generatePublic(spec); } catch (Exception e) { } return publicKey; }