本文整理汇总了Java中org.bouncycastle.openpgp.PGPPublicKey类的典型用法代码示例。如果您正苦于以下问题:Java PGPPublicKey类的具体用法?Java PGPPublicKey怎么用?Java PGPPublicKey使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
PGPPublicKey类属于org.bouncycastle.openpgp包,在下文中一共展示了PGPPublicKey类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: writeData
import org.bouncycastle.openpgp.PGPPublicKey; //导入依赖的package包/类
public static void writeData(List<Calendar> data) throws Exception
{
List<PGPPublicKey> pubKeys = new ArrayList<PGPPublicKey>();
pubKeys.add(ppk);
pubKeys.add(davidPub);
try
{
if (!dataFile.exists())
dataFile.createNewFile();
DataXMLParser.writeData(dataFile, data, pubKeys);
} catch (Exception e)
{
Main.logger.error("Error while writing data!", e);
throw e;
}
}
示例2: readPublicKey
import org.bouncycastle.openpgp.PGPPublicKey; //导入依赖的package包/类
public static PGPPublicKey readPublicKey(InputStream in)
throws IOException, PGPException {
in = PGPUtil.getDecoderStream(in);
PGPPublicKeyRingCollection pgpPub = new PGPPublicKeyRingCollection(in, new JcaKeyFingerprintCalculator() );
Iterator rIt = pgpPub.getKeyRings();
while (rIt.hasNext()) {
PGPPublicKeyRing kRing = (PGPPublicKeyRing) rIt.next();
Iterator kIt = kRing.getPublicKeys();
while (kIt.hasNext()) {
PGPPublicKey k = (PGPPublicKey) kIt.next();
if (k.isEncryptionKey()) {
return k;
}
}
}
throw new IllegalArgumentException(
"Can't find encryption key in key ring.");
}
示例3: getKeyInfo
import org.bouncycastle.openpgp.PGPPublicKey; //导入依赖的package包/类
/**
* Returns information on a public key
* @param key A PGP public key
* @return Key information
*/
public static String getKeyInfo(PGPPublicKey key) {
StringBuffer info = new StringBuffer(PGPInit.getKeyExchangeAlgorithm(key.getAlgorithm()))
.append(" (").append(key.getBitStrength()).append(")")
.append(" v").append(key.getVersion())
.append(" id:").append(key.getKeyID())
.append(" ").append(key.getCreationTime());
if (key.isEncryptionKey()) {
info.append(" [").append("encryption").append("]");
}
if (key.isMasterKey()) {
info.append(" [").append("master").append("]");
}
if (key.isRevoked()) {
info.append(" [").append("revoked").append("]");
}
return info.toString();
}
示例4: verify
import org.bouncycastle.openpgp.PGPPublicKey; //导入依赖的package包/类
/**
* Verify a signature. Only return true if signature matches calculated signature of signedData
* and if it was signed by the publicKey
*
* @param signedData
* @param signature
* @return
*/
public boolean verify(InputStream signedData, InputStream signature) {
try {
signature = PGPUtil.getDecoderStream(signature);
JcaPGPObjectFactory pgpFact = new JcaPGPObjectFactory(signature);
PGPSignature sig = ((PGPSignatureList) pgpFact.nextObject()).get(0);
PGPPublicKey key = pgpPubRingCollection.getPublicKey(sig.getKeyID());
sig.init(new JcaPGPContentVerifierBuilderProvider().setProvider("BC"), key);
byte[] buff = new byte[1024];
int read = 0;
while ((read = signedData.read(buff)) != -1) {
sig.update(buff, 0, read);
}
signedData.close();
return sig.verify();
}
catch (Exception ex) {
// can we put a logger here please?
return false;
}
}
示例5: findPublicKey
import org.bouncycastle.openpgp.PGPPublicKey; //导入依赖的package包/类
@Deprecated
public static PGPPublicKey findPublicKey(CamelContext context, String filename, byte[] keyRing, String userid, boolean forEncryption)
throws IOException, PGPException, NoSuchProviderException {
InputStream is = determineKeyRingInputStream(context, filename, keyRing, forEncryption);
try {
List<PGPPublicKey> result = findPublicKeys(is, Collections.singletonList(userid), forEncryption);
if (result.isEmpty()) {
return null;
} else {
return result.get(0);
}
} finally {
IOHelper.close(is);
}
}
示例6: test11
import org.bouncycastle.openpgp.PGPPublicKey; //导入依赖的package包/类
private void test11()
throws Exception
{
PGPPublicKeyRing pubRing = new PGPPublicKeyRing(subKeyBindingKey, new BcKeyFingerprintCalculator());
Iterator it = pubRing.getPublicKeys();
while (it.hasNext())
{
PGPPublicKey key = (PGPPublicKey)it.next();
if (key.getValidSeconds() != 0)
{
fail("expiration time non-zero");
}
}
}
示例7: testEncryptOnly
import org.bouncycastle.openpgp.PGPPublicKey; //导入依赖的package包/类
@Theory
public void testEncryptOnly(Content content) throws Exception {
Keyring keyring = new FakeKeyringModule().get();
byte[] data = content.get().getBytes(UTF_8);
PGPPublicKey publicKey = keyring.getRdeStagingEncryptionKey();
PGPPrivateKey privateKey = keyring.getRdeStagingDecryptionKey();
Ghostryde ghost = new Ghostryde(1024);
ByteArrayOutputStream bsOut = new ByteArrayOutputStream();
try (Ghostryde.Encryptor encryptor = ghost.openEncryptor(bsOut, publicKey)) {
encryptor.write(data);
}
ByteArrayInputStream bsIn = new ByteArrayInputStream(bsOut.toByteArray());
bsOut.reset();
try (Ghostryde.Decryptor decryptor = ghost.openDecryptor(bsIn, privateKey)) {
ByteStreams.copy(decryptor, bsOut);
}
assertThat(new String(bsOut.toByteArray(), UTF_8)).isEqualTo(content.get());
}
示例8: getPublicKeyWithKeyIdAndUserID
import org.bouncycastle.openpgp.PGPPublicKey; //导入依赖的package包/类
/**
* Determines a public key from the keyring collection which has a certain
* key ID and which has a User ID which contains at least one of the User ID
* parts.
*
* @param keyId
* key ID
* @param userIdParts
* user ID parts, can be empty, than no filter on the User ID is
* executed
* @param publicKeyringCollection
* keyring collection
* @return public key or <code>null</code> if no fitting key is found
* @throws PGPException
*/
@SuppressWarnings("unchecked")
public static PGPPublicKey getPublicKeyWithKeyIdAndUserID(long keyId, List<String> userIdParts, PGPPublicKeyRingCollection publicKeyringCollection)
throws PGPException {
PGPPublicKeyRing publicKeyring = publicKeyringCollection.getPublicKeyRing(keyId);
if (publicKeyring == null) {
LOG.debug("No public key found for key ID {}.", Long.toString(keyId));
return null;
}
// publicKey can be a subkey the user IDs must therefore be provided by the primary/master key
if (isAllowedKey(userIdParts, publicKeyring.getPublicKey().getUserIDs())) {
return publicKeyring.getPublicKey(keyId);
} else {
return null;
}
}
示例9: setApprovedResult
import org.bouncycastle.openpgp.PGPPublicKey; //导入依赖的package包/类
private void setApprovedResult( final RegistrationData result, final String keyPhrase )
{
String sslCert =
securityManager.getKeyStoreManager().exportCertificate( Common.DEFAULT_PUBLIC_SECURE_PORT, "" );
PGPPublicKey pkey = securityManager.getKeyManager().getPublicKey( localPeerId );
try
{
byte[] key = SecurityUtilities.generateKey( keyPhrase.getBytes( "UTF-8" ) );
Encrypted encryptedSslCert = new Encrypted( sslCert, key );
result.setSslCert( encryptedSslCert );
String publicKey = PGPKeyUtil.exportAscii( pkey );
Encrypted encryptedPublicKey = new Encrypted( publicKey, key );
result.setPublicKey( encryptedPublicKey );
}
catch ( Exception e )
{
LOG.warn( e.getMessage(), e );
}
}
示例10: verifyPublicKey
import org.bouncycastle.openpgp.PGPPublicKey; //导入依赖的package包/类
/**
* Verifies that a public key is signed with another public key
*
* @param keyToVerify the public key to verify
* @param id the id we are verifying against the public key
* @param keyToVerifyWith the key to verify with
*
* @return true if verified, false otherwise
*/
public static boolean verifyPublicKey( PGPPublicKey keyToVerify, String id, PGPPublicKey keyToVerifyWith )
throws PGPException
{
try
{
Iterator<PGPSignature> signIterator = keyToVerify.getSignatures();
while ( signIterator.hasNext() )
{
PGPSignature signature = signIterator.next();
signature.init( new JcaPGPContentVerifierBuilderProvider().setProvider( provider ), keyToVerifyWith );
if ( signature.verifyCertification( id.getBytes(), keyToVerify ) )
{
return true;
}
}
return false;
}
catch ( Exception e )
{
//throw custom exception
throw new PGPException( "Error verifying public key", e );
}
}
示例11: removeSignature
import org.bouncycastle.openpgp.PGPPublicKey; //导入依赖的package包/类
public static PGPPublicKeyRing removeSignature( PGPPublicKeyRing keyToRemoveFrom, String id ) throws PGPException
{
try
{
PGPPublicKey oldKey = keyToRemoveFrom.getPublicKey();
PGPPublicKey newKey = PGPPublicKey.removeCertification( oldKey, id );
PGPPublicKeyRing newPublicKeyRing = PGPPublicKeyRing.removePublicKey( keyToRemoveFrom, oldKey );
return PGPPublicKeyRing.insertPublicKey( newPublicKeyRing, newKey );
}
catch ( Exception e )
{
//throw custom exception
throw new PGPException( "Error removing signature", e );
}
}
示例12: testSignEncryptAndDecryptVerify
import org.bouncycastle.openpgp.PGPPublicKey; //导入依赖的package包/类
@Test
public void testSignEncryptAndDecryptVerify() throws Exception
{
PGPSecretKey signingKey =
PGPEncryptionUtil.findSecretKeyByFingerprint( findFile( SECRET_KEYRING ), SECRET_KEY_FINGERPRINT );
PGPPublicKey encryptingKey =
PGPEncryptionUtil.findPublicKeyByFingerprint( findFile( PUBLIC_KEYRING ), PUBLIC_KEY_FINGERPRINT );
byte[] signedAndEncryptedMessage =
PGPEncryptionUtil.signAndEncrypt( MESSAGE.getBytes(), signingKey, SECRET_PWD, encryptingKey, true );
PGPSecretKey decryptingSecretKey = PGPEncryptionUtil.findSecretKeyByFingerprint( findFile( SECRET_KEYRING ),
PGPEncryptionUtil.BytesToHex( encryptingKey.getFingerprint() ) );
byte[] decryptedAndVerifiedMessage = PGPEncryptionUtil
.decryptAndVerify( signedAndEncryptedMessage, decryptingSecretKey, SECRET_PWD,
signingKey.getPublicKey() );
assertTrue( Arrays.equals( MESSAGE.getBytes(), decryptedAndVerifiedMessage ) );
}
示例13: testGetX509CertificateFromPgpKeyPair
import org.bouncycastle.openpgp.PGPPublicKey; //导入依赖的package包/类
@Test
public void testGetX509CertificateFromPgpKeyPair() throws Exception
{
Date today = new Date();
PGPPublicKey pgpPublicKey = PGPEncryptionUtil.findPublicKeyById( findFile( PUBLIC_KEYRING ), PUBLIC_KEY_ID );
PGPSecretKey pgpSecretKey = PGPEncryptionUtil.findSecretKeyById( findFile( SECRET_KEYRING ), SECRET_KEY_ID );
X509Certificate x509Certificate = PGPEncryptionUtil
.getX509CertificateFromPgpKeyPair( pgpPublicKey, pgpSecretKey, SECRET_PWD,
"C=ZA, ST=Western Cape, L=Cape Town, O=Thawte Consulting cc,"
+ " OU=Certification Services Division,"
+ " CN=Thawte Server CA/[email protected]",
"C=US, ST=Maryland, L=Pasadena, O=Brent Baccala,"
+ "OU=FreeSoft, CN=www.freesoft.org/[email protected]",
today, new Date( today.getTime() + ( 1000 * 60 * 60 * 24 ) ), new BigInteger( "1" ) );
assertNotNull( x509Certificate );
JcaPGPKeyConverter c = new JcaPGPKeyConverter();
PublicKey publicKey = c.getPublicKey( pgpSecretKey.getPublicKey() );
x509Certificate.verify( publicKey, new BouncyCastleProvider() );
}
示例14: readPublicKey
import org.bouncycastle.openpgp.PGPPublicKey; //导入依赖的package包/类
public static PGPPublicKey readPublicKey( InputStream is ) throws IOException, PGPException
{
PGPPublicKeyRingCollection pgpPub =
new PGPPublicKeyRingCollection( PGPUtil.getDecoderStream( is ), new JcaKeyFingerprintCalculator() );
Iterator keyRingIter = pgpPub.getKeyRings();
while ( keyRingIter.hasNext() )
{
PGPPublicKeyRing keyRing = ( PGPPublicKeyRing ) keyRingIter.next();
Iterator keyIter = keyRing.getPublicKeys();
while ( keyIter.hasNext() )
{
PGPPublicKey key = ( PGPPublicKey ) keyIter.next();
if ( key.isEncryptionKey() )
{
return key;
}
}
}
throw new IllegalArgumentException( "Can't find encryption key in key ring." );
}
示例15: rememberKey
import org.bouncycastle.openpgp.PGPPublicKey; //导入依赖的package包/类
/**
* Simply stores the key ID in {@link #discoveredKeyIds} for future
* reference of all Key IDs we've come across. This method uses a
* {@link PGPPublicKeyRing} to ensure the input is actually a valid key,
* plus locating any key IDs that have signed the key.
* <p>
* Please note {@link #discoveredKeyIds} is not used for any key functions
* of this class. It is simply for user interface convenience.
*
* @param keyRing the key ID to store (required)
*/
@SuppressWarnings("unchecked")
private void rememberKey(final PGPPublicKeyRing keyRing) {
final PGPPublicKey key = keyRing.getPublicKey();
if (key != null) {
final PgpKeyId keyId = new PgpKeyId(key);
discoveredKeyIds.add(keyId);
final Iterator<String> userIdIterator = key.getUserIDs();
while (userIdIterator.hasNext()) {
final String userId = userIdIterator.next();
final Iterator<PGPSignature> signatureIterator = key
.getSignaturesForID(userId);
while (signatureIterator.hasNext()) {
final PGPSignature signature = signatureIterator.next();
final PgpKeyId signatureKeyId = new PgpKeyId(signature);
discoveredKeyIds.add(signatureKeyId);
}
}
}
}