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);
}
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;
}
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;
}
Aggregations