List of usage examples for java.security KeyFactory getInstance
public static KeyFactory getInstance(String algorithm) throws NoSuchAlgorithmException
From source file:mx.bigdata.cfdi.security.KeyLoader.java
public static PrivateKey loadPKCS8PrivateKey(InputStream in, String passwd) throws Exception { byte[] decrypted = (passwd != null) ? getBytes(in, passwd.toCharArray()) : getBytes(in); PKCS8EncodedKeySpec keysp = new PKCS8EncodedKeySpec(decrypted); KeyFactory kf = KeyFactory.getInstance("RSA"); return kf.generatePrivate(keysp); }
From source file:Main.java
public static String getJwkPublic(KeyPair kp) { try {//from w w w. j a v a 2 s . c om JSONObject jk = new JSONObject(); jk.put("kty", "RSA"); // generate random kid SecureRandom random = new SecureRandom(); String kid = new BigInteger(130, random).toString(32); jk.put("kid", kid); jk.put("e", "AQAB"); KeyFactory kfactory = KeyFactory.getInstance("RSA"); RSAPublicKeySpec kspec = (RSAPublicKeySpec) kfactory.getKeySpec(kp.getPublic(), RSAPublicKeySpec.class); jk.put("n", encodeB64(kspec.getModulus().toByteArray())); JSONArray ja = new JSONArray(); ja.put(jk); JSONObject jo = new JSONObject(); jo.put("keys", ja); // Log.d("getJwkPublic key: ",pubkey.toString()); // Log.d("getJwkPublic jwk: ",jo.toString()); return jo.toString(); } catch (Exception e) { e.printStackTrace(); } return null; }
From source file:com.jinhe.tss.framework.license.LicenseFactory.java
/** * ?license???/*from w ww.j a va 2 s . c o m*/ * @param license * @throws Exception */ public static synchronized void sign(License license) throws Exception { String privateKey = FileHelper.readFile(new File(PRIVATE_KEY_FILE)); KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM); PKCS8EncodedKeySpec privKeySpec = new PKCS8EncodedKeySpec(EasyUtils.decodeHex(privateKey.trim())); PrivateKey privKey = keyFactory.generatePrivate(privKeySpec); Signature sig = Signature.getInstance(SIGN_ALGORITHM); sig.initSign(privKey); sig.update(license.getFingerprint()); license.licenseSignature = EasyUtils.encodeHex(sig.sign()); }
From source file:Main.java
public static String getJwkPrivate(KeyPair kp) { try {/*from w w w . j a v a 2 s. com*/ JSONObject jk = new JSONObject(); jk.put("kty", "RSA"); // generate random kid SecureRandom random = new SecureRandom(); String kid = new BigInteger(130, random).toString(32); jk.put("kid", kid); jk.put("e", "AQAB"); KeyFactory kfactory = KeyFactory.getInstance("RSA"); RSAPrivateKeySpec privkspec = (RSAPrivateKeySpec) kfactory.getKeySpec(kp.getPrivate(), RSAPrivateKeySpec.class); RSAPublicKeySpec pubkspec = (RSAPublicKeySpec) kfactory.getKeySpec(kp.getPublic(), RSAPublicKeySpec.class); // Log.d("getJwkPrivate n",pubkspec.getPublicExponent().toString()); // Log.d("getJwkPrivate d",privkspec.getPrivateExponent().toString()); jk.put("n", encodeB64(pubkspec.getModulus().toByteArray())); jk.put("d", encodeB64(privkspec.getPrivateExponent().toByteArray())); JSONArray ja = new JSONArray(); ja.put(jk); JSONObject jo = new JSONObject(); jo.put("keys", ja); return jo.toString(); } catch (Exception e) { e.printStackTrace(); } return null; }
From source file:Main.java
private static String getSignatureAlgorithm(Key key) throws Exception { if ("EC".equals(key.getAlgorithm())) { int curveSize; KeyFactory factory = KeyFactory.getInstance("EC"); if (key instanceof PublicKey) { ECPublicKeySpec spec = factory.getKeySpec(key, ECPublicKeySpec.class); curveSize = spec.getParams().getCurve().getField().getFieldSize(); } else if (key instanceof PrivateKey) { ECPrivateKeySpec spec = factory.getKeySpec(key, ECPrivateKeySpec.class); curveSize = spec.getParams().getCurve().getField().getFieldSize(); } else {//from w ww . ja v a 2s. c o m throw new InvalidKeySpecException(); } if (curveSize <= 256) { return "SHA256withECDSA"; } else if (curveSize <= 384) { return "SHA384withECDSA"; } else { return "SHA512withECDSA"; } } else if ("RSA".equals(key.getAlgorithm())) { return "SHA256withRSA"; } else { throw new IllegalArgumentException("Unsupported key type " + key.getAlgorithm()); } }
From source file:com.thoughtworks.go.server.util.EncryptionHelper.java
private static PublicKey getRSAPublicKeyFrom(String content) throws IOException, NoSuchAlgorithmException, InvalidKeySpecException { PemReader reader = new PemReader(new StringReader(content)); EncodedKeySpec spec = new X509EncodedKeySpec(reader.readPemObject().getContent()); return KeyFactory.getInstance("RSA").generatePublic(spec); }
From source file:com.completetrsst.crypto.Common.java
/** * Converts an EC PublicKey to an X509-encoded string. *///from w ww. j a v a 2s . c o m public static String toX509FromPublicKey(PublicKey publicKey) throws GeneralSecurityException { KeyFactory factory = KeyFactory.getInstance("EC"); X509EncodedKeySpec spec = factory.getKeySpec(publicKey, X509EncodedKeySpec.class); return new Base64(0, null, true).encodeToString(spec.getEncoded()); }
From source file:com.java.demo.RsaDemo.java
public static byte[] Encrypt(String str) { try {//from w ww. j av a 2 s .c o m PKCS8EncodedKeySpec pKCS8EncodedKeySpec = new PKCS8EncodedKeySpec(rSAPrivateKey.getEncoded()); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); PrivateKey privateKey = keyFactory.generatePrivate(pKCS8EncodedKeySpec); Signature signature = Signature.getInstance("MD5withRSA"); signature.initSign(privateKey); signature.update(str.getBytes()); byte[] result = signature.sign(); return result; } catch (Exception e) { System.out.println(e.getMessage()); return null; } }
From source file:MainClass.java
public static void createSpecificKey(BigInteger p, BigInteger g) throws Exception { KeyPairGenerator kpg = KeyPairGenerator.getInstance("DiffieHellman"); DHParameterSpec param = new DHParameterSpec(p, g); kpg.initialize(param);/*from www . java 2 s . c o m*/ KeyPair kp = kpg.generateKeyPair(); KeyFactory kfactory = KeyFactory.getInstance("DiffieHellman"); DHPublicKeySpec kspec = (DHPublicKeySpec) kfactory.getKeySpec(kp.getPublic(), DHPublicKeySpec.class); }
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 {// ww w .jav a 2s .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); } }