Example usage for javax.crypto EncryptedPrivateKeyInfo getEncoded

List of usage examples for javax.crypto EncryptedPrivateKeyInfo getEncoded

Introduction

In this page you can find the example usage for javax.crypto EncryptedPrivateKeyInfo getEncoded.

Prototype

public byte[] getEncoded() throws IOException 

Source Link

Document

Returns the ASN.1 encoding of this object.

Usage

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:cherry.goods.crypto.RSASignatureTest.java

private RSASignature create2(char[] password) throws Exception {

    KeyPairGenerator keygen = KeyPairGenerator.getInstance("RSA");
    keygen.initialize(2048);//w ww.j a  v a2  s .  c om
    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);//  ww  w . j a v  a 2  s. co  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: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 {//from  w w  w  .  java  2  s. c o  m
        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);
}