import org.bouncycastle.openpgp.PGPKeyRingGenerator; //导入依赖的package包/类
/**
*
* @param dsaKeyPair - the generated DSA key pair
* @param elGamalKeyPair - the generated El Gamal key pair
* @param identity - the given identity of the key pair ring
* @param passphrase - the secret pass phrase to protect the key pair
* @return a PGP Key Ring Generate with the El Gamal key pair added as sub key
* @throws Exception
*/
public static final PGPKeyRingGenerator createPGPKeyRingGenerator(KeyPair dsaKeyPair, KeyPair elGamalKeyPair, String identity, char[] passphrase) throws Exception {
PGPKeyPair dsaPgpKeyPair = new JcaPGPKeyPair(PGPPublicKey.DSA, dsaKeyPair, new Date());
PGPKeyPair elGamalPgpKeyPair = new JcaPGPKeyPair(PGPPublicKey.ELGAMAL_ENCRYPT, elGamalKeyPair, new Date());
PGPDigestCalculator sha1Calc = new JcaPGPDigestCalculatorProviderBuilder().build().get(HashAlgorithmTags.SHA1);
PGPKeyRingGenerator keyRingGen = new PGPKeyRingGenerator(
PGPSignature.POSITIVE_CERTIFICATION,
dsaPgpKeyPair,
identity,
sha1Calc,
null,
null,
new JcaPGPContentSignerBuilder(dsaPgpKeyPair.getPublicKey().getAlgorithm(), HashAlgorithmTags.SHA1),
new JcePBESecretKeyEncryptorBuilder(PGPEncryptedData.AES_256, sha1Calc).setProvider("BC").build(passphrase)
);
keyRingGen.addSubKey(elGamalPgpKeyPair);
return keyRingGen;
}
import org.bouncycastle.openpgp.PGPKeyRingGenerator; //导入依赖的package包/类
public SecretKey generateKeyPair(final String id, final char[] pass) throws CryptoException {
try {
// This object generates individual key-pairs.
final RSAKeyPairGenerator kpg = new RSAKeyPairGenerator();
kpg.init(new RSAKeyGenerationParameters(BigInteger.valueOf(0x10001), new SecureRandom(), 2048, 12));
// First create the master (signing) key with the generator.
final PGPKeyPair keyPair = new BcPGPKeyPair(PGPPublicKey.RSA_GENERAL, kpg.generateKeyPair(), new Date());
// Add a self-signature on the id
final PGPSignatureSubpacketGenerator signhashgen = new PGPSignatureSubpacketGenerator();
signhashgen.setKeyFlags(true, KeyFlags.CERTIFY_OTHER | KeyFlags.SIGN_DATA | KeyFlags.ENCRYPT_COMMS | KeyFlags.ENCRYPT_STORAGE);
signhashgen.setPreferredCompressionAlgorithms(false, new int[] { CompressionAlgorithmTags.ZIP });
signhashgen.setPreferredHashAlgorithms(false, new int[] { HashAlgorithmTags.SHA1 });
signhashgen.setPreferredSymmetricAlgorithms(false, new int[] { SymmetricKeyAlgorithmTags.AES_256 });
signhashgen.setFeature(false, Features.FEATURE_MODIFICATION_DETECTION);
// Create a signature on the encryption subkey.
final PGPSignatureSubpacketGenerator enchashgen = new PGPSignatureSubpacketGenerator();
enchashgen.setKeyFlags(false, KeyFlags.ENCRYPT_COMMS | KeyFlags.ENCRYPT_STORAGE);
// Objects used to encrypt the secret key.
// Finally, create the keyring itself. The constructor
// takes parameters that allow it to generate the self
// signature.
final PGPDigestCalculator sha1Calc = new BcPGPDigestCalculatorProvider().get(HashAlgorithmTags.SHA1);
final PBESecretKeyEncryptor secretKeyEncryptor = new BcPBESecretKeyEncryptorBuilder(PGPEncryptedData.AES_128, sha1Calc).build(pass);
final BcPGPContentSignerBuilder contentSigner = new BcPGPContentSignerBuilder(keyPair.getPublicKey().getAlgorithm(), HashAlgorithmTags.SHA1);
final PGPKeyRingGenerator keyRingGen = new PGPKeyRingGenerator(PGPSignature.POSITIVE_CERTIFICATION, keyPair, id, sha1Calc,
signhashgen.generate(), null, contentSigner, secretKeyEncryptor);
// return new SimpleKeyPair(new BcPGPPublicKey(publicKeyRing.getPublicKey()),
return new BcPGPSecretKey(keyRingGen.generateSecretKeyRing().getSecretKey());
}
catch (final Exception e) {
throw new CryptoException(e);
}
}
import org.bouncycastle.openpgp.PGPKeyRingGenerator; //导入依赖的package包/类
public void generateTest()
throws Exception
{
char[] passPhrase = "hello".toCharArray();
DSAParametersGenerator dsaPGen = new DSAParametersGenerator();
dsaPGen.init(512, 10, new SecureRandom());
DSAKeyPairGenerator dsaKpg = new DSAKeyPairGenerator();
dsaKpg.init(new DSAKeyGenerationParameters(new SecureRandom(), dsaPGen.generateParameters()));
//
// this takes a while as the key generator has to generate some DSA params
// before it generates the key.
//
AsymmetricCipherKeyPair dsaKp = dsaKpg.generateKeyPair();
ElGamalKeyPairGenerator elgKpg = new ElGamalKeyPairGenerator();
BigInteger g = new BigInteger("153d5d6172adb43045b68ae8e1de1070b6137005686d29d3d73a7749199681ee5b212c9b96bfdcfa5b20cd5e3fd2044895d609cf9b410b7a0f12ca1cb9a428cc", 16);
BigInteger p = new BigInteger("9494fec095f3b85ee286542b3836fc81a5dd0a0349b4c239dd38744d488cf8e31db8bcb7d33b41abb9e5a33cca9144b1cef332c94bf0573bf047a3aca98cdf3b", 16);
ElGamalParameters elParams = new ElGamalParameters(p, g);
elgKpg.init(new ElGamalKeyGenerationParameters(new SecureRandom(), elParams));
//
// this is quicker because we are using pregenerated parameters.
//
AsymmetricCipherKeyPair elgKp = elgKpg.generateKeyPair();
PGPKeyPair dsaKeyPair = new BcPGPKeyPair(PGPPublicKey.DSA, dsaKp, new Date());
PGPKeyPair elgKeyPair = new BcPGPKeyPair(PGPPublicKey.ELGAMAL_ENCRYPT, elgKp, new Date());
PGPKeyRingGenerator keyRingGen = new PGPKeyRingGenerator(PGPSignature.POSITIVE_CERTIFICATION, dsaKeyPair,
"test", null, null, null, new BcPGPContentSignerBuilder(PGPPublicKey.DSA, HashAlgorithmTags.SHA1), new BcPBESecretKeyEncryptorBuilder(PGPEncryptedData.AES_256).build(passPhrase));
keyRingGen.addSubKey(elgKeyPair);
PGPSecretKeyRing keyRing = keyRingGen.generateSecretKeyRing();
keyRing.getSecretKey().extractPrivateKey(new BcPBESecretKeyDecryptorBuilder(new BcPGPDigestCalculatorProvider()).build(passPhrase));
PGPPublicKeyRing pubRing = keyRingGen.generatePublicKeyRing();
PGPPublicKey vKey = null;
PGPPublicKey sKey = null;
Iterator it = pubRing.getPublicKeys();
while (it.hasNext())
{
PGPPublicKey pk = (PGPPublicKey)it.next();
if (pk.isMasterKey())
{
vKey = pk;
}
else
{
sKey = pk;
}
}
Iterator sIt = sKey.getSignatures();
while (sIt.hasNext())
{
PGPSignature sig = (PGPSignature)sIt.next();
if (sig.getKeyID() == vKey.getKeyID()
&& sig.getSignatureType() == PGPSignature.SUBKEY_BINDING)
{
sig.init(new BcPGPContentVerifierBuilderProvider(), vKey);
if (!sig.verifyCertification(vKey, sKey))
{
fail("failed to verify sub-key signature.");
}
}
}
}
import org.bouncycastle.openpgp.PGPKeyRingGenerator; //导入依赖的package包/类
public void generateSha1Test()
throws Exception
{
char[] passPhrase = "hello".toCharArray();
DSAParametersGenerator dsaPGen = new DSAParametersGenerator();
dsaPGen.init(512, 10, new SecureRandom());
DSAKeyPairGenerator dsaKpg = new DSAKeyPairGenerator();
dsaKpg.init(new DSAKeyGenerationParameters(new SecureRandom(), dsaPGen.generateParameters()));
//
// this takes a while as the key generator has to generate some DSA params
// before it generates the key.
//
AsymmetricCipherKeyPair dsaKp = dsaKpg.generateKeyPair();
ElGamalKeyPairGenerator elgKpg = new ElGamalKeyPairGenerator();
BigInteger g = new BigInteger("153d5d6172adb43045b68ae8e1de1070b6137005686d29d3d73a7749199681ee5b212c9b96bfdcfa5b20cd5e3fd2044895d609cf9b410b7a0f12ca1cb9a428cc", 16);
BigInteger p = new BigInteger("9494fec095f3b85ee286542b3836fc81a5dd0a0349b4c239dd38744d488cf8e31db8bcb7d33b41abb9e5a33cca9144b1cef332c94bf0573bf047a3aca98cdf3b", 16);
ElGamalParameters elParams = new ElGamalParameters(p, g);
elgKpg.init(new ElGamalKeyGenerationParameters(new SecureRandom(), elParams));
//
// this is quicker because we are using pregenerated parameters.
//
AsymmetricCipherKeyPair elgKp = elgKpg.generateKeyPair();
PGPKeyPair dsaKeyPair = new BcPGPKeyPair(PGPPublicKey.DSA, dsaKp, new Date());
PGPKeyPair elgKeyPair = new BcPGPKeyPair(PGPPublicKey.ELGAMAL_ENCRYPT, elgKp, new Date());
PGPDigestCalculator chkSumCalc = new BcPGPDigestCalculatorProvider().get(HashAlgorithmTags.SHA1);
PGPKeyRingGenerator keyRingGen = new PGPKeyRingGenerator(PGPSignature.POSITIVE_CERTIFICATION, dsaKeyPair,
"test", chkSumCalc, null, null, new BcPGPContentSignerBuilder(PGPPublicKey.DSA, HashAlgorithmTags.SHA1), new BcPBESecretKeyEncryptorBuilder(PGPEncryptedData.AES_256).build(passPhrase));
keyRingGen.addSubKey(elgKeyPair);
PGPSecretKeyRing keyRing = keyRingGen.generateSecretKeyRing();
keyRing.getSecretKey().extractPrivateKey(new BcPBESecretKeyDecryptorBuilder(new BcPGPDigestCalculatorProvider()).build(passPhrase));
PGPPublicKeyRing pubRing = keyRingGen.generatePublicKeyRing();
PGPPublicKey vKey = null;
PGPPublicKey sKey = null;
Iterator it = pubRing.getPublicKeys();
while (it.hasNext())
{
PGPPublicKey pk = (PGPPublicKey)it.next();
if (pk.isMasterKey())
{
vKey = pk;
}
else
{
sKey = pk;
}
}
Iterator sIt = sKey.getSignatures();
while (sIt.hasNext())
{
PGPSignature sig = (PGPSignature)sIt.next();
if (sig.getKeyID() == vKey.getKeyID()
&& sig.getSignatureType() == PGPSignature.SUBKEY_BINDING)
{
sig.init(new BcPGPContentVerifierBuilderProvider(), vKey);
if (!sig.verifyCertification(vKey, sKey))
{
fail("failed to verify sub-key signature.");
}
}
}
}
请发表评论