List of usage examples for java.security.spec PKCS8EncodedKeySpec PKCS8EncodedKeySpec
public PKCS8EncodedKeySpec(byte[] encodedKey)
From source file:net.arccotangent.pacchat.filesystem.KeyManager.java
private static void saveKeys(PrivateKey privkey, PublicKey pubkey) { km_log.i("Saving keys to disk."); X509EncodedKeySpec pubSpec = new X509EncodedKeySpec(pubkey.getEncoded()); PKCS8EncodedKeySpec privSpec = new PKCS8EncodedKeySpec(privkey.getEncoded()); try {/* w w w.j a v a 2 s . c om*/ km_log.i(pubkeyFile.createNewFile() ? "Creation of public key file successful." : "Creation of public key file failed!"); FileOutputStream pubOut = new FileOutputStream(pubkeyFile); pubOut.write(Base64.encodeBase64(pubSpec.getEncoded())); pubOut.flush(); pubOut.close(); } catch (IOException e) { km_log.e("Error while saving public key!"); e.printStackTrace(); } try { km_log.i(privkeyFile.createNewFile() ? "Creation of private key file successful." : "Creation of private key file failed!"); FileOutputStream privOut = new FileOutputStream(privkeyFile); privOut.write(Base64.encodeBase64(privSpec.getEncoded())); privOut.flush(); privOut.close(); } catch (IOException e) { km_log.e("Error while saving private key!"); e.printStackTrace(); } km_log.i("Finished saving keys to disk. Operation appears successful."); }
From source file:com.vexsoftware.votifier.util.rsa.RSAIO.java
/** * Saves the key pair to the disk./*from w ww . j a v a 2s . c o m*/ * * @param directory * The directory to save to * @param keyPair * The key pair to save * @throws Exception * If an error occurs */ public static void save(File directory, KeyPair keyPair) throws Exception { PrivateKey privateKey = keyPair.getPrivate(); PublicKey publicKey = keyPair.getPublic(); // Store the public key. X509EncodedKeySpec publicSpec = new X509EncodedKeySpec(publicKey.getEncoded()); FileOutputStream out = null; try { out = new FileOutputStream(directory + "/public.key"); out.write(DatatypeConverter.printBase64Binary(publicSpec.getEncoded()).getBytes()); } finally { try { out.close(); } catch (Exception exception) { // ignore } } // Store the private key. PKCS8EncodedKeySpec privateSpec = new PKCS8EncodedKeySpec(privateKey.getEncoded()); try { out = new FileOutputStream(directory + "/private.key"); out.write(DatatypeConverter.printBase64Binary(privateSpec.getEncoded()).getBytes()); } finally { try { out.close(); } catch (Exception exception) { // ignore } } }
From source file:org.hypoport.jwt.common.Toolbox.java
public static ECPrivateKey readECDSAPrivateKey(Reader keyReader) throws Exception { return (ECPrivateKey) KeyFactory.getInstance("EC") .generatePrivate(new PKCS8EncodedKeySpec(readPemFile(keyReader))); }
From source file:cn.lynx.emi.license.GenerateLicense.java
private static final String encrypt(String key, String data) { byte[] corekey = Base64.decodeBase64(key); PKCS8EncodedKeySpec pkspec = new PKCS8EncodedKeySpec(corekey); try {//from w w w .jav a 2s.co m KeyFactory keyFactory = KeyFactory.getInstance("RSA"); Key privateKey = keyFactory.generatePrivate(pkspec); Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm()); cipher.init(Cipher.ENCRYPT_MODE, privateKey); byte[] encData = cipher.doFinal(data.getBytes("UTF-8")); System.out.println("after encrypt, len=" + encData.length); return Base64.encodeBase64String(encData); } catch (Exception e) { e.printStackTrace(); return null; } }
From source file:hh.learnj.test.license.test.rsacoder.RSACoder.java
/** * ?//from ww w . j a va 2s . com * * @param data? * @param key * * @return byte[] ? */ public static byte[] encryptByPrivateKey(byte[] data, byte[] key) throws Exception { // ?? PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(key); KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM); // ?? PrivateKey privateKey = keyFactory.generatePrivate(pkcs8KeySpec); // ? Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm()); cipher.init(Cipher.ENCRYPT_MODE, privateKey); return cipher.doFinal(data); }
From source file:facturatron.facturacion.PAC.finkok.ClassicKeyLoader.java
/** * @param crtInputStream Flujo de entrada del certificado del cual se obtiene la llave privada * @param passwd Contrasea con la cual se puede obtener la informacin de la llave * privada//from w w w. j a v a2 s .c o m * * @return Llave privada encapsulada en el objeto {@link PrivateKey} * * @throws KeyException Lanzada si existe un problema con la lectura de la llave privada. La * excepcin es lanzada si alguno de estos casos se presenta: * <ul> * <li> * Error de lectura del flujo de entrada del documento. * </li> * <li> * Error en la obtencn de la informacin de la llave privada debido * a que la contrasea no es correcta. * </li> * <li> * Error en la obtencin de la llave privada debido a que el algoritmo * de cifrado no es el adecuado para el certificado. * </li> * </ul> */ public static PrivateKey loadPKCS8PrivateKey(InputStream crtInputStream, String passwd) throws KeyException { byte[] decrypted = null; PrivateKey privateKey = null; try { decrypted = (passwd != null) ? getCertBytes(crtInputStream, passwd.toCharArray()) : getBytes(crtInputStream); } catch (IOException ioe) { throw new KeyException("Error de E/S al leer la informacin del certificado", ioe.getCause()); } PKCS8EncodedKeySpec keysp = new PKCS8EncodedKeySpec(decrypted); try { KeyFactory kf = KeyFactory.getInstance("RSA"); privateKey = kf.generatePrivate(keysp); } catch (GeneralSecurityException gse) { throw new KeyException("Error al obtener la informacin del certificado debido a su codificacin", gse.getCause()); } return privateKey; }
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 w w . ja v a2 s. c om keyAgree.doPhase(pubKey, true); return keyAgree.generateSecret(AES).getEncoded(); }
From source file:net.nicholaswilliams.java.licensing.encryption.KeyFileUtilities.java
protected static byte[] writeEncryptedPrivateKey(PrivateKey privateKey, char[] passphrase) { PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(privateKey.getEncoded()); return Encryptor.encryptRaw(pkcs8EncodedKeySpec.getEncoded(), passphrase); }
From source file:com.shenit.commons.codec.RsaUtils.java
/** * RSA??/* w w w . j a v a 2 s . com*/ * * @param content * ??? * @param privateKey * ? * @param input_charset * ?? * @return ?? */ public static String sign(String content, String privateKey, String algorithm, String input_charset) { try { PKCS8EncodedKeySpec priPKCS8 = new PKCS8EncodedKeySpec(Base64.decodeBase64(privateKey)); KeyFactory keyf = KeyFactory.getInstance(CODEC_RSA); PrivateKey priKey = keyf.generatePrivate(priPKCS8); Signature signature = Signature.getInstance(algorithm); signature.initSign(priKey); signature.update(content.getBytes(input_charset)); byte[] signed = signature.sign(); return Base64Utils.base64EncodeHex(signed); } catch (Exception e) { if (LOG.isWarnEnabled()) LOG.warn("[sign] could not sign with exception", e); } return null; }
From source file:org.syphr.utils.x509.X509Utils.java
/** * Create a signature using the given token and private key. * * @param message//from w w w. j ava2 s . c o m * the message to sign * @param key * the private key to use to create the signature (this must be * PKCS8 encoded) * @param keyAlg * the algorithm used to create the private key * @param sigAlg * the algorithm to use to create the signature * @return the signature * @throws IOException * if there is an error reading the key * @throws InvalidKeySpecException * if the key algorithm is not appropriate for the given private * key * @throws InvalidKeyException * if the given private key is not valid * @throws SignatureException * if there is an error while generating the signature */ public static byte[] sign(String message, InputStream key, KeyAlgorithm keyAlg, SignatureAlgorithm sigAlg) throws IOException, InvalidKeySpecException, InvalidKeyException, SignatureException { KeySpec privateKeySpec = new PKCS8EncodedKeySpec(IOUtils.toByteArray(key)); try { KeyFactory keyFactory = KeyFactory.getInstance(keyAlg.getAlgorithm()); PrivateKey privateKey = keyFactory.generatePrivate(privateKeySpec); Signature sig = Signature.getInstance(sigAlg.getAlgorithm()); sig.initSign(privateKey); sig.update(message.getBytes()); return sig.sign(); } catch (NoSuchAlgorithmException e) { /* * This is protected against by enforcing specific algorithm * choices. */ throw new IllegalArgumentException("Unknown algorithm", e); } }