List of usage examples for java.security.spec PKCS8EncodedKeySpec PKCS8EncodedKeySpec
public PKCS8EncodedKeySpec(byte[] encodedKey)
From source file:org.commonjava.maven.galley.transport.htcli.internal.SSLUtils.java
public static KeyStore readKeyAndCert(final String pemContent, final String keyPass) throws CertificateException, IOException, KeyStoreException, NoSuchAlgorithmException, InvalidKeySpecException { final KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType()); ks.load(null);//www .ja v a 2s. com final CertificateFactory certFactory = CertificateFactory.getInstance("X.509"); final KeyFactory keyFactory = KeyFactory.getInstance("RSA"); final List<String> lines = readLines(pemContent); String currentHeader = null; final StringBuilder current = new StringBuilder(); final Map<String, String> entries = new LinkedHashMap<String, String>(); for (final String line : lines) { if (line == null) { continue; } if (line.startsWith("-----BEGIN")) { currentHeader = line.trim(); current.setLength(0); } else if (line.startsWith("-----END")) { entries.put(currentHeader, current.toString()); } else { current.append(line.trim()); } } final List<Certificate> certs = new ArrayList<Certificate>(); for (int pass = 0; pass < 2; pass++) { for (final Map.Entry<String, String> entry : entries.entrySet()) { final String header = entry.getKey(); final byte[] data = decodeBase64(entry.getValue()); if (pass > 0 && header.contains("BEGIN PRIVATE KEY")) { final KeySpec spec = new PKCS8EncodedKeySpec(data); final PrivateKey key = keyFactory.generatePrivate(spec); ks.setKeyEntry("key", key, keyPass.toCharArray(), certs.toArray(new Certificate[] {})); } else if (pass < 1 && header.contains("BEGIN CERTIFICATE")) { final Certificate c = certFactory.generateCertificate(new ByteArrayInputStream(data)); ks.setCertificateEntry("certificate", c); certs.add(c); } } } return ks; }
From source file:net.networksaremadeofstring.cyllell.Authentication.java
private String SignHeaders(String dataToSign) throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException, NoSuchProviderException { PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(Base64.decode(this.PrivateKey.getBytes(), 0)); KeyFactory kf = KeyFactory.getInstance("RSA"); PrivateKey pk = kf.generatePrivate(spec); Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1PADDING"); cipher.init(Cipher.ENCRYPT_MODE, pk); byte[] EncryptedStream = new byte[cipher.getOutputSize(dataToSign.length())]; try {/* w w w . ja va2 s . c o m*/ cipher.doFinal(dataToSign.getBytes(), 0, dataToSign.length(), EncryptedStream, 0); } catch (ShortBufferException e) { // TODO Auto-generated catch block e.printStackTrace(); } return Base64.encodeToString(EncryptedStream, Base64.NO_WRAP); }
From source file:net.sf.zekr.common.util.CryptoUtils.java
public static byte[] sign(byte[] text, byte[] prvKeyBytes) throws GeneralSecurityException { PKCS8EncodedKeySpec prvSpec = new PKCS8EncodedKeySpec(prvKeyBytes); KeyFactory keyFactory = KeyFactory.getInstance("DSA"); PrivateKey prvKey = keyFactory.generatePrivate(prvSpec); Signature sig = Signature.getInstance("SHA1withDSA"); sig.initSign(prvKey);//from w w w . ja v a2 s . c o m sig.update(text); return sig.sign(); }
From source file:com.vexsoftware.votifier.crypto.RSAIO.java
/** * Loads an RSA key pair from a directory. The directory must have the files * "public.key" and "private.key"./* ww w . j a v a 2 s. c om*/ * * @param directory * The directory to load from * @return The key pair * @throws Exception * If an error occurs */ public static KeyPair load(File directory) throws Exception { // Read the public key file. File publicKeyFile = new File(directory + "/public.key"); byte[] encodedPublicKey = FileUtils.readFileToByteArray(publicKeyFile); encodedPublicKey = DatatypeConverter.parseBase64Binary(new String(encodedPublicKey)); // Read the private key file. File privateKeyFile = new File(directory + "/private.key"); byte[] encodedPrivateKey = FileUtils.readFileToByteArray(privateKeyFile); encodedPrivateKey = DatatypeConverter.parseBase64Binary(new String(encodedPrivateKey)); // Instantiate and return the key pair. KeyFactory keyFactory = KeyFactory.getInstance("RSA"); X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(encodedPublicKey); PublicKey publicKey = keyFactory.generatePublic(publicKeySpec); PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(encodedPrivateKey); PrivateKey privateKey = keyFactory.generatePrivate(privateKeySpec); return new KeyPair(publicKey, privateKey); }
From source file:architecture.common.license.LicenseSigner.java
protected void init(Reader keyReader) throws IOException, NoSuchAlgorithmException, DecoderException, InvalidKeySpecException, InvalidKeyException { BufferedReader in = new BufferedReader(keyReader); String privateKey = in.readLine(); in.close();//from w w w .j av a 2 s. c o m KeyFactory keyFactory = KeyFactory.getInstance("DSA"); sig = Signature.getInstance("SHA1withDSA"); PKCS8EncodedKeySpec privKeySpec = new PKCS8EncodedKeySpec(Hex.decodeHex(privateKey.toCharArray())); java.security.PrivateKey privKey = keyFactory.generatePrivate(privKeySpec); sig.initSign(privKey); }
From source file:org.picketlink.pki.internal.DefaultDecryptionAuthority.java
@Override public String decrypt(String cipherText, String privateKeyPath, String transformation, String encoding) { String decryptedText = null;//from w ww . j a v a 2 s.c o m try { PKCS8EncodedKeySpec pKCS8EncodedKeySpec = new PKCS8EncodedKeySpec( IOUtils.toByteArray(new FileInputStream(privateKeyPath))); Cipher cipher = Cipher.getInstance(transformation); cipher.init(Cipher.DECRYPT_MODE, KeyFactory.getInstance("RSA").generatePrivate(pKCS8EncodedKeySpec)); decryptedText = new String(Base64.decodeBase64(cipher.doFinal(cipherText.getBytes(encoding)))); } catch (Exception e) { throw new RuntimeException("Could not decrypt cipherText.", e); } return decryptedText; }
From source file:com.teasoft.teavote.util.Signature.java
private PrivateKey getPrivateKey() throws IOException, NoSuchAlgorithmException, InvalidKeySpecException { Resource resource = res.getResource("classpath:xormeSafui"); byte[] privKeyBytes; try (InputStream privKeyInputStream = resource.getInputStream()) { privKeyBytes = IOUtils.toByteArray(privKeyInputStream); privKeyBytes = Base64.decodeBase64(privKeyBytes); }//w w w . j a v a2s . c om PKCS8EncodedKeySpec privKeySpec = new PKCS8EncodedKeySpec(privKeyBytes); KeyFactory keyFactory = KeyFactory.getInstance("DSA"); PrivateKey privKey = keyFactory.generatePrivate(privKeySpec); return privKey; }
From source file:nl.surfnet.spring.security.opensaml.util.KeyStoreUtil.java
/** * Append a certificate and private key to a keystore. * * @param keyStore where to append the certificate and private key to * @param keyAlias the alias of the key * @param certificateInputStream the inputStream containing the certificate in the PEM format * @param privatekeyInputStream the input stream containing the private key in the DER format * @param password the password on the key * <p/>// w w w . j av a 2 s . c o m * Generate your private key: openssl genrsa -out something.key 1024 * <p/> * Show the PEM private key: openssl asn1parse -inform pem -dump -i * -in something.key * <p/> * Translate the key to pkcs8 DER format: openssl pkcs8 -topk8 * -inform PEM -outform DER -in something.key -nocrypt > * something.pkcs8.der * <p/> * Show the DER private key: openssl asn1parse -inform der -dump -i * -in something.pkcs8.der * <p/> * Generate a certificate request: openssl req -new -key * something.key -out something.csr * <p/> * Generate a certificate: openssl x509 -req -days 365 -in * something.csr -signkey something.key -out something.crt */ public static void appendKeyToKeyStore(KeyStore keyStore, String keyAlias, InputStream certificateInputStream, InputStream privatekeyInputStream, char[] password) throws IOException { CertificateFactory certFact; Certificate cert; try { certFact = CertificateFactory.getInstance("X.509"); cert = certFact.generateCertificate(certificateInputStream); } catch (CertificateException e) { throw new RuntimeException("Could not instantiate cert", e); } ArrayList<Certificate> certs = new ArrayList<Certificate>(); certs.add(cert); byte[] privKeyBytes = IOUtils.toByteArray(privatekeyInputStream); try { KeySpec ks = new PKCS8EncodedKeySpec(privKeyBytes); RSAPrivateKey privKey = (RSAPrivateKey) KeyFactory.getInstance("RSA").generatePrivate(ks); keyStore.setKeyEntry(keyAlias, privKey, password, certs.toArray(new Certificate[certs.size()])); } catch (InvalidKeySpecException e) { throw new RuntimeException(e); } catch (KeyStoreException e) { throw new RuntimeException(e); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); } }
From source file:be.fedict.eid.idp.model.CryptoUtil.java
public static PrivateKey getPrivate(byte[] keyBytes) throws KeyLoadException { // try DSA/*from w ww. j a v a2 s.com*/ try { KeyFactory dsaKeyFactory = KeyFactory.getInstance("DSA"); PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(keyBytes); try { return dsaKeyFactory.generatePrivate(privateKeySpec); } catch (InvalidKeySpecException e) { // try RSA KeyFactory rsaKeyFactory = KeyFactory.getInstance("RSA"); try { return rsaKeyFactory.generatePrivate(privateKeySpec); } catch (InvalidKeySpecException e1) { throw new KeyLoadException(e); } } } catch (NoSuchAlgorithmException e) { throw new KeyLoadException(e); } }
From source file:cn.util.RSAUtils.java
/** * ?RSA?//from ww w. ja v a 2s. c o m * ?????RSA/None/PKCS1Padding??JDK?????AndroidRSA * /None/NoPadding * * @param modulus * * @param exponent * * @return */ public static RSAPrivateKey getPrivateKey() { try { return (RSAPrivateKey) KeyFactory.getInstance("RSA") .generatePrivate(new PKCS8EncodedKeySpec(privateKeyData)); } catch (Exception e) { e.printStackTrace(); return null; } }