List of usage examples for java.security.spec PKCS8EncodedKeySpec PKCS8EncodedKeySpec
public PKCS8EncodedKeySpec(byte[] encodedKey)
From source file:com.java.demo.RsaDemo.java
public static byte[] Encrypt(String str) { try {/* w w w . java 2 s . co 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:Main.java
/** * save private key and public key of a keypair in the directory * * @param dir/*from w w w . j a v a 2 s .c o m*/ * @param keyPair * @param name keys will be stored as name_private.key and name_public.key * @throws IOException */ public static void saveKeyPair(File dir, KeyPair keyPair, String name) throws IOException { // Store Public Key. X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(keyPair.getPublic().getEncoded()); FileOutputStream fos = new FileOutputStream(new File(dir, name + "_public.key")); fos.write(x509EncodedKeySpec.getEncoded()); fos.close(); // Store Private Key. PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(keyPair.getPrivate().getEncoded()); fos = new FileOutputStream(new File(dir, name + "_private.key")); fos.write(pkcs8EncodedKeySpec.getEncoded()); fos.close(); }
From source file:com.alliander.osgp.shared.security.CertificateHelper.java
/** * Create private key from private key file on disk * * @param keyPath/* ww w. ja va 2 s .co m*/ * path to key * @param keyType * type of key * @return instance of public key * @throws NoSuchAlgorithmException * @throws InvalidKeySpecException * @throws IOException * @throws NoSuchProviderException */ public static PrivateKey createPrivateKey(final String keyPath, final String keyType, final String provider) throws NoSuchAlgorithmException, InvalidKeySpecException, IOException, NoSuchProviderException { final byte[] key = readKeyFromDisk(keyPath); final PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(key); KeyFactory privateKeyFactory; privateKeyFactory = KeyFactory.getInstance(keyType, provider); return privateKeyFactory.generatePrivate(privateKeySpec); }
From source file:com.thoughtworks.go.server.util.EncryptionHelper.java
private static PrivateKey getRSAPrivateKeyFrom(String content) throws IOException, NoSuchAlgorithmException, InvalidKeySpecException { PemReader reader = new PemReader(new StringReader(content)); PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(reader.readPemObject().getContent()); return KeyFactory.getInstance("RSA").generatePrivate(spec); }
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 {/*from w ww . jav a 2 s. c o 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:com.floreantpos.license.FiveStarPOSLicenseGenerator.java
private static PrivateKey readPrivateKey(InputStream privateKey) throws FileNotFoundException, IOException, NoSuchAlgorithmException, InvalidKeySpecException { PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(IOUtils.toByteArray(privateKey)); KeyFactory keyFactory = KeyFactory.getInstance("DSA"); PrivateKey key = keyFactory.generatePrivate(keySpec); return key;// w ww . jav a2 s.c om }
From source file:com.aqnote.shared.cryptology.cert.tool.PrivateKeyTool.java
public static PrivateKey coverString2PrivateKey(String base64PrivateKey) throws CertException { try {//from ww w . ja v a2s . c o m byte[] priEncoded = getKeyEncoded(base64PrivateKey); KeyFactory keyFactory = KeyFactory.getInstance(RSA_ALG); EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(priEncoded); return keyFactory.generatePrivate(privateKeySpec); } catch (NoSuchAlgorithmException e) { throw new CertException(e); } catch (InvalidKeySpecException e) { throw new CertException(e); } }
From source file:com.boubei.tss.modules.license.LicenseFactory.java
/** * ?license???//from w w w. j a v a 2 s. co 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.signature = EasyUtils.encodeHex(sig.sign()); }
From source file:be.solidx.hot.utils.SSLUtils.java
protected static RSAPrivateKey generatePrivateKeyFromDER(byte[] keyBytes) throws InvalidKeySpecException, NoSuchAlgorithmException { PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(keyBytes); KeyFactory factory = KeyFactory.getInstance("RSA"); return (RSAPrivateKey) factory.generatePrivate(spec); }
From source file:org.hypoport.jwt.common.Toolbox.java
public static RSAPrivateKey readRSAPrivateKey(Reader keyReader) throws Exception { return (RSAPrivateKey) KeyFactory.getInstance("RSA") .generatePrivate(new PKCS8EncodedKeySpec(readPemFile(keyReader))); }