List of usage examples for javax.crypto EncryptedPrivateKeyInfo EncryptedPrivateKeyInfo
public EncryptedPrivateKeyInfo(AlgorithmParameters algParams, byte[] encryptedData) throws NoSuchAlgorithmException
EncryptedPrivateKeyInfo
from the encryption algorithm parameters and the encrypted data. From source file:cherry.goods.crypto.RSASignatureTest.java
private RSASignature create2(char[] password) throws Exception { KeyPairGenerator keygen = KeyPairGenerator.getInstance("RSA"); keygen.initialize(2048);//from w w w.j av a 2 s .c o m KeyPair key = keygen.generateKeyPair(); String pbeAlgName = "PBEWithMD5AndDES"; PBEKeySpec pbeKeySpec = new PBEKeySpec(password); PBEParameterSpec pbeParamSpec = new PBEParameterSpec(RandomUtils.nextBytes(8), 20); SecretKey pbeKey = SecretKeyFactory.getInstance(pbeAlgName).generateSecret(pbeKeySpec); AlgorithmParameters pbeParam = AlgorithmParameters.getInstance(pbeAlgName); pbeParam.init(pbeParamSpec); Cipher cipher = Cipher.getInstance(pbeAlgName); cipher.init(Cipher.ENCRYPT_MODE, pbeKey, pbeParam); EncryptedPrivateKeyInfo encryptedKeyInfo = new EncryptedPrivateKeyInfo(pbeParam, cipher.doFinal(key.getPrivate().getEncoded())); RSASignature impl = new RSASignature(); impl.setAlgorithm("SHA256withRSA"); impl.setPublicKeyBytes(key.getPublic().getEncoded()); impl.setPrivateKeyBytes(encryptedKeyInfo.getEncoded(), password); return impl; }
From source file:cherry.goods.crypto.RSACryptoTest.java
private RSACrypto create2(char[] password) throws Exception { KeyPairGenerator keygen = KeyPairGenerator.getInstance("RSA"); keygen.initialize(2048);//from w ww .j av a 2 s .c o m KeyPair key = keygen.generateKeyPair(); String pbeAlgName = "PBEWithMD5AndDES"; PBEKeySpec pbeKeySpec = new PBEKeySpec(password); PBEParameterSpec pbeParamSpec = new PBEParameterSpec(RandomUtils.nextBytes(8), 20); SecretKey pbeKey = SecretKeyFactory.getInstance(pbeAlgName).generateSecret(pbeKeySpec); AlgorithmParameters pbeParam = AlgorithmParameters.getInstance(pbeAlgName); pbeParam.init(pbeParamSpec); Cipher cipher = Cipher.getInstance(pbeAlgName); cipher.init(Cipher.ENCRYPT_MODE, pbeKey, pbeParam); EncryptedPrivateKeyInfo encryptedKeyInfo = new EncryptedPrivateKeyInfo(pbeParam, cipher.doFinal(key.getPrivate().getEncoded())); RSACrypto impl = new RSACrypto(); impl.setAlgorithm("RSA/ECB/PKCS1Padding"); impl.setPublicKeyBytes(key.getPublic().getEncoded()); impl.setPrivateKeyBytes(encryptedKeyInfo.getEncoded(), password); return impl; }
From source file:net.sf.keystore_explorer.crypto.privatekey.Pkcs8Util.java
/** * PKCS #8 encode and encrypt a private key. * * @return The encrypted encoding/*from w w w . j ava2 s .c o m*/ * @param privateKey * The private key * @param pbeType * PBE algorithm to use for encryption * @param password * Encryption password * @throws CryptoException * Problem encountered while getting the encoded private key * @throws IOException * If an I/O error occurred */ public static byte[] getEncrypted(PrivateKey privateKey, Pkcs8PbeType pbeType, Password password) throws CryptoException, IOException { try { byte[] pkcs8 = get(privateKey); // Generate PBE secret key from password SecretKeyFactory keyFact = SecretKeyFactory.getInstance(pbeType.jce()); PBEKeySpec pbeKeySpec = new PBEKeySpec(password.toCharArray()); SecretKey pbeKey = keyFact.generateSecret(pbeKeySpec); // Generate random salt and iteration count byte[] salt = generateSalt(); int iterationCount = generateIterationCount(); // Store in algorithm parameters PBEParameterSpec pbeParameterSpec = new PBEParameterSpec(salt, iterationCount); AlgorithmParameters params = AlgorithmParameters.getInstance(pbeType.jce()); params.init(pbeParameterSpec); // Create PBE cipher from key and params Cipher cipher = Cipher.getInstance(pbeType.jce()); cipher.init(Cipher.ENCRYPT_MODE, pbeKey, params); // Encrypt key byte[] encPkcs8 = cipher.doFinal(pkcs8); // Create and return encrypted private key information EncryptedPrivateKeyInfo encPrivateKeyInfo = new EncryptedPrivateKeyInfo(params, encPkcs8); return encPrivateKeyInfo.getEncoded(); } catch (GeneralSecurityException ex) { throw new CryptoException("NoEncryptPkcs8PrivateKey.exception.message", ex); } }
From source file:org.tolven.config.model.CredentialManager.java
private void writeDER(char[] password, PrivateKey privateKey, File file) throws IOException, GeneralSecurityException { byte[] bytes = null; if (password == null) { bytes = privateKey.getEncoded(); } else {/* w ww . j ava 2s. com*/ SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES"); PBEKeySpec passwordSpec = new PBEKeySpec(password); SecretKey secretKey = secretKeyFactory.generateSecret(passwordSpec); Cipher cipher = Cipher.getInstance(secretKey.getAlgorithm()); cipher.init(Cipher.ENCRYPT_MODE, secretKey); byte[] encryptedPrivateKey = cipher.doFinal(privateKey.getEncoded()); EncryptedPrivateKeyInfo encryptedPrivateKeyInfo = new EncryptedPrivateKeyInfo(cipher.getParameters(), encryptedPrivateKey); bytes = encryptedPrivateKeyInfo.getEncoded(); } FileUtils.writeByteArrayToFile(file, bytes); }