本文整理汇总了Java中org.bouncycastle.openpgp.PGPKeyRingGenerator类的典型用法代码示例。如果您正苦于以下问题:Java PGPKeyRingGenerator类的具体用法?Java PGPKeyRingGenerator怎么用?Java PGPKeyRingGenerator使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
PGPKeyRingGenerator类属于org.bouncycastle.openpgp包,在下文中一共展示了PGPKeyRingGenerator类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: createPGPKeyRingGenerator
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;
}
示例2: doInBackground
import org.bouncycastle.openpgp.PGPKeyRingGenerator; //导入依赖的package包/类
@Override
protected Void doInBackground() throws Exception
{
Main.logger.info("Neue Schlüssel werden generiert...");
PGPKeyRingGenerator pkg = RSAGen.generateKeyRingGenerator(mail, pass, this);
PGPPublicKeyRing pkr = pkg.generatePublicKeyRing();
PGPSecretKeyRing skr = pkg.generateSecretKeyRing();
Main.psk = skr.getSecretKey();
Iterator<PGPPublicKey> rIt = pkr.getPublicKeys();
// Sucht den Verschlüsselungsschlüssel
while (Main.ppk == null && rIt.hasNext())
{
PGPPublicKey temp_key = rIt.next();
if (temp_key.isEncryptionKey())
{
Main.ppk = temp_key;
break;
}
}
PBESecretKeyDecryptor secretKeyDecryptor = new JcePBESecretKeyDecryptorBuilder()
.setProvider(BouncyCastleProvider.PROVIDER_NAME).build(pass);
Main.pprk = Main.psk.extractPrivateKey(secretKeyDecryptor);
setProgress(90);
// Speichern der Schlüssel
PGPSecretKeyRing pskr = pkg.generateSecretKeyRing();
ArmoredOutputStream secout = new ArmoredOutputStream(
new BufferedOutputStream(new FileOutputStream(Main.secKey)));
// Geheimer Schlüssel
pskr.encode(secout);
secout.close();
ArmoredOutputStream pubout = new ArmoredOutputStream(
new BufferedOutputStream(new FileOutputStream(Main.pubKey)));
pkr.encode(pubout);
pubout.close();
setProgress(100);
return null;
}
示例3: generateKeyPair
import org.bouncycastle.openpgp.PGPKeyRingGenerator; //导入依赖的package包/类
@ReactMethod
public void generateKeyPair(final String userId, final int numBits, final String passphrase, Promise promise) {
Log.d("ReactNativePGP", "generateKeyPair");
try {
WritableMap resultMap = Arguments.createMap();
PGPKeyRingGenerator keyGenerator = PGPUtils.generateKeyRingGenerator(userId, numBits, passphrase.toCharArray());
// public key
PGPPublicKeyRing publicKeyRing = keyGenerator.generatePublicKeyRing();
ByteArrayOutputStream publicKeyOutputStream = new ByteArrayOutputStream();
ArmoredOutputStream armoredPubOutputStream = new ArmoredOutputStream(publicKeyOutputStream);
publicKeyRing.encode(armoredPubOutputStream);
armoredPubOutputStream.close();
resultMap.putString("publicKey", publicKeyOutputStream.toString("UTF-8"));
// private key
PGPSecretKeyRing secretKeyRing = keyGenerator.generateSecretKeyRing();
ByteArrayOutputStream privateKeyOutputStream = new ByteArrayOutputStream();
ArmoredOutputStream armoredPrivOutputStream = new ArmoredOutputStream(privateKeyOutputStream);
secretKeyRing.encode(armoredPrivOutputStream);
armoredPrivOutputStream.close();
resultMap.putString("privateKey", privateKeyOutputStream.toString("UTF-8"));
resultMap.putString("fingerPrint", Utils.bytesToHex(secretKeyRing.getPublicKey().getFingerprint()));
promise.resolve(resultMap);
} catch(Exception e) {
promise.reject(new Exception(e.getMessage()));
}
}
示例4: generateKeyPair
import org.bouncycastle.openpgp.PGPKeyRingGenerator; //导入依赖的package包/类
public static KeyPair generateKeyPair( String userId, String secretPwd, boolean armored ) throws PGPException
{
try
{
KeyPair keyPair = new KeyPair();
PGPKeyRingGenerator krgen = generateKeyRingGenerator( userId, secretPwd, keyPair );
// Generate public key ring
PGPPublicKeyRing pkr = krgen.generatePublicKeyRing();
ByteArrayOutputStream pubOut = new ByteArrayOutputStream();
pkr.encode( pubOut );
pubOut.close();
// Generate private key
PGPSecretKeyRing skr = krgen.generateSecretKeyRing();
ByteArrayOutputStream secOut = new ByteArrayOutputStream();
skr.encode( secOut );
secOut.close();
keyPair.setPubKeyring( armored ? armorByteArray( pubOut.toByteArray() ) : pubOut.toByteArray() );
keyPair.setSecKeyring( armored ? armorByteArray( secOut.toByteArray() ) : secOut.toByteArray() );
return keyPair;
}
catch ( Exception e )
{
throw new PGPException( "Error in generateKeyPair", e );
}
}
示例5: exportSecretKey
import org.bouncycastle.openpgp.PGPKeyRingGenerator; //导入依赖的package包/类
public static final void exportSecretKey(PGPKeyRingGenerator pgpKeyRingGen, File keyFile, boolean asciiArmor) throws IOException {
PGPSecretKeyRing pgpSecKeyRing = pgpKeyRingGen.generateSecretKeyRing();
if (asciiArmor) {
ArmoredOutputStream aos = new ArmoredOutputStream(new FileOutputStream(keyFile));
pgpSecKeyRing.encode(aos);
aos.close();
}
else {
FileOutputStream fos = new FileOutputStream(keyFile);
pgpSecKeyRing.encode(fos);
fos.close();
}
}
示例6: exportPublicKey
import org.bouncycastle.openpgp.PGPKeyRingGenerator; //导入依赖的package包/类
public static final void exportPublicKey(PGPKeyRingGenerator pgpKeyRingGen, File keyFile, boolean asciiArmor) throws IOException {
PGPPublicKeyRing pgpPubKeyRing = pgpKeyRingGen.generatePublicKeyRing();
if (asciiArmor) {
ArmoredOutputStream aos = new ArmoredOutputStream(new FileOutputStream(keyFile));
pgpPubKeyRing.encode(aos);
aos.close();
}
else {
FileOutputStream fos = new FileOutputStream(keyFile);
pgpPubKeyRing.encode(fos);
fos.close();
}
}
示例7: generateKeyPair
import org.bouncycastle.openpgp.PGPKeyRingGenerator; //导入依赖的package包/类
public void generateKeyPair() {
try {
String keysDir = System.getProperty("user.dir") + File.separator + "src/george/crypto/pgp/keys";
BigInteger primeModulous = PGPKeyTools.getSafePrimeModulus(PGPKeyTools.PRIME_MODULUS_4096_BIT);
BigInteger baseGenerator = PGPKeyTools.getBaseGenerator();
ElGamalParameterSpec paramSpecs = new ElGamalParameterSpec(primeModulous, baseGenerator);
KeyPair dsaKeyPair = PGPKeyTools.generateDsaKeyPair(1024);
KeyPair elGamalKeyPair = PGPKeyTools.generateElGamalKeyPair(paramSpecs);
PGPKeyRingGenerator pgpKeyRingGen = PGPKeyTools.createPGPKeyRingGenerator(
dsaKeyPair,
elGamalKeyPair,
"Greg House <[email protected]>",
"TestPass12345!".toCharArray()
);
File privateKey = new File(keysDir + File.separator + "secret4.asc");
File publicKey = new File(keysDir + File.separator + "public4.asc");
PGPKeyTools.exportSecretKey(pgpKeyRingGen, privateKey, true);
PGPKeyTools.exportPublicKey(pgpKeyRingGen, publicKey, true);
System.out.println("Generated private key: " + privateKey.getAbsolutePath());
System.out.println("Generated public key: " + publicKey.getAbsolutePath());
}
catch (Exception ex) {
ex.printStackTrace();
}
}
示例8: generateKeyPair
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);
}
}
示例9: generateKeyRingGenerator
import org.bouncycastle.openpgp.PGPKeyRingGenerator; //导入依赖的package包/类
public final static PGPKeyRingGenerator generateKeyRingGenerator(String id, char[] pass,
KeyGenPane.BackgroundTask bgt) throws Exception
{
return generateKeyRingGenerator(id, pass, 0xc0, bgt);
}
示例10: generateKeyRingGenerator
import org.bouncycastle.openpgp.PGPKeyRingGenerator; //导入依赖的package包/类
static PGPKeyRingGenerator generateKeyRingGenerator(String userId, int numBits, char[] passphrase) throws Exception {
RSAKeyPairGenerator keyPairGenerator = new RSAKeyPairGenerator();
keyPairGenerator.init(
new RSAKeyGenerationParameters(
BigInteger.valueOf(0x10001),
new SecureRandom(),
numBits,
12
)
);
PGPKeyPair rsaKeyPairSign = new BcPGPKeyPair(
PGPPublicKey.RSA_SIGN,
keyPairGenerator.generateKeyPair(),
new Date()
);
PGPKeyPair rsaKeyPairEncrypt = new BcPGPKeyPair(
PGPPublicKey.RSA_ENCRYPT,
keyPairGenerator.generateKeyPair(),
new Date()
);
PGPSignatureSubpacketGenerator signHashGenerator = new PGPSignatureSubpacketGenerator();
signHashGenerator.setKeyFlags(false, KeyFlags.SIGN_DATA | KeyFlags.CERTIFY_OTHER);
signHashGenerator.setPreferredSymmetricAlgorithms(
false,
new int[] {
SymmetricKeyAlgorithmTags.AES_256,
SymmetricKeyAlgorithmTags.AES_192,
SymmetricKeyAlgorithmTags.AES_128
}
);
signHashGenerator.setPreferredHashAlgorithms(
false,
new int[] {
HashAlgorithmTags.SHA512,
HashAlgorithmTags.SHA384,
HashAlgorithmTags.SHA256,
HashAlgorithmTags.SHA1, // Not recommended
HashAlgorithmTags.SHA224, // Not recommended
}
);
signHashGenerator.setFeature(false, Features.FEATURE_MODIFICATION_DETECTION);
PGPSignatureSubpacketGenerator encryptHashGenerator = new PGPSignatureSubpacketGenerator();
encryptHashGenerator.setKeyFlags(false, KeyFlags.ENCRYPT_COMMS | KeyFlags.ENCRYPT_STORAGE);
PGPDigestCalculator sha1DigestCalculator = new BcPGPDigestCalculatorProvider().get(HashAlgorithmTags.SHA1);
PGPDigestCalculator sha512DigestCalculator = new BcPGPDigestCalculatorProvider().get(HashAlgorithmTags.SHA512);
PBESecretKeyEncryptor secretKeyEncryptor = (
new BcPBESecretKeyEncryptorBuilder(PGPEncryptedData.AES_256, sha512DigestCalculator)
)
.build(passphrase);
PGPKeyRingGenerator keyRingGen = new PGPKeyRingGenerator(
PGPSignature.NO_CERTIFICATION,
rsaKeyPairSign,
userId,
sha1DigestCalculator,
signHashGenerator.generate(),
null,
new BcPGPContentSignerBuilder(rsaKeyPairSign.getPublicKey().getAlgorithm(), HashAlgorithmTags.SHA512),
secretKeyEncryptor
);
keyRingGen.addSubKey(rsaKeyPairEncrypt, encryptHashGenerator.generate(), null);
return keyRingGen;
}
示例11: generateTest
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.");
}
}
}
}
示例12: generateSha1Test
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.");
}
}
}
}
示例13: generateKeyRingGenerator
import org.bouncycastle.openpgp.PGPKeyRingGenerator; //导入依赖的package包/类
private static PGPKeyRingGenerator generateKeyRingGenerator( String userId, String secretPwd, KeyPair keyPair )
throws PGPException
{
return generateKeyRingGenerator( userId, secretPwd.toCharArray(), 0xc0, 2048, keyPair );
}
示例14: generateAndSign
import org.bouncycastle.openpgp.PGPKeyRingGenerator; //导入依赖的package包/类
private void generateAndSign()
throws Exception
{
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("ECDSA", "BC");
keyGen.initialize(new ECGenParameterSpec("P-256"));
KeyPair kpSign = keyGen.generateKeyPair();
PGPKeyPair ecdsaKeyPair = new JcaPGPKeyPair(PGPPublicKey.ECDSA, kpSign, new Date());
//
// try a signature
//
PGPSignatureGenerator signGen = new PGPSignatureGenerator(new JcaPGPContentSignerBuilder(PGPPublicKey.ECDSA, HashAlgorithmTags.SHA256).setProvider("BC"));
signGen.init(PGPSignature.BINARY_DOCUMENT, ecdsaKeyPair.getPrivateKey());
signGen.update("hello world!".getBytes());
PGPSignature sig = signGen.generate();
sig.init(new JcaPGPContentVerifierBuilderProvider().setProvider("BC"), ecdsaKeyPair.getPublicKey());
sig.update("hello world!".getBytes());
if (!sig.verify())
{
fail("signature failed to verify!");
}
//
// generate a key ring
//
char[] passPhrase = "test".toCharArray();
PGPDigestCalculator sha1Calc = new JcaPGPDigestCalculatorProviderBuilder().build().get(HashAlgorithmTags.SHA1);
PGPKeyRingGenerator keyRingGen = new PGPKeyRingGenerator(PGPSignature.POSITIVE_CERTIFICATION, ecdsaKeyPair,
"[email protected]", sha1Calc, null, null, new JcaPGPContentSignerBuilder(ecdsaKeyPair.getPublicKey().getAlgorithm(), HashAlgorithmTags.SHA1), new JcePBESecretKeyEncryptorBuilder(PGPEncryptedData.AES_256, sha1Calc).setProvider("BC").build(passPhrase));
PGPPublicKeyRing pubRing = keyRingGen.generatePublicKeyRing();
PGPSecretKeyRing secRing = keyRingGen.generateSecretKeyRing();
KeyFingerPrintCalculator fingerCalc = new JcaKeyFingerprintCalculator();
PGPPublicKeyRing pubRingEnc = new PGPPublicKeyRing(pubRing.getEncoded(), fingerCalc);
if (!Arrays.areEqual(pubRing.getEncoded(), pubRingEnc.getEncoded()))
{
fail("public key ring encoding failed");
}
PGPSecretKeyRing secRingEnc = new PGPSecretKeyRing(secRing.getEncoded(), fingerCalc);
if (!Arrays.areEqual(secRing.getEncoded(), secRingEnc.getEncoded()))
{
fail("secret key ring encoding failed");
}
}
示例15: generateTest
import org.bouncycastle.openpgp.PGPKeyRingGenerator; //导入依赖的package包/类
public void generateTest()
throws Exception
{
char[] passPhrase = "hello".toCharArray();
KeyPairGenerator dsaKpg = KeyPairGenerator.getInstance("DSA", "BC");
dsaKpg.initialize(512);
//
// this takes a while as the key generator has to generate some DSA params
// before it generates the key.
//
KeyPair dsaKp = dsaKpg.generateKeyPair();
KeyPairGenerator elgKpg = KeyPairGenerator.getInstance("ELGAMAL", "BC");
BigInteger g = new BigInteger("153d5d6172adb43045b68ae8e1de1070b6137005686d29d3d73a7749199681ee5b212c9b96bfdcfa5b20cd5e3fd2044895d609cf9b410b7a0f12ca1cb9a428cc", 16);
BigInteger p = new BigInteger("9494fec095f3b85ee286542b3836fc81a5dd0a0349b4c239dd38744d488cf8e31db8bcb7d33b41abb9e5a33cca9144b1cef332c94bf0573bf047a3aca98cdf3b", 16);
ElGamalParameterSpec elParams = new ElGamalParameterSpec(p, g);
elgKpg.initialize(elParams);
//
// this is quicker because we are using pregenerated parameters.
//
KeyPair elgKp = elgKpg.generateKeyPair();
PGPKeyPair dsaKeyPair = new PGPKeyPair(PGPPublicKey.DSA, dsaKp, new Date());
PGPKeyPair elgKeyPair = new PGPKeyPair(PGPPublicKey.ELGAMAL_ENCRYPT, elgKp, new Date());
PGPKeyRingGenerator keyRingGen = new PGPKeyRingGenerator(PGPSignature.POSITIVE_CERTIFICATION, dsaKeyPair,
"test", PGPEncryptedData.AES_256, passPhrase, null, null, new SecureRandom(), "BC");
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.");
}
}
}
}