本文整理汇总了Java中org.bouncycastle.openpgp.PGPSecretKeyRingCollection类的典型用法代码示例。如果您正苦于以下问题:Java PGPSecretKeyRingCollection类的具体用法?Java PGPSecretKeyRingCollection怎么用?Java PGPSecretKeyRingCollection使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
PGPSecretKeyRingCollection类属于org.bouncycastle.openpgp包,在下文中一共展示了PGPSecretKeyRingCollection类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getSecretKey
import org.bouncycastle.openpgp.PGPSecretKeyRingCollection; //导入依赖的package包/类
static PGPSecretKey getSecretKey(String privateKeyData) throws IOException, PGPException {
PGPPrivateKey privKey = null;
try (InputStream privStream = new ArmoredInputStream(new ByteArrayInputStream(privateKeyData.getBytes("UTF-8")))) {
PGPSecretKeyRingCollection pgpSec = new PGPSecretKeyRingCollection(PGPUtil.getDecoderStream(privStream), new JcaKeyFingerprintCalculator());
Iterator keyRingIter = pgpSec.getKeyRings();
while (keyRingIter.hasNext()) {
PGPSecretKeyRing keyRing = (PGPSecretKeyRing)keyRingIter.next();
Iterator keyIter = keyRing.getSecretKeys();
while (keyIter.hasNext()) {
PGPSecretKey key = (PGPSecretKey)keyIter.next();
if (key.isSigningKey()) {
return key;
}
}
}
}
throw new IllegalArgumentException("Can't find signing key in key ring.");
}
示例2: asLiteral
import org.bouncycastle.openpgp.PGPSecretKeyRingCollection; //导入依赖的package包/类
private static PGPLiteralData asLiteral( final byte[] message, final InputStream secretKeyRing,
final String secretPwd ) throws IOException, PGPException
{
PGPPrivateKey key = null;
PGPPublicKeyEncryptedData encrypted = null;
final PGPSecretKeyRingCollection keys =
new PGPSecretKeyRingCollection( PGPUtil.getDecoderStream( secretKeyRing ),
new JcaKeyFingerprintCalculator() );
for ( final Iterator<PGPPublicKeyEncryptedData> i = getEncryptedObjects( message );
( key == null ) && i.hasNext(); )
{
encrypted = i.next();
key = getPrivateKey( keys, encrypted.getKeyID(), secretPwd );
}
if ( key == null )
{
throw new IllegalArgumentException( "secret key for message not found." );
}
final InputStream stream = encrypted
.getDataStream( new JcePublicKeyDataDecryptorFactoryBuilder().setProvider( provider ).build( key ) );
return asLiteral( stream );
}
示例3: readSecretKey
import org.bouncycastle.openpgp.PGPSecretKeyRingCollection; //导入依赖的package包/类
static PGPSecretKey readSecretKey() throws Exception {
InputStream input = new ByteArrayInputStream(getSecKeyRing());
PGPSecretKeyRingCollection pgpSec = new PGPSecretKeyRingCollection(PGPUtil.getDecoderStream(input),
new BcKeyFingerprintCalculator());
@SuppressWarnings("rawtypes")
Iterator keyRingIter = pgpSec.getKeyRings();
while (keyRingIter.hasNext()) {
PGPSecretKeyRing keyRing = (PGPSecretKeyRing) keyRingIter.next();
@SuppressWarnings("rawtypes")
Iterator keyIter = keyRing.getSecretKeys();
while (keyIter.hasNext()) {
PGPSecretKey key = (PGPSecretKey) keyIter.next();
if (key.isSigningKey()) {
return key;
}
}
}
throw new IllegalArgumentException("Can't find signing key in key ring.");
}
示例4: checkSecretKeyRingWithPersonalCertificate
import org.bouncycastle.openpgp.PGPSecretKeyRingCollection; //导入依赖的package包/类
private void checkSecretKeyRingWithPersonalCertificate(byte[] keyRing)
throws Exception
{
PGPSecretKeyRingCollection secCol = new PGPSecretKeyRingCollection(keyRing, new BcKeyFingerprintCalculator());
int count = 0;
for (Iterator rIt = secCol.getKeyRings(); rIt.hasNext();)
{
PGPSecretKeyRing ring = (PGPSecretKeyRing)rIt.next();
for (Iterator it = ring.getExtraPublicKeys(); it.hasNext();)
{
it.next();
count++;
}
}
if (count != 1)
{
fail("personal certificate data subkey not found - count = " + count);
}
}
示例5: readSecretKey
import org.bouncycastle.openpgp.PGPSecretKeyRingCollection; //导入依赖的package包/类
private static PGPSecretKey readSecretKey( InputStream is ) throws IOException, PGPException
{
PGPSecretKeyRingCollection pgpSec =
new PGPSecretKeyRingCollection( PGPUtil.getDecoderStream( is ), new JcaKeyFingerprintCalculator() );
Iterator keyRingIter = pgpSec.getKeyRings();
while ( keyRingIter.hasNext() )
{
PGPSecretKeyRing keyRing = ( PGPSecretKeyRing ) keyRingIter.next();
Iterator keyIter = keyRing.getSecretKeys();
while ( keyIter.hasNext() )
{
PGPSecretKey key = ( PGPSecretKey ) keyIter.next();
if ( key.isSigningKey() )
{
return key;
}
}
}
throw new IllegalArgumentException( "Can't find signing key in key ring." );
}
示例6: lookupKeyPair
import org.bouncycastle.openpgp.PGPSecretKeyRingCollection; //导入依赖的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);
}
示例7: readSecretKey
import org.bouncycastle.openpgp.PGPSecretKeyRingCollection; //导入依赖的package包/类
private PGPSecretKey readSecretKey() throws IOException, PGPException {
PGPSecretKeyRingCollection pgpSec = new PGPSecretKeyRingCollection(
PGPUtil.getDecoderStream(new ByteArrayInputStream(config.keypair.getBytes())),
new JcaKeyFingerprintCalculator());
Iterator<PGPSecretKeyRing> keyRings = pgpSec.getKeyRings();
while (keyRings.hasNext()) {
PGPSecretKeyRing keyRing = (PGPSecretKeyRing) keyRings.next();
Iterator<PGPSecretKey> keys = keyRing.getSecretKeys();
while (keys.hasNext()) {
PGPSecretKey key = (PGPSecretKey) keys.next();
if (key.isSigningKey()) {
return key;
}
}
}
throw new IllegalStateException("Can't find signing key in key ring.");
}
示例8: checkSecretKeyRingWithPersonalCertificate
import org.bouncycastle.openpgp.PGPSecretKeyRingCollection; //导入依赖的package包/类
private void checkSecretKeyRingWithPersonalCertificate(byte[] keyRing)
throws Exception
{
PGPSecretKeyRingCollection secCol = new PGPSecretKeyRingCollection(keyRing);
int count = 0;
for (Iterator rIt = secCol.getKeyRings(); rIt.hasNext();)
{
PGPSecretKeyRing ring = (PGPSecretKeyRing)rIt.next();
for (Iterator it = ring.getExtraPublicKeys(); it.hasNext();)
{
it.next();
count++;
}
}
if (count != 1)
{
fail("personal certificate data subkey not found - count = " + count);
}
}
示例9: readSecretKey
import org.bouncycastle.openpgp.PGPSecretKeyRingCollection; //导入依赖的package包/类
private static PGPSecretKey readSecretKey(InputStream input, String keyId)
throws IOException, PGPException {
for (@SuppressWarnings("unchecked")
Iterator<PGPSecretKeyRing> keyRingIter = new PGPSecretKeyRingCollection(
PGPUtil.getDecoderStream(input)).getKeyRings(); keyRingIter
.hasNext();) {
for (@SuppressWarnings("unchecked")
Iterator<PGPSecretKey> keyIter = keyRingIter.next().getSecretKeys(); keyIter
.hasNext();) {
PGPSecretKey key = keyIter.next();
String id = bytArrayToHex(key.getPublicKey().getFingerprint());
if ((keyId == null || keyId.equalsIgnoreCase(id.substring(id
.length() - 8))) && key.isSigningKey()) {
return key;
}
}
}
throw new IllegalArgumentException("No signing key in keyring");
}
示例10: readSecretKey
import org.bouncycastle.openpgp.PGPSecretKeyRingCollection; //导入依赖的package包/类
protected PGPSecretKey readSecretKey(PGPSecretKeyRingCollection keyRings, final PgpKeyId keyId, String sourceDescription) {
PGPSecretKey key = findSecretKey(keyRings, keyId);
if (key == null) {
throw new InvalidUserDataException("did not find secret key for id \'" + String.valueOf(keyId) + "\' in key source \'" + sourceDescription + "\'");
}
return key;
}
示例11: findSecretKey
import org.bouncycastle.openpgp.PGPSecretKeyRingCollection; //导入依赖的package包/类
@Nullable
private PGPSecretKey findSecretKey(PGPSecretKeyRingCollection keyRings, PgpKeyId keyId) {
Iterator<PGPSecretKeyRing> keyRingIterator = uncheckedCast(keyRings.getKeyRings());
while (keyRingIterator.hasNext()) {
PGPSecretKeyRing keyRing = keyRingIterator.next();
Iterator<PGPSecretKey> secretKeyIterator = uncheckedCast(keyRing.getSecretKeys());
while (secretKeyIterator.hasNext()) {
PGPSecretKey secretKey = secretKeyIterator.next();
if (hasId(keyId, secretKey)) {
return secretKey;
}
}
}
return null;
}
示例12: readSecretKey
import org.bouncycastle.openpgp.PGPSecretKeyRingCollection; //导入依赖的package包/类
public static PGPSecretKey readSecretKey(InputStream in)
throws IOException, PGPException
{
in = PGPUtil.getDecoderStream(in);
PGPSecretKeyRingCollection keyRingCollection = new PGPSecretKeyRingCollection(in, new JcaKeyFingerprintCalculator());
//
// We just loop through the collection till we find a key suitable for signing.
// In the real world you would probably want to be a bit smarter about this.
//
PGPSecretKey secretKey = null;
Iterator<PGPSecretKeyRing> rIt = keyRingCollection.getKeyRings();
while (secretKey == null && rIt.hasNext()) {
PGPSecretKeyRing keyRing = rIt.next();
Iterator<PGPSecretKey> kIt = keyRing.getSecretKeys();
while (secretKey == null && kIt.hasNext()) {
PGPSecretKey key = kIt.next();
if (key.isSigningKey()) {
secretKey = key;
}
}
}
// Validate secret key
if (secretKey == null) {
throw new IllegalArgumentException("Can't find private key in the key ring.");
}
if (!secretKey.isSigningKey()) {
throw new IllegalArgumentException("Private key does not allow signing.");
}
if (secretKey.getPublicKey().isRevoked()) {
throw new IllegalArgumentException("Private key has been revoked.");
}
return secretKey;
}
示例13: readSecretKey
import org.bouncycastle.openpgp.PGPSecretKeyRingCollection; //导入依赖的package包/类
/**
* A simple routine that opens a key ring file and loads the first available
* key suitable for signature generation.
*
* @param input
* stream to read the secret key ring collection from.
* @return a secret key.
* @throws IOException
* on a problem with using the input stream.
* @throws PGPException
* if there is an issue parsing the input stream.
*/
static PGPSecretKey readSecretKey(InputStream input) throws IOException, PGPException
{
PGPSecretKeyRingCollection pgpSec = new PGPSecretKeyRingCollection(PGPUtil.getDecoderStream(input),
new JcaKeyFingerprintCalculator());
//
// we just loop through the collection till we find a key suitable for
// encryption, in the real
// world you would probably want to be a bit smarter about this.
//
Iterator keyRingIter = pgpSec.getKeyRings();
while (keyRingIter.hasNext())
{
PGPSecretKeyRing keyRing = (PGPSecretKeyRing) keyRingIter.next();
Iterator keyIter = keyRing.getSecretKeys();
while (keyIter.hasNext())
{
PGPSecretKey key = (PGPSecretKey) keyIter.next();
if (key.isSigningKey())
{
return key;
}
}
}
throw new IllegalArgumentException("Can't find signing key in key ring.");
}
示例14: readSecretKey
import org.bouncycastle.openpgp.PGPSecretKeyRingCollection; //导入依赖的package包/类
public static PGPSecretKey readSecretKey(InputStream in)
throws IOException, PGPException, NoSuchProviderException {
// Get the decoder stream (auto-disarming)
in = PGPUtil.getDecoderStream(in);
// Open the key ring
PGPSecretKeyRingCollection coll = new PGPSecretKeyRingCollection(in);
// Find key
return readSecretKey(coll);
}
示例15: getKeyInfo
import org.bouncycastle.openpgp.PGPSecretKeyRingCollection; //导入依赖的package包/类
public static String getKeyInfo(PGPSecretKeyRingCollection coll) {
StringBuffer info = new StringBuffer();
// Iterate through key rings
Iterator<?> rings = coll.getKeyRings();
while (rings.hasNext()) {
PGPSecretKeyRing ring = (PGPSecretKeyRing)rings.next();
Iterator<?> keys = ring.getSecretKeys();
while (keys.hasNext()) {
info.append(getKeyInfo((PGPSecretKey)keys.next())).append("\n");
}
}
return info.toString();
}