本文整理汇总了Java中org.bouncycastle.openpgp.PGPSecretKeyRing.getSecretKey方法的典型用法代码示例。如果您正苦于以下问题:Java PGPSecretKeyRing.getSecretKey方法的具体用法?Java PGPSecretKeyRing.getSecretKey怎么用?Java PGPSecretKeyRing.getSecretKey使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.bouncycastle.openpgp.PGPSecretKeyRing
的用法示例。
在下文中一共展示了PGPSecretKeyRing.getSecretKey方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: createPgpKey
import org.bouncycastle.openpgp.PGPSecretKeyRing; //导入方法依赖的package包/类
@Override
public PgpKey createPgpKey(final CreatePgpKeyParam createPgpKeyParam) {
assertNotNull(createPgpKeyParam, "createPgpKeyParam");
try {
final Pair<PGPPublicKeyRing, PGPSecretKeyRing> pair = createPGPSecretKeyRing(createPgpKeyParam);
final PGPPublicKeyRing pgpPublicKeyRing = pair.a;
final PGPSecretKeyRing pgpSecretKeyRing = pair.b;
final ImportKeysResult importKeysResult = new ImportKeysResult();
synchronized (this) {
importPublicKeyRing(importKeysResult, pgpPublicKeyRing);
importSecretKeyRing(importKeysResult, pgpSecretKeyRing);
}
final PGPSecretKey secretKey = pgpSecretKeyRing.getSecretKey();
final PgpKey pgpKey = getPgpKey(new PgpKeyId(secretKey.getKeyID()));
assertNotNull(pgpKey, "pgpKey");
return pgpKey;
} catch (IOException | NoSuchAlgorithmException | PGPException e) {
throw new RuntimeException(e);
}
}
示例2: OpenPGPSecretKey
import org.bouncycastle.openpgp.PGPSecretKeyRing; //导入方法依赖的package包/类
public OpenPGPSecretKey(String keyId, InputStream secretKeyRing, char[] password) throws IOException {
PGPObjectFactory pgpObjectFactory = new BcPGPObjectFactory(PGPUtil.getDecoderStream(secretKeyRing));
for (Object it = pgpObjectFactory.nextObject(); it != null; it = pgpObjectFactory.nextObject()) {
PGPSecretKeyRing pgpSecretKeyRing = (PGPSecretKeyRing) it;
PGPSecretKey pgpSecretKey = pgpSecretKeyRing.getSecretKey();
if (keyId == null || keyId.isEmpty() || Long.valueOf(keyId, 16) == (pgpSecretKey.getKeyID() & MASK)) {
this.secretKey = pgpSecretKey;
break;
}
}
// sanity check
if (secretKey == null) {
throw new IllegalArgumentException("Secret key " + keyId + " not found");
}
this.password = password;
}
示例3: doInBackground
import org.bouncycastle.openpgp.PGPSecretKeyRing; //导入方法依赖的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;
}
示例4: findPrivateKeyWithkeyId
import org.bouncycastle.openpgp.PGPSecretKeyRing; //导入方法依赖的package包/类
public static PGPPrivateKey findPrivateKeyWithkeyId(long keyid, String passphrase, PGPPassphraseAccessor passphraseAccessor,
String provider, PGPSecretKeyRingCollection pgpSec) throws PGPException {
for (Iterator<?> i = pgpSec.getKeyRings(); i.hasNext();) {
Object data = i.next();
if (data instanceof PGPSecretKeyRing) {
PGPSecretKeyRing keyring = (PGPSecretKeyRing) data;
PGPSecretKey secKey = keyring.getSecretKey(keyid);
if (secKey != null) {
if (passphrase == null && passphraseAccessor != null) {
// get passphrase from accessor // only primary/master key has user IDS
@SuppressWarnings("unchecked")
Iterator<String> userIDs = keyring.getSecretKey().getUserIDs();
while (passphrase == null && userIDs.hasNext()) {
passphrase = passphraseAccessor.getPassphrase(userIDs.next());
}
}
if (passphrase != null) {
PGPPrivateKey privateKey = secKey.extractPrivateKey(new JcePBESecretKeyDecryptorBuilder().setProvider(provider)
.build(passphrase.toCharArray()));
if (privateKey != null) {
return privateKey;
}
}
}
}
}
return null;
}
示例5: findSecretKeysWithPrivateKeyAndUserId
import org.bouncycastle.openpgp.PGPSecretKeyRing; //导入方法依赖的package包/类
public static List<PGPSecretKeyAndPrivateKeyAndUserId> findSecretKeysWithPrivateKeyAndUserId(Map<String, String> sigKeyUserId2Password,
String provider, PGPSecretKeyRingCollection pgpSec) throws PGPException {
List<PGPSecretKeyAndPrivateKeyAndUserId> result = new ArrayList<PGPSecretKeyAndPrivateKeyAndUserId>(sigKeyUserId2Password.size());
for (Iterator<?> i = pgpSec.getKeyRings(); i.hasNext();) {
Object data = i.next();
if (data instanceof PGPSecretKeyRing) {
PGPSecretKeyRing keyring = (PGPSecretKeyRing) data;
PGPSecretKey primaryKey = keyring.getSecretKey();
List<String> useridParts = new ArrayList<String>(sigKeyUserId2Password.keySet());
String[] foundKeyUserIdForUserIdPart = findFirstKeyUserIdContainingOneOfTheParts(useridParts, primaryKey.getPublicKey());
if (foundKeyUserIdForUserIdPart == null) {
LOG.debug("No User ID found in primary key with key ID {} containing one of the parts {}", primaryKey.getKeyID(),
useridParts);
continue;
}
LOG.debug("User ID {} found in primary key with key ID {} containing one of the parts {}", new Object[] {
foundKeyUserIdForUserIdPart[0], primaryKey.getKeyID(), useridParts });
// add all signing keys
for (Iterator<PGPSecretKey> iterKey = keyring.getSecretKeys(); iterKey.hasNext();) {
PGPSecretKey secKey = iterKey.next();
if (isSigningKey(secKey)) {
PGPPrivateKey privateKey = secKey.extractPrivateKey(new JcePBESecretKeyDecryptorBuilder().setProvider(provider)
.build(sigKeyUserId2Password.get(foundKeyUserIdForUserIdPart[1]).toCharArray()));
if (privateKey != null) {
result.add(new PGPSecretKeyAndPrivateKeyAndUserId(secKey, privateKey, foundKeyUserIdForUserIdPart[0]));
LOG.debug("Private key with user ID {} and key ID {} added to the signing keys",
foundKeyUserIdForUserIdPart[0], Long.toString(privateKey.getKeyID()));
}
}
}
}
}
return result;
}
示例6: mergeSecretKey
import org.bouncycastle.openpgp.PGPSecretKeyRing; //导入方法依赖的package包/类
private PGPSecretKeyRing mergeSecretKey(PGPSecretKeyRing secretKeyRing, final PGPSecretKey secretKey) {
assertNotNull(secretKeyRing, "secretKeyRing");
assertNotNull(secretKey, "secretKey");
PGPSecretKey oldSecretKey = secretKeyRing.getSecretKey(secretKey.getKeyID());
if (oldSecretKey == null)
secretKeyRing = PGPSecretKeyRing.insertSecretKey(secretKeyRing, secretKey);
// else: there is nothing to merge - a secret key is immutable. btw. it contains a public key - but without signatures.
return secretKeyRing;
}
示例7: addUserId
import org.bouncycastle.openpgp.PGPSecretKeyRing; //导入方法依赖的package包/类
private static PGPPublicKeyRing addUserId(
final PGPPublicKeyRing publicKeyRing, final String userId,
final PGPSecretKeyRing secretKeyRing,
final char[] passphrase,
PGPContentSignerBuilder signerBuilder,
final PGPSignatureSubpacketVector hashedSubpackets,
final PGPSignatureSubpacketVector unhashedSubpackets) throws PGPException {
assertNotNull(publicKeyRing, "publicKeyRing");
assertNotNull(userId, "userId");
final PGPPublicKey masterPublicKey = getMasterKeyOrFail(publicKeyRing);
final PGPSecretKey masterSecretKey = secretKeyRing.getSecretKey(masterPublicKey.getKeyID());
assertNotNull(masterSecretKey, "masterSecretKey");
final PGPPrivateKey privateKey = extractPrivateKey(masterSecretKey, passphrase);
final PGPSignatureGenerator sGen = new PGPSignatureGenerator(signerBuilder);
sGen.init(PGPSignature.POSITIVE_CERTIFICATION, privateKey);
sGen.setHashedSubpackets(hashedSubpackets);
sGen.setUnhashedSubpackets(unhashedSubpackets);
final PGPSignature certification = sGen.generateCertification(userId, masterPublicKey);
final PGPPublicKey newMasterPublicKey = PGPPublicKey.addCertification(masterPublicKey, userId, certification);
PGPPublicKeyRing result = PGPPublicKeyRing.removePublicKey(publicKeyRing, masterPublicKey);
result = PGPPublicKeyRing.insertPublicKey(result, newMasterPublicKey);
return result;
}
示例8: test_key
import org.bouncycastle.openpgp.PGPSecretKeyRing; //导入方法依赖的package包/类
public void test_key(BigInteger keyId, String passphrase)
throws Exception
{
PGPSecretKeyRingCollection secretKeyRing = loadSecretKeyCollection("secring.gpg");
PGPSecretKeyRing secretKey = secretKeyRing.getSecretKeyRing(keyId.longValue());
assertNotNull("Could not locate secret keyring with Id=" + keyId.toString(16), secretKey);
PGPSecretKey key = secretKey.getSecretKey();
assertNotNull("Could not locate secret key!", key);
try
{
PGPDigestCalculatorProvider calcProvider = new JcaPGPDigestCalculatorProviderBuilder()
.setProvider(BouncyCastleProvider.PROVIDER_NAME).build();
PBESecretKeyDecryptor decryptor = new JcePBESecretKeyDecryptorBuilder(calcProvider)
.setProvider(BouncyCastleProvider.PROVIDER_NAME).build(passphrase.toCharArray());
PGPPrivateKey privateKey = key.extractPrivateKey(decryptor);
assertTrue(privateKey.getKeyID() == keyId.longValue());
}
catch (PGPException e)
{
throw new PGPException("Password incorrect!", e);
}
// all fine!
}
示例9: testDecrypt
import org.bouncycastle.openpgp.PGPSecretKeyRing; //导入方法依赖的package包/类
private void testDecrypt(PGPSecretKeyRing secretKeyRing)
throws Exception
{
PGPObjectFactory pgpF = new PGPObjectFactory(testMessage);
PGPEncryptedDataList encList = (PGPEncryptedDataList)pgpF.nextObject();
PGPPublicKeyEncryptedData encP = (PGPPublicKeyEncryptedData)encList.get(0);
PGPSecretKey secretKey = secretKeyRing.getSecretKey(); // secretKeyRing.getSecretKey(encP.getKeyID());
//
// PGPPrivateKey pgpPrivKey = secretKey.extractPrivateKey()extractPrivateKey(null);
//
// clear = encP.getDataStream(pgpPrivKey, "BC");
//
// bOut.reset();
//
// while ((ch = clear.read()) >= 0)
// {
// bOut.write(ch);
// }
//
// out = bOut.toByteArray();
//
// if (!areEqual(out, text))
// {
// fail("wrong plain text in generated packet");
// }
}
示例10: setUp
import org.bouncycastle.openpgp.PGPSecretKeyRing; //导入方法依赖的package包/类
@Before
public void setUp() throws Exception {
try (FileInputStream file = new FileInputStream("src/test/resources/secret-keyring.gpg")) {
final PGPSecretKeyRing keyRing = new PGPSecretKeyRing(file, new JcaKeyFingerprintCalculator());
this.key = new MasterKey(keyRing.getSecretKey(0x8C7035EF8838238CL));
}
}
示例11: setUp
import org.bouncycastle.openpgp.PGPSecretKeyRing; //导入方法依赖的package包/类
@Before
public void setUp() throws Exception {
try (FileInputStream file = new FileInputStream("src/test/resources/secret-keyring.gpg")) {
final PGPSecretKeyRing keyRing = new PGPSecretKeyRing(file, new JcaKeyFingerprintCalculator());
final MasterKey masterKey = new MasterKey(keyRing.getSecretKey(0x8C7035EF8838238CL));
this.key = new SubKey(keyRing.getSecretKey(0xA3A5D038FF30574EL), masterKey);
}
}
示例12: setUp
import org.bouncycastle.openpgp.PGPSecretKeyRing; //导入方法依赖的package包/类
@Before
public void setUp() throws Exception {
try (FileInputStream file = new FileInputStream("src/test/resources/secret-keyring.gpg")) {
final PGPSecretKeyRing keyRing = new PGPSecretKeyRing(file, new JcaKeyFingerprintCalculator());
final PGPSecretKey key = keyRing.getSecretKey(0x8C7035EF8838238CL);
final Iterator<?> signatures = key.getPublicKey().getSignatures();
this.signature = new KeySignature((PGPSignature) signatures.next());
}
}
示例13: testMessageSigning
import org.bouncycastle.openpgp.PGPSecretKeyRing; //导入方法依赖的package包/类
@Test
public void testMessageSigning() throws Exception
{
InputStream secondSecretStream = findFile( PLUGIN_PRIVATE_KEY );
InputStream secondPublicStream = findFile( PLUGIN_PUBLIC_KEY );
PGPSecretKeyRingCollection secretKeyRingCollection =
new PGPSecretKeyRingCollection( PGPUtil.getDecoderStream( secondSecretStream ),
new JcaKeyFingerprintCalculator() );
PGPSecretKeyRing secretKeyRing = secretKeyRingCollection
.getSecretKeyRing( secretKeyRingCollection.iterator().next().getSecretKey().getKeyID() );
PGPSecretKey secondSecretKey = secretKeyRing.getSecretKey();
PGPPublicKeyRingCollection secondPublicKeyRingCollection =
new PGPPublicKeyRingCollection( PGPUtil.getDecoderStream( secondPublicStream ),
new JcaKeyFingerprintCalculator() );
PGPPublicKeyRing pgpKeyring = secondPublicKeyRingCollection
.getPublicKeyRing( secondPublicKeyRingCollection.iterator().next().getPublicKey().getKeyID() );
byte[] encryptedMessage =
PGPEncryptionUtil.encrypt( "Test message.\n".getBytes(), pgpKeyring.getPublicKey(), true );
byte[] signedMessageArmor =
PGPEncryptionUtil.clearSign( encryptedMessage, secondSecretKey, "123".toCharArray(), "" );
String signedMessage = new String( signedMessageArmor, "UTF-8" );
logger.info( "\n" + signedMessage );
logger.info( "\n======================" );
boolean result = PGPEncryptionUtil.verifyClearSign( signedMessageArmor, pgpKeyring );
if ( result )
{
logger.info( "signature verified." );
}
else
{
logger.info( "signature verification failed." );
}
byte[] extracted = PGPEncryptionUtil.extractContentFromClearSign( signedMessage.getBytes() );
byte[] decrypted = PGPEncryptionUtil.decrypt( extracted, secretKeyRing, "123" );
logger.info( "Decrypted message \n" + new String( decrypted, "UTF-8" ) );
assertEquals( true, result );
}
示例14: testClearSign
import org.bouncycastle.openpgp.PGPSecretKeyRing; //导入方法依赖的package包/类
@Test
public void testClearSign() throws Exception
{
InputStream secondSecretStream = findFile( PLUGIN_PRIVATE_KEY );
InputStream secondPublicStream = findFile( PLUGIN_PUBLIC_KEY );
PGPSecretKeyRingCollection secretKeyRingCollection =
new PGPSecretKeyRingCollection( PGPUtil.getDecoderStream( secondSecretStream ),
new JcaKeyFingerprintCalculator() );
PGPSecretKeyRing secretKeyRing = secretKeyRingCollection
.getSecretKeyRing( secretKeyRingCollection.iterator().next().getPublicKey().getKeyID() );
PGPSecretKey secondSecretKey = secretKeyRing.getSecretKey();
PGPPublicKeyRingCollection secondPublicKeyRingCollection =
new PGPPublicKeyRingCollection( PGPUtil.getDecoderStream( secondPublicStream ),
new JcaKeyFingerprintCalculator() );
PGPPublicKeyRing pgpKeyring = secondPublicKeyRingCollection
.getPublicKeyRing( secondPublicKeyRingCollection.iterator().next().getPublicKey().getKeyID() );
byte[] signedMessageArmor = PGPEncryptionUtil
.clearSign( IOUtils.toString( findFile( "message.txt" ) ).getBytes(), secondSecretKey,
"123".toCharArray(), "" );
String signedMessage = new String( signedMessageArmor, "UTF-8" );
logger.info( "\n" + signedMessage );
boolean result = PGPEncryptionUtil.verifyClearSign( signedMessage.getBytes(), pgpKeyring );
if ( result )
{
logger.info( "signature verified." );
}
else
{
logger.info( "signature verification failed." );
}
assertEquals( true, result );
}