List of usage examples for java.security KeyFactory generatePublic
public final PublicKey generatePublic(KeySpec keySpec) throws InvalidKeySpecException
From source file:com.clustercontrol.util.KeyCheck.java
/** * ?// w w w . j a v a 2s . c o m * com.clustercontrol.key.KeyGenerator????????public?? * @param str * @return * @throws HinemosUnknown */ public static PublicKey getPublicKey(String str) throws HinemosUnknown { try { X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(string2Byte(str)); KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM); return keyFactory.generatePublic(publicKeySpec); } catch (InvalidKeySpecException e) { throw new HinemosUnknown("getPublicKey fail " + e.getMessage(), e); } catch (NoSuchAlgorithmException e) { throw new HinemosUnknown("getPublicKey fail " + e.getMessage(), e); } }
From source file:com.jaspersoft.jasperserver.jaxrs.client.core.EncryptionUtils.java
private static PublicKey getPublicKey(String n, String e) throws Exception { KeyFactory keyFactory = KeyFactory.getInstance("RSA"); int radix = 16; BigInteger modulus = new BigInteger(n, radix); BigInteger publicExponent = new BigInteger(e, radix); RSAPublicKeySpec publicKeySpec = new RSAPublicKeySpec(modulus, publicExponent); return keyFactory.generatePublic(publicKeySpec); }
From source file:net.adamcin.httpsig.testutil.KeyTestUtil.java
public static KeyPair getKeyPairFromProperties(String parentName, String keyName) { InputStream is = null;//from www . jav a2 s . c o m try { is = KeyTestUtil.class.getResourceAsStream("/" + parentName + "/" + keyName + ".properties"); Properties props = new Properties(); props.load(is); if (TYPE_RSA.equals(props.getProperty(P_TYPE))) { RSAPrivateKeySpec privSpec = null; if (props.getProperty(RSA_P) != null && props.getProperty(RSA_Q) != null && props.getProperty(RSA_U) != null) { privSpec = new RSAPrivateCrtKeySpec(new BigInteger(props.getProperty(RSA_N)), new BigInteger(props.getProperty(RSA_E)), new BigInteger(props.getProperty(RSA_D)), new BigInteger(props.getProperty(RSA_P)), new BigInteger(props.getProperty(RSA_Q)), new BigInteger(props.getProperty(RSA_PE)), new BigInteger(props.getProperty(RSA_QE)), new BigInteger(props.getProperty(RSA_U))); } else { privSpec = new RSAPrivateKeySpec(new BigInteger(props.getProperty(RSA_N)), new BigInteger(props.getProperty(RSA_D))); } RSAPublicKeySpec pubSpec = new RSAPublicKeySpec(new BigInteger(props.getProperty(RSA_N)), new BigInteger(props.getProperty(RSA_E))); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); return new KeyPair(keyFactory.generatePublic(pubSpec), keyFactory.generatePrivate(privSpec)); } else if (TYPE_DSA.equals(props.getProperty(P_TYPE))) { DSAPrivateKeySpec privSpec = new DSAPrivateKeySpec(new BigInteger(props.getProperty(DSA_X)), new BigInteger(props.getProperty(DSA_P)), new BigInteger(props.getProperty(DSA_Q)), new BigInteger(props.getProperty(DSA_G))); DSAPublicKeySpec pubSpec = new DSAPublicKeySpec(new BigInteger(props.getProperty(DSA_Y)), new BigInteger(props.getProperty(DSA_P)), new BigInteger(props.getProperty(DSA_Q)), new BigInteger(props.getProperty(DSA_G))); KeyFactory keyFactory = KeyFactory.getInstance("DSA"); return new KeyPair(keyFactory.generatePublic(pubSpec), keyFactory.generatePrivate(privSpec)); } } catch (Exception e) { LOGGER.error("Failed to read properties", e); } finally { IOUtils.closeQuietly(is); } return null; }
From source file:com.github.aynu.yukar.framework.util.SecurityHelper.java
/** * RSA???/* w ww. ja va 2 s. co m*/ * <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("RSA"); return (RSAPublicKey) keyFactory.generatePublic(new RSAPublicKeySpec(modulus, exponent)); } catch (NoSuchAlgorithmException | InvalidKeySpecException e) { throw new StandardRuntimeException(e); } }
From source file:hh.learnj.test.license.test.rsacoder.RSACoder.java
/** * /*from w ww. j a v a2 s. c om*/ * * @param data * ? * @param key * * @return byte[] ? */ public static byte[] decryptByPublicKey(byte[] data, byte[] key) throws Exception { // KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM); // ? // ??? X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(key); // PublicKey pubKey = keyFactory.generatePublic(x509KeySpec); // ? Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm()); cipher.init(Cipher.DECRYPT_MODE, pubKey); return cipher.doFinal(data); }
From source file:com.zxy.commons.codec.rsa.RSAUtils.java
/** * <p>//w w w . j a v a 2s . c om * ?? * </p> * * @param data ? * @param publicKey (BASE64?) * @param sign ?? * * @return boolean * @throws Exception Exception * */ public static boolean verify(byte[] data, String publicKey, String sign) throws Exception { byte[] keyBytes = Base64.decodeBase64(publicKey); X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes); KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM); 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:hh.learnj.test.license.test.rsacoder.RSACoder.java
/** * /*from w ww.j a va2s .c om*/ * * @param data? * @param key * * @return byte[] ? */ public static byte[] encryptByPublicKey(byte[] data, byte[] key) throws Exception { // KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM); // ? // ??? X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(key); // PublicKey pubKey = keyFactory.generatePublic(x509KeySpec); // ? Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm()); cipher.init(Cipher.ENCRYPT_MODE, pubKey); return cipher.doFinal(data); }
From source file:com.sammyun.util.RSAUtils.java
/** * RSA??//from w w w . j ava 2 s. c om * * @param content ??? * @return */ public static String encryptContent(String content, String ali_public_key) { try { KeyFactory keyFactory = KeyFactory.getInstance("RSA"); byte[] encodedKey = Base64Util.decode(ali_public_key); PublicKey pubKey = keyFactory.generatePublic(new X509EncodedKeySpec(encodedKey)); java.security.Signature signature = java.security.Signature.getInstance(SIGN_ALGORITHMS); signature.initVerify(pubKey); signature.update(content.getBytes("utf-8")); byte[] signed = signature.sign(); return Base64Util.encode(signed); } catch (Exception e) { e.printStackTrace(); logger.error(e.getMessage()); return ""; } }
From source file:de.alpharogroup.crypto.key.KeyExtensions.java
/** * Read public key./* w w w . j av a 2 s . c om*/ * * @param publicKeyBytes * the public key bytes * @param provider * the provider * @return the public key * @throws NoSuchAlgorithmException * is thrown if instantiation of the cypher object fails. * @throws InvalidKeySpecException * is thrown if generation of the SecretKey object fails. * @throws NoSuchProviderException * is thrown if the specified provider is not registered in the security provider * list. */ public static PublicKey readPublicKey(final byte[] publicKeyBytes, final String provider) throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchProviderException { final X509EncodedKeySpec keySpec = new X509EncodedKeySpec(publicKeyBytes); final KeyFactory keyFactory = KeyFactory.getInstance(KeyPairGeneratorAlgorithm.RSA.getAlgorithm()); final PublicKey publicKey = keyFactory.generatePublic(keySpec); return publicKey; }
From source file:com.sammyun.util.RSAUtils.java
/** * RSA??/*ww w.j a va 2 s . c o m*/ * * @param content ??? * @param sign ?? * @param ali_public_key ? * @param input_charset ?? * @return */ public static boolean verify(String content, String sign, String ali_public_key, String input_charset) { try { KeyFactory keyFactory = KeyFactory.getInstance("RSA"); byte[] encodedKey = Base64Util.decode(ali_public_key); PublicKey pubKey = keyFactory.generatePublic(new X509EncodedKeySpec(encodedKey)); java.security.Signature signature = java.security.Signature.getInstance(SIGN_ALGORITHMS); signature.initVerify(pubKey); signature.update(content.getBytes(input_charset)); boolean bverify = signature.verify(Base64Util.decode(sign)); return bverify; } catch (Exception e) { e.printStackTrace(); } return false; }