List of usage examples for java.security KeyFactory generatePublic
public final PublicKey generatePublic(KeySpec keySpec) throws InvalidKeySpecException
From source file:pepperim.util.IMCrypt.java
/** * @param b64str Base64-encoded public key * @return PublicKey object//from w w w. j a va 2s .co m */ public static PublicKey decodePublicKey(String b64str) { try { byte[] keydata = B64_Dec(b64str); X509EncodedKeySpec ks = new X509EncodedKeySpec(keydata); KeyFactory kf = KeyFactory.getInstance("RSA"); PublicKey pk = kf.generatePublic(ks); return pk; } catch (GeneralSecurityException e) { Main.log(e.getMessage()); return null; } }
From source file:com.soomla.billing.Security.java
/** * Generates a PublicKey instance from a string containing the * Base64-encoded public key./*from w ww .j a va 2s . co m*/ * * @param encodedPublicKey Base64-encoded public key * @throws IllegalArgumentException if encodedPublicKey is invalid */ public static PublicKey generatePublicKey(String 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) { StoreUtils.LogError(TAG, "Invalid key specification."); throw new IllegalArgumentException(e); } catch (Base64DecoderException e) { StoreUtils.LogError(TAG, "Base64 decoding failed."); throw new IllegalArgumentException(e); } }
From source file:org.cprados.wificellmanager.billing.Security.java
/** * Generates a PublicKey instance from a string containing the * Base64-encoded public key./*from w w w. j a v a2s .co m*/ * * @param encodedPublicKey Base64-encoded public key * @throws IllegalArgumentException if encodedPublicKey is invalid */ public static PublicKey generatePublicKey(String encodedPublicKey) { try { //byte[] decodedKey = Base64.decode(encodedPublicKey); byte[] decodedKey = Base64.decode(encodedPublicKey, Base64.DEFAULT); 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:org.kaaproject.kaa.common.endpoint.security.KeyUtil.java
/** * Gets the public key from bytes.//from w w w . j av a 2s.co m * * @param keyBytes the key bytes * @return the public * @throws InvalidKeyException invalid key exception */ public static PublicKey getPublic(byte[] keyBytes) throws InvalidKeyException { try { X509EncodedKeySpec spec = new X509EncodedKeySpec(keyBytes); KeyFactory kf = KeyFactory.getInstance(RSA); return kf.generatePublic(spec); } catch (NoSuchAlgorithmException | InvalidKeySpecException ex) { throw new InvalidKeyException(ex); } }
From source file:com.security.ch08_rsa.RSACoderTextKey.java
/** * /*from w w w . java 2 s .c om*/ * * @param data * ? * @param key * * @return byte[] ? * @throws Exception */ private static byte[] decryptByPublicKey(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.DECRYPT_MODE, publicKey); return cipher.doFinal(data); }
From source file:com.shenit.commons.codec.RsaUtils.java
/** * RSA??/*ww w.j av a 2 s. co m*/ * * @param content * ??? * @param sign * ?? * @param publicKey * ? * @param inputCharset * ?? * @return */ public static boolean verify(String content, String sign, String publicKey, String algorithm, String inputCharset) { try { KeyFactory keyFactory = KeyFactory.getInstance(CODEC_RSA); byte[] encodedKey = Base64.decodeBase64(publicKey); PublicKey pubKey = keyFactory.generatePublic(new X509EncodedKeySpec(encodedKey)); java.security.Signature signature = java.security.Signature.getInstance(algorithm); signature.initVerify(pubKey); signature.update(content.getBytes(inputCharset)); boolean bverify = signature.verify(Base64.decodeBase64(sign)); return bverify; } catch (Exception e) { if (LOG.isWarnEnabled()) LOG.warn("[verify] verify with exception", e); } return false; }
From source file:oscar.oscarLab.ca.all.pageUtil.LabUploadAction.java
public static ArrayList<Object> getClientInfo(String service) { PublicKey Key = null;//ww w .ja v a 2 s .c om Base64 base64 = new Base64(0); String keyString = ""; String type = ""; byte[] publicKey; ArrayList<Object> info = new ArrayList<Object>(); try { PublicKeyDao publicKeyDao = (PublicKeyDao) SpringUtils.getBean("publicKeyDao"); org.oscarehr.common.model.PublicKey publicKeyObject = publicKeyDao.find(service); if (publicKeyObject != null) { keyString = publicKeyObject.getBase64EncodedPublicKey(); type = publicKeyObject.getType(); } publicKey = base64.decode(keyString.getBytes(MiscUtils.ENCODING)); X509EncodedKeySpec pubKeySpec = new X509EncodedKeySpec(publicKey); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); Key = keyFactory.generatePublic(pubKeySpec); info.add(Key); info.add(type); } catch (Exception e) { logger.error("Could not retrieve private key: ", e); } return (info); }
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; PublicKey publicKey = null;//from w ww . j ava 2s. com 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:jfabrix101.billing.BillingSecurity.java
/** * Generates a PublicKey instance from a string containing the * Base64-encoded public key.//from w w w . ja va 2 s. c om * * @param encodedPublicKey Base64-encoded public key * @throws IllegalArgumentException if encodedPublicKey is invalid */ public static PublicKey generatePublicKey(String 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 (Exception e) { Log.e(TAG, "Base64 decoding failed."); throw new IllegalArgumentException(e); } }
From source file:com.cablevision.util.sso.UtilSSO.java
/** * Creates a PublicKey from the specified public key file and algorithm. * Returns null if failure to generate PublicKey. * /* w w w .ja v a 2 s .co m*/ * @param publicKeyFilepath location of public key file * @param algorithm algorithm of specified key file * @return PublicKey object representing contents of specified public key * file, null if error in generating key or invalid file specified */ public static PublicKey getPublicKey(String publicKeyFilepath, String algorithm) throws SamlException { try { InputStream pubKey = null; ClassLoader cl = Thread.currentThread().getContextClassLoader(); pubKey = cl.getResourceAsStream(publicKeyFilepath); byte[] bytes = IOUtils.toByteArray(pubKey); pubKey.close(); System.out.println("Private bytes: " + Arrays.toString(bytes)); pubKey.close(); X509EncodedKeySpec pubSpec = new X509EncodedKeySpec(bytes); KeyFactory factory = KeyFactory.getInstance(algorithm); return factory.generatePublic(pubSpec); } catch (FileNotFoundException e) { throw new SamlException("ERROR: Public key file not found - " + publicKeyFilepath); } catch (IOException e) { throw new SamlException("ERROR: Invalid public key file - " + e.getMessage()); } catch (NoSuchAlgorithmException e) { throw new SamlException("ERROR: Invalid public key algorithm - " + e.getMessage()); } catch (InvalidKeySpecException e) { throw new SamlException("ERROR: Invalid public key spec - " + e.getMessage()); } }