List of usage examples for java.security KeyFactory generatePrivate
public final PrivateKey generatePrivate(KeySpec keySpec) throws InvalidKeySpecException
From source file:com.thoughtworks.go.security.Registration.java
public static Registration fromJson(String json) { Map map = new Gson().fromJson(json, Map.class); List<Certificate> chain = new ArrayList<>(); try {// w w w .j a v a2 s.co m PemReader reader = new PemReader(new StringReader((String) map.get("agentPrivateKey"))); KeyFactory kf = KeyFactory.getInstance("RSA"); PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(reader.readPemObject().getContent()); PrivateKey privateKey = kf.generatePrivate(spec); String agentCertificate = (String) map.get("agentCertificate"); PemReader certReader = new PemReader(new StringReader(agentCertificate)); while (true) { PemObject obj = certReader.readPemObject(); if (obj == null) { break; } chain.add(CertificateFactory.getInstance("X.509") .generateCertificate(new ByteArrayInputStream(obj.getContent()))); } return new Registration(privateKey, chain.toArray(new Certificate[chain.size()])); } catch (IOException | NoSuchAlgorithmException | CertificateException | InvalidKeySpecException e) { throw bomb(e); } }
From source file:org.casbah.provider.SSLeayEncoderTest.java
private static RSAPrivateCrtKey generateKey() throws GeneralSecurityException { KeyFactory kf = KeyFactory.getInstance("RSA"); RSAPrivateCrtKeySpec keyspec = new RSAPrivateCrtKeySpec(MODULUS, PUBLIC_EXPONENT, PRIVATE_EXPONENT, PRIME1, PRIME2, EXPONENT1, EXPONENT2, COEFFICIENT); return (RSAPrivateCrtKey) kf.generatePrivate(keyspec); }
From source file:com.sammyun.util.RSAUtils.java
/** * ?/*w ww .java2 s. co m*/ * * @param key ?base64? * @throws Exception */ public static PrivateKey getPrivateKey(String key) throws Exception { byte[] keyBytes; keyBytes = Base64Util.decode(key); PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); PrivateKey privateKey = keyFactory.generatePrivate(keySpec); return privateKey; }
From source file:com.github.aynu.mosir.core.standard.util.SecurityHelper.java
/** * RSA???//w w w. jav a2s .c o m * <dl> * <dt>? * <dd>RSA????????????? * </dl> * @param modulus * @param exponent ?? * @return RSA? */ public static RSAPrivateKey createPrivateKey(final BigInteger modulus, final BigInteger exponent) { try { final KeyFactory keyFactory = KeyFactory.getInstance(ALGO_KEY); return (RSAPrivateKey) keyFactory.generatePrivate(new RSAPrivateKeySpec(modulus, exponent)); } catch (NoSuchAlgorithmException | InvalidKeySpecException e) { throw new StandardRuntimeException(e); } }
From source file:com.sammyun.util.RSAUtils.java
/** * RSA??/*from ww w . j a v a 2 s.c om*/ * * @param content ??? * @param privateKey ? * @param input_charset ?? * @return ?? */ public static String sign(String content, String privateKey, String input_charset) { try { PKCS8EncodedKeySpec priPKCS8 = new PKCS8EncodedKeySpec(Base64Util.decode(privateKey)); KeyFactory keyf = KeyFactory.getInstance("RSA"); PrivateKey priKey = keyf.generatePrivate(priPKCS8); java.security.Signature signature = java.security.Signature.getInstance(SIGN_ALGORITHMS); signature.initSign(priKey); signature.update(content.getBytes(input_charset)); byte[] signed = signature.sign(); return Base64Util.encode(signed); } catch (Exception e) { e.printStackTrace(); } return null; }
From source file:net.arccotangent.pacchat.filesystem.KeyManager.java
public static KeyPair loadRSAKeys() { try {/*from w w w . j a va 2 s . c om*/ km_log.i("Loading RSA key pair from disk."); byte[] privEncoded = Files.readAllBytes(privkeyFile.toPath()); byte[] pubEncoded = Files.readAllBytes(pubkeyFile.toPath()); X509EncodedKeySpec pubSpec = new X509EncodedKeySpec(Base64.decodeBase64(pubEncoded)); PKCS8EncodedKeySpec privSpec = new PKCS8EncodedKeySpec(Base64.decodeBase64(privEncoded)); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); PublicKey pubkey = keyFactory.generatePublic(pubSpec); PrivateKey privkey = keyFactory.generatePrivate(privSpec); return new KeyPair(pubkey, privkey); } catch (IOException | NoSuchAlgorithmException | InvalidKeySpecException e) { km_log.e("Error while loading keypair!"); e.printStackTrace(); } return null; }
From source file:org.javaweb.utils.RSAUtils.java
/** * RSA???/*from ww w. j a v a2 s . c o m*/ * * @param data ? * @param key ? * @return * @throws Exception */ public static String sign(byte[] data, Key key) throws Exception { byte[] keyBytes = key.getEncoded(); PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes); KeyFactory keyFactory = KeyFactory.getInstance(key.getAlgorithm()); PrivateKey privateK = keyFactory.generatePrivate(pkcs8KeySpec); Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM); signature.initSign(privateK); signature.update(data); return Base64.encodeBase64String(signature.sign()); }
From source file:pepperim.util.IMCrypt.java
/** * @param b64str Base64-encoded private key * @return PrivateKey object/*from ww w. j a v a2s . com*/ */ public static PrivateKey decodePrivateKey(String b64str) { try { byte[] keydata = B64_Dec(b64str); PKCS8EncodedKeySpec ks = new PKCS8EncodedKeySpec(keydata); KeyFactory kf = KeyFactory.getInstance("RSA"); PrivateKey pk = kf.generatePrivate(ks); return pk; } catch (GeneralSecurityException e) { Main.log(e.getMessage()); return null; } }
From source file:com.zf.decipher.DataEn.java
private static byte[] getSecretKey(byte[] publicKey, byte[] privateKey) throws Exception { KeyFactory keyFactory = KeyFactory.getInstance("DH"); X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(publicKey); PublicKey pubKey = keyFactory.generatePublic(x509KeySpec); PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(privateKey); PrivateKey priKey = keyFactory.generatePrivate(pkcs8KeySpec); KeyAgreement keyAgree = KeyAgreement.getInstance(keyFactory.getAlgorithm()); keyAgree.init(priKey);/* w ww. j a v a 2 s. co m*/ keyAgree.doPhase(pubKey, true); return keyAgree.generateSecret(AES).getEncoded(); }
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 {/* ww w . j a v a2s .c o 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(); } }