本文整理汇总了Java中org.bouncycastle.openpgp.PGPKeyPair类的典型用法代码示例。如果您正苦于以下问题:Java PGPKeyPair类的具体用法?Java PGPKeyPair怎么用?Java PGPKeyPair使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
PGPKeyPair类属于org.bouncycastle.openpgp包,在下文中一共展示了PGPKeyPair类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: createPGPKeyRingGenerator
import org.bouncycastle.openpgp.PGPKeyPair; //导入依赖的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: lookupKeyPair
import org.bouncycastle.openpgp.PGPKeyPair; //导入依赖的package包/类
/**
* Same as {@link #lookupPublicKey} but also retrieves the associated private key.
*
* @throws VerifyException if either keys couldn't be found.
* @see #lookupPublicKey
*/
@SuppressWarnings("deprecation")
public static PGPKeyPair lookupKeyPair(
PGPPublicKeyRingCollection publics,
PGPSecretKeyRingCollection privates,
String query,
KeyRequirement want) {
PGPPublicKey publicKey = lookupPublicKey(publics, query, want);
PGPPrivateKey privateKey;
try {
PGPSecretKey secret = verifyNotNull(privates.getSecretKey(publicKey.getKeyID()),
"Keyring missing private key associated with public key id: %x (query '%s')",
publicKey.getKeyID(), query);
// We do not support putting a password on the private key so we're just going to
// put char[0] here.
privateKey = secret.extractPrivateKey(
new BcPBESecretKeyDecryptorBuilder(new BcPGPDigestCalculatorProvider())
.build(new char[0]));
} catch (PGPException e) {
throw new VerifyException(e.getMessage());
}
return new PGPKeyPair(publicKey, privateKey);
}
示例3: serializeKeyPair
import org.bouncycastle.openpgp.PGPKeyPair; //导入依赖的package包/类
/**
* Serialize a PGPKeyPair
*
* <p>Use this to serialize a PGPPrivateKey as well (pairing it with the corresponding
* PGPPublicKey), as private keys can't be serialized on their own.
*/
public static byte[] serializeKeyPair(PGPKeyPair keyPair) throws IOException, PGPException {
try (ByteArrayOutputStream byteStream = new ByteArrayOutputStream()) {
// NOTE: We have to close the ArmoredOutputStream before calling the underlying OutputStream's
// "toByteArray". Failing to do so would result in a truncated serialization as we took the
// byte array before the ArmoredOutputStream wrote all the data.
//
// Even "flushing" the ArmoredOutputStream isn't enough - as there are parts that are only
// written by the ArmoredOutputStream when it is closed: the "-----END PGP PRIVATE KEY
// BLOCK-----" (or similar) footer.
try (ArmoredOutputStream out = new ArmoredOutputStream(byteStream)) {
new PGPSecretKey(
keyPair.getPrivateKey(),
keyPair.getPublicKey(),
new JcaPGPDigestCalculatorProviderBuilder()
.setProvider("BC")
.build()
.get(HashAlgorithmTags.SHA256),
true,
null).encode(out);
}
return byteStream.toByteArray();
}
}
示例4: testRdeSigningKey_actualThrows
import org.bouncycastle.openpgp.PGPKeyPair; //导入依赖的package包/类
@Test
public void testRdeSigningKey_actualThrows() throws Exception {
Keyring actualKeyring = mock(Keyring.class);
Keyring secondKeyring = mock(Keyring.class);
PGPKeyPair keyPair =
new PGPKeyPair(
mockPublicKey(false, false),
mockPrivateKey(false, false, false, false));
when(actualKeyring.getRdeSigningKey()).thenThrow(new KeyringException("message"));
when(secondKeyring.getRdeSigningKey()).thenReturn(keyPair);
Keyring comparatorKeyring = ComparatorKeyring.create(actualKeyring, secondKeyring);
assertThrows(KeyringException.class, comparatorKeyring::getRdeSigningKey);
assertAboutLogs()
.that(testLogHandler)
.hasLogAtLevelWithMessage(
Level.SEVERE, ".getRdeSigningKey: Only actual implementation threw exception");
}
示例5: testRdeSigningKey_secondThrows
import org.bouncycastle.openpgp.PGPKeyPair; //导入依赖的package包/类
@Test
public void testRdeSigningKey_secondThrows() throws Exception {
Keyring actualKeyring = mock(Keyring.class);
Keyring secondKeyring = mock(Keyring.class);
PGPKeyPair keyPair =
new PGPKeyPair(
mockPublicKey(false, false),
mockPrivateKey(false, false, false, false));
when(actualKeyring.getRdeSigningKey()).thenReturn(keyPair);
when(secondKeyring.getRdeSigningKey()).thenThrow(new KeyringException("message"));
Keyring comparatorKeyring = ComparatorKeyring.create(actualKeyring, secondKeyring);
assertThat(comparatorKeyring.getRdeSigningKey()).isSameAs(keyPair);
assertAboutLogs()
.that(testLogHandler)
.hasLogAtLevelWithMessage(
Level.SEVERE, ".getRdeSigningKey: Only second implementation threw exception");
}
示例6: testRdeSigningKey_same
import org.bouncycastle.openpgp.PGPKeyPair; //导入依赖的package包/类
@Test
public void testRdeSigningKey_same() throws Exception {
Keyring actualKeyring = mock(Keyring.class);
Keyring secondKeyring = mock(Keyring.class);
PGPKeyPair keyPair =
new PGPKeyPair(
mockPublicKey(false, false),
mockPrivateKey(false, false, false, false));
PGPKeyPair keyPairCopy =
new PGPKeyPair(
mockPublicKey(false, false),
mockPrivateKey(false, false, false, false));
when(actualKeyring.getRdeSigningKey()).thenReturn(keyPair);
when(secondKeyring.getRdeSigningKey()).thenReturn(keyPairCopy);
Keyring comparatorKeyring = ComparatorKeyring.create(actualKeyring, secondKeyring);
assertThat(comparatorKeyring.getRdeSigningKey()).isSameAs(keyPair);
assertAboutLogs().that(testLogHandler).hasNoLogsAtLevel(Level.SEVERE);
}
示例7: RydePgpSigningOutputStream
import org.bouncycastle.openpgp.PGPKeyPair; //导入依赖的package包/类
/**
* Create a signer that wraps {@code os} and generates a detached signature using
* {@code signingKey}. After closing, you should call {@link #getSignature()} to get the detached
* signature.
*
* @param os is the upstream {@link OutputStream} which is not closed by this object
* @throws RuntimeException to rethrow {@link PGPException}
*/
public RydePgpSigningOutputStream(
@WillNotClose OutputStream os,
PGPKeyPair signingKey) {
super(os, false, -1);
try {
signer = new PGPSignatureGenerator(
new BcPGPContentSignerBuilder(RSA_GENERAL, SHA256));
signer.init(BINARY_DOCUMENT, signingKey.getPrivateKey());
} catch (PGPException e) {
throw new RuntimeException(e);
}
addUserInfoToSignature(signingKey.getPublicKey(), signer);
}
示例8: encrypt
import org.bouncycastle.openpgp.PGPKeyPair; //导入依赖的package包/类
/** Creates a {@code .ryde} and {@code .sig} file, provided an XML deposit file. */
void encrypt(String tld, Path xmlFile, Path outdir)
throws IOException, PGPException, XmlException {
try (InputStream xmlFileInput = Files.newInputStream(xmlFile);
BufferedInputStream xmlInput = new BufferedInputStream(xmlFileInput, PEEK_BUFFER_SIZE)) {
DateTime watermark = RdeUtil.peekWatermark(xmlInput);
String name = RdeNamingUtils.makeRydeFilename(tld, watermark, FULL, 1, 0);
Path rydePath = outdir.resolve(name + ".ryde");
Path sigPath = outdir.resolve(name + ".sig");
Path pubPath = outdir.resolve(tld + ".pub");
PGPKeyPair signingKey = rdeSigningKey.get();
try (OutputStream rydeOutput = Files.newOutputStream(rydePath);
RydePgpSigningOutputStream signLayer =
pgpSigningFactory.create(rydeOutput, signingKey)) {
try (RydePgpEncryptionOutputStream encryptLayer =
pgpEncryptionFactory.create(signLayer, rdeReceiverKey.get());
RydePgpCompressionOutputStream compressLayer =
pgpCompressionFactory.create(encryptLayer);
RydePgpFileOutputStream fileLayer =
pgpFileFactory.create(compressLayer, watermark, name + ".tar");
RydeTarOutputStream tarLayer =
tarFactory.create(fileLayer, Files.size(xmlFile), watermark, name + ".xml")) {
ByteStreams.copy(xmlInput, tarLayer);
}
Files.write(sigPath, signLayer.getSignature());
try (OutputStream pubOutput = Files.newOutputStream(pubPath);
ArmoredOutputStream ascOutput = new ArmoredOutputStream(pubOutput)) {
signingKey.getPublicKey().encode(ascOutput);
}
}
}
}
示例9: setKeyPair
import org.bouncycastle.openpgp.PGPKeyPair; //导入依赖的package包/类
private KmsUpdater setKeyPair(
PGPKeyPair keyPair, PrivateKeyLabel privateKeyLabel, PublicKeyLabel publicKeyLabel)
throws IOException, PGPException {
checkArgumentNotNull(keyPair);
setSecret(privateKeyLabel.getLabel(), KeySerializer.serializeKeyPair(keyPair));
setSecret(publicKeyLabel.getLabel(), KeySerializer.serializePublicKey(keyPair.getPublicKey()));
return this;
}
示例10: getKeyPair
import org.bouncycastle.openpgp.PGPKeyPair; //导入依赖的package包/类
private PGPKeyPair getKeyPair(PrivateKeyLabel keyLabel) {
try {
return KeySerializer.deserializeKeyPair(getDecryptedData(keyLabel.getLabel()));
} catch (IOException | PGPException e) {
throw new KeyringException(
String.format("Could not parse private keyLabel %s", keyLabel), e);
}
}
示例11: deserializeKeyPair
import org.bouncycastle.openpgp.PGPKeyPair; //导入依赖的package包/类
/** Deserialize a PGPKeyPair */
public static PGPKeyPair deserializeKeyPair(byte[] serialized)
throws IOException, PGPException {
PGPSecretKey secretKey =
new BcPGPSecretKeyRing(
PGPUtil.getDecoderStream(
new ByteArrayInputStream(serialized))).getSecretKey();
return new PGPKeyPair(
secretKey.getPublicKey(),
secretKey.extractPrivateKey(createSecretKeyDecryptor()));
}
示例12: compareResults
import org.bouncycastle.openpgp.PGPKeyPair; //导入依赖的package包/类
/** Implements equals for the PGP classes. */
@Override
protected boolean compareResults(Method method, @Nullable Object a, @Nullable Object b) {
Class<?> clazz = method.getReturnType();
if (PGPPublicKey.class.equals(clazz)) {
return compare((PGPPublicKey) a, (PGPPublicKey) b);
}
if (PGPPrivateKey.class.equals(clazz)) {
return compare((PGPPrivateKey) a, (PGPPrivateKey) b);
}
if (PGPKeyPair.class.equals(clazz)) {
return compare((PGPKeyPair) a, (PGPKeyPair) b);
}
return super.compareResults(method, a, b);
}
示例13: stringifyResult
import org.bouncycastle.openpgp.PGPKeyPair; //导入依赖的package包/类
/** Implements toString for the PGP classes. */
@Override
protected String stringifyResult(Method method, @Nullable Object a) {
Class<?> clazz = method.getReturnType();
if (PGPPublicKey.class.equals(clazz)) {
return stringify((PGPPublicKey) a);
}
if (PGPPrivateKey.class.equals(clazz)) {
return stringify((PGPPrivateKey) a);
}
if (PGPKeyPair.class.equals(clazz)) {
return stringify((PGPKeyPair) a);
}
return super.stringifyResult(method, a);
}
示例14: compare
import org.bouncycastle.openpgp.PGPKeyPair; //导入依赖的package包/类
@VisibleForTesting
static boolean compare(@Nullable PGPKeyPair a, @Nullable PGPKeyPair b) {
if (a == null || b == null) {
return a == null && b == null;
}
return compare(a.getPublicKey(), b.getPublicKey())
&& compare(a.getPrivateKey(), b.getPrivateKey());
}
示例15: stringify
import org.bouncycastle.openpgp.PGPKeyPair; //导入依赖的package包/类
@VisibleForTesting
static String stringify(PGPKeyPair a) {
if (a == null) {
return "null";
}
return MoreObjects.toStringHelper(PGPKeyPair.class)
.addValue(stringify(a.getPublicKey()))
.addValue(stringify(a.getPrivateKey()))
.toString();
}