Skip to content

Instantly share code, notes, and snippets.

@kbsriram
Created March 13, 2012 18:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kbsriram/2030426 to your computer and use it in GitHub Desktop.
Save kbsriram/2030426 to your computer and use it in GitHub Desktop.
private static void genCert
(String id, String pass, File pubfile, File secfile)
throws Exception
{
KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA", "BC");
kpg.initialize(2048);
KeyPair sign_pair = kpg.generateKeyPair();
KeyPair enc_pair = kpg.generateKeyPair();
FileOutputStream pubout = new FileOutputStream(pubfile);
FileOutputStream secout = new FileOutputStream(secfile);
exportKP(secout, pubout, sign_pair,
enc_pair, id, pass.toCharArray());
}
private static void exportKP
(OutputStream secout, OutputStream pubout, KeyPair sign_kp,
KeyPair enc_kp, String id, char[] pass)
throws Exception
{
secout = new ArmoredOutputStream(secout);
pubout = new ArmoredOutputStream(pubout);
PGPKeyPair rsakp_sign =
new PGPKeyPair(PGPPublicKey.RSA_SIGN, sign_kp, new Date());
PGPKeyPair rsakp_enc =
new PGPKeyPair(PGPPublicKey.RSA_ENCRYPT, enc_kp, new Date());
PGPDigestCalculator sha1Calc =
new BcPGPDigestCalculatorProvider()
.get(HashAlgorithmTags.SHA1);
// Set up some defaults.
PGPSignatureSubpacketGenerator hashgen =
new PGPSignatureSubpacketGenerator();
hashgen.setPreferredSymmetricAlgorithms
(false, new int[] { SymmetricKeyAlgorithmTags.AES_256 });
hashgen.setFeature(false, Features.FEATURE_MODIFICATION_DETECTION);
PGPKeyRingGenerator keyRingGen =
new PGPKeyRingGenerator
(PGPSignature.POSITIVE_CERTIFICATION, rsakp_sign,
id, sha1Calc, hashgen.generate(), null,
new BcPGPContentSignerBuilder
(rsakp_sign.getPublicKey().getAlgorithm(), HashAlgorithmTags.SHA1),
new BcPBESecretKeyEncryptorBuilder
(PGPEncryptedData.AES_256, sha1Calc).build(pass));
keyRingGen.addSubKey(rsakp_enc);
PGPSecretKeyRing secretkr = keyRingGen.generateSecretKeyRing();
secretkr.encode(secout);
secout.close();
keyRingGen.generatePublicKeyRing().encode(pubout);
pubout.close();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment