Search in sources :

Example 1 with VMPCRandomGenerator

use of org.bouncycastle.crypto.prng.VMPCRandomGenerator in project nhin-d by DirectProject.

the class CertGenerator method writeCertAndKey.

private static void writeCertAndKey(X509Certificate cert, PrivateKey key, CertCreateFields fields) throws Exception {
    // write the cert
    FileUtils.writeByteArrayToFile(fields.getNewCertFile(), cert.getEncoded());
    if (fields.getNewPassword() == null || fields.getNewPassword().length == 0) {
        // no password... just write the file 
        FileUtils.writeByteArrayToFile(fields.getNewKeyFile(), key.getEncoded());
    } else {
        // encypt it, then write it
        // prime the salts
        byte[] salt = new byte[8];
        VMPCRandomGenerator ranGen = new VMPCRandomGenerator();
        ranGen.addSeedMaterial(new SecureRandom().nextLong());
        ranGen.nextBytes(salt);
        // create PBE parameters from salt and iteration count
        PBEParameterSpec pbeSpec = new PBEParameterSpec(salt, 20);
        PBEKeySpec pbeKeySpec = new PBEKeySpec(fields.getNewPassword());
        SecretKey sKey = SecretKeyFactory.getInstance("PBEWithMD5AndDES", CryptoExtensions.getJCEProviderName()).generateSecret(pbeKeySpec);
        // encrypt
        Cipher cipher = Cipher.getInstance("PBEWithMD5AndDES", CryptoExtensions.getJCEProviderName());
        cipher.init(Cipher.ENCRYPT_MODE, sKey, pbeSpec, null);
        byte[] plain = (byte[]) key.getEncoded();
        byte[] encrKey = cipher.doFinal(plain, 0, plain.length);
        // set the algorithm parameters
        AlgorithmParameters pbeParams = AlgorithmParameters.getInstance(PBE_WITH_MD5_AND_DES_CBC_OID, Security.getProvider("SunJCE"));
        pbeParams.init(pbeSpec);
        // place in a EncryptedPrivateKeyInfo to encode to the proper file format
        EncryptedPrivateKeyInfo info = new EncryptedPrivateKeyInfo(pbeParams, encrKey);
        // now write it to the file
        FileUtils.writeByteArrayToFile(fields.getNewKeyFile(), info.getEncoded());
    }
    if (fields.getSignerCert() == null)
        fields.setSignerCert(cert);
    if (fields.getSignerKey() == null)
        fields.setSignerKey(key);
}
Also used : PBEKeySpec(javax.crypto.spec.PBEKeySpec) SecretKey(javax.crypto.SecretKey) VMPCRandomGenerator(org.bouncycastle.crypto.prng.VMPCRandomGenerator) SecureRandom(java.security.SecureRandom) EncryptedPrivateKeyInfo(javax.crypto.EncryptedPrivateKeyInfo) Cipher(javax.crypto.Cipher) PBEParameterSpec(javax.crypto.spec.PBEParameterSpec) AlgorithmParameters(java.security.AlgorithmParameters)

Example 2 with VMPCRandomGenerator

use of org.bouncycastle.crypto.prng.VMPCRandomGenerator in project nhin-d by DirectProject.

the class PKCS11Commands method generatePositiveRandom.

private static long generatePositiveRandom() {
    Random ranGen;
    long retVal = -1;
    byte[] seed = new byte[8];
    VMPCRandomGenerator seedGen = new VMPCRandomGenerator();
    seedGen.addSeedMaterial(new SecureRandom().nextLong());
    seedGen.nextBytes(seed);
    ranGen = new SecureRandom(seed);
    while (retVal < 1) {
        retVal = ranGen.nextLong();
    }
    return retVal;
}
Also used : Random(java.util.Random) SecureRandom(java.security.SecureRandom) VMPCRandomGenerator(org.bouncycastle.crypto.prng.VMPCRandomGenerator) SecureRandom(java.security.SecureRandom)

Example 3 with VMPCRandomGenerator

use of org.bouncycastle.crypto.prng.VMPCRandomGenerator in project nhin-d by DirectProject.

the class CertGenerator method generatePositiveRandom.

public static long generatePositiveRandom() {
    Random ranGen;
    long retVal = -1;
    byte[] seed = new byte[8];
    VMPCRandomGenerator seedGen = new VMPCRandomGenerator();
    seedGen.addSeedMaterial(new SecureRandom().nextLong());
    seedGen.nextBytes(seed);
    ranGen = new SecureRandom(seed);
    while (retVal < 1) {
        retVal = ranGen.nextLong();
    }
    return retVal;
}
Also used : Random(java.util.Random) SecureRandom(java.security.SecureRandom) VMPCRandomGenerator(org.bouncycastle.crypto.prng.VMPCRandomGenerator) SecureRandom(java.security.SecureRandom)

Aggregations

SecureRandom (java.security.SecureRandom)3 VMPCRandomGenerator (org.bouncycastle.crypto.prng.VMPCRandomGenerator)3 Random (java.util.Random)2 AlgorithmParameters (java.security.AlgorithmParameters)1 Cipher (javax.crypto.Cipher)1 EncryptedPrivateKeyInfo (javax.crypto.EncryptedPrivateKeyInfo)1 SecretKey (javax.crypto.SecretKey)1 PBEKeySpec (javax.crypto.spec.PBEKeySpec)1 PBEParameterSpec (javax.crypto.spec.PBEParameterSpec)1