本文整理汇总了Java中org.spongycastle.openpgp.PGPPublicKeyRing类的典型用法代码示例。如果您正苦于以下问题:Java PGPPublicKeyRing类的具体用法?Java PGPPublicKeyRing怎么用?Java PGPPublicKeyRing使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
PGPPublicKeyRing类属于org.spongycastle.openpgp包,在下文中一共展示了PGPPublicKeyRing类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: PGPPubkeyEncryptionUtil
import org.spongycastle.openpgp.PGPPublicKeyRing; //导入依赖的package包/类
public PGPPubkeyEncryptionUtil(String pubkeyBlock){
InputStream in = new ByteArrayInputStream(pubkeyBlock.getBytes());
publicKey = null;
try {
in = PGPUtil.getDecoderStream(in);
JcaPGPPublicKeyRingCollection pgpPub = new JcaPGPPublicKeyRingCollection(in);
in.close();
Iterator<PGPPublicKeyRing> rIt = pgpPub.getKeyRings();
while (publicKey == null && rIt.hasNext()) {
PGPPublicKeyRing keyRing = rIt.next();
Iterator<PGPPublicKey> kIt = keyRing.getPublicKeys();
while (publicKey == null && kIt.hasNext()) {
PGPPublicKey k = kIt.next();
if (k.isEncryptionKey()) {
publicKey = k;
}
}
}
} catch (Exception e){
Log.e(LOGCATTAG, e.toString());
}
}
示例2: readPublicKeyFromCol
import org.spongycastle.openpgp.PGPPublicKeyRing; //导入依赖的package包/类
public static PGPPublicKey readPublicKeyFromCol(InputStream in) throws IOException, PGPException {
in = PGPUtil.getDecoderStream(in);
PGPPublicKeyRingCollection pgpPub = new PGPPublicKeyRingCollection(in, new BcKeyFingerprintCalculator());
PGPPublicKey key = null;
Iterator<PGPPublicKeyRing> keyRings = pgpPub.getKeyRings();
while (key == null && keyRings.hasNext()) {
PGPPublicKeyRing keyRing = keyRings.next();
Iterator<PGPPublicKey> keys = keyRing.getPublicKeys();
while (keys.hasNext()) {
PGPPublicKey k = keys.next();
if (k.isEncryptionKey()) {
key = k;
break;
}
}
}
if (key == null)
throw new PGPException("Can't find a valid encryption key in key ring.");
return key;
}
示例3: getEncryptionKey
import org.spongycastle.openpgp.PGPPublicKeyRing; //导入依赖的package包/类
public static PGPPublicKey getEncryptionKey(PGPPublicKeyRing publicKeyring) {
@SuppressWarnings("unchecked")
Iterator<PGPPublicKey> iter = publicKeyring.getPublicKeys();
while (iter.hasNext()) {
PGPPublicKey pk = iter.next();
if (!pk.isMasterKey()) {
int keyFlags = getKeyFlags(pk);
if ((keyFlags & PGPKeyFlags.CAN_ENCRYPT_COMMS) == PGPKeyFlags.CAN_ENCRYPT_COMMS)
return pk;
}
}
// legacy key format support
return getLegacyEncryptionKey(publicKeyring);
}
示例4: createCertificate
import org.spongycastle.openpgp.PGPPublicKeyRing; //导入依赖的package包/类
public static X509Certificate createCertificate(byte[] privateKeyData, byte[] publicKeyData, String passphrase)
throws PGPException, IOException, InvalidKeyException, IllegalStateException,
NoSuchAlgorithmException, SignatureException, CertificateException, NoSuchProviderException, OperatorCreationException {
PGPSecretKeyRing secRing = new PGPSecretKeyRing(privateKeyData, sFingerprintCalculator);
PGPPublicKeyRing pubRing = new PGPPublicKeyRing(publicKeyData, sFingerprintCalculator);
PGPDigestCalculatorProvider sha1Calc = new JcaPGPDigestCalculatorProviderBuilder().build();
PBESecretKeyDecryptor decryptor = new JcePBESecretKeyDecryptorBuilder(sha1Calc)
.setProvider(PGP.PROVIDER)
.build(passphrase.toCharArray());
// secret key
PGPSecretKey secKey = secRing.getSecretKey();
return createCertificate(pubRing, secKey.extractPrivateKey(decryptor));
}
示例5: updateAccountManager
import org.spongycastle.openpgp.PGPPublicKeyRing; //导入依赖的package包/类
/** Stores the public keyring to the system {@link AccountManager}. */
public void updateAccountManager(Context context)
throws IOException, InvalidKeyException,
IllegalStateException, NoSuchAlgorithmException, SignatureException,
CertificateException, NoSuchProviderException, PGPException, OperatorCreationException {
AccountManager am = (AccountManager) context.getSystemService(Context.ACCOUNT_SERVICE);
Account account = Authenticator.getDefaultAccount(am);
if (account != null) {
PGPPublicKeyRing pubRing = getPublicKeyRing();
// regenerate bridge certificate
byte[] bridgeCertData = X509Bridge.createCertificate(pubRing,
mPair.authKey.getPrivateKey()).getEncoded();
byte[] publicKeyData = pubRing.getEncoded();
am.setUserData(account, Authenticator.DATA_PUBLICKEY,
Base64.encodeToString(publicKeyData, Base64.NO_WRAP));
am.setUserData(account, Authenticator.DATA_BRIDGECERT,
Base64.encodeToString(bridgeCertData, Base64.NO_WRAP));
}
}
示例6: store
import org.spongycastle.openpgp.PGPPublicKeyRing; //导入依赖的package包/类
/** Creates public and secret keyring for a given keypair. */
public static PGPKeyPairRing store(PGPDecryptedKeyPairRing pair,
String id,
String passphrase)
throws PGPException {
PGPDigestCalculator sha1Calc = new JcaPGPDigestCalculatorProviderBuilder().build().get(HashAlgorithmTags.SHA1);
PGPKeyRingGenerator keyRingGen = new PGPKeyRingGenerator(PGPSignature.POSITIVE_CERTIFICATION, pair.signKey,
id, sha1Calc, null, null,
new JcaPGPContentSignerBuilder(pair.signKey.getPublicKey().getAlgorithm(), HashAlgorithmTags.SHA1),
new JcePBESecretKeyEncryptorBuilder(PGPEncryptedData.AES_256, sha1Calc)
.setProvider(PROVIDER).build(passphrase.toCharArray()));
keyRingGen.addSubKey(pair.encryptKey);
PGPSecretKeyRing secRing = keyRingGen.generateSecretKeyRing();
PGPPublicKeyRing pubRing = keyRingGen.generatePublicKeyRing();
return new PGPKeyPairRing(pubRing, secRing);
}
示例7: createCertificate
import org.spongycastle.openpgp.PGPPublicKeyRing; //导入依赖的package包/类
public static X509Certificate createCertificate(byte[] privateKeyData, byte[] publicKeyData, String passphrase, String subjectAltName)
throws PGPException, IOException, InvalidKeyException, IllegalStateException,
NoSuchAlgorithmException, SignatureException, CertificateException, NoSuchProviderException {
KeyFingerPrintCalculator fpr = new BcKeyFingerprintCalculator();
PGPSecretKeyRing secRing = new PGPSecretKeyRing(privateKeyData, fpr);
PGPPublicKeyRing pubRing = new PGPPublicKeyRing(publicKeyData, fpr);
PGPDigestCalculatorProvider sha1Calc = new JcaPGPDigestCalculatorProviderBuilder().build();
PBESecretKeyDecryptor decryptor = new JcePBESecretKeyDecryptorBuilder(sha1Calc)
.setProvider(PGP.PROVIDER)
.build(passphrase.toCharArray());
// secret key
PGPSecretKey secKey = secRing.getSecretKey();
return createCertificate(pubRing, secKey.extractPrivateKey(decryptor), subjectAltName);
}
示例8: signPublicKey
import org.spongycastle.openpgp.PGPPublicKeyRing; //导入依赖的package包/类
/**
* Searches for the master (signing) key in the given public keyring and
* signs it with our master key.
* @return the same public keyring with the signed key. This is suitable to
* be imported directly into GnuPG.
* @see #signPublicKey(PGPPublicKey, String)
*/
@SuppressWarnings("unchecked")
public PGPPublicKeyRing signPublicKey(byte[] publicKeyring, String id)
throws PGPException, IOException, SignatureException {
PGPObjectFactory reader = new PGPObjectFactory(publicKeyring);
Object o = reader.nextObject();
while (o != null) {
if (o instanceof PGPPublicKeyRing) {
PGPPublicKeyRing pubRing = (PGPPublicKeyRing) o;
Iterator<PGPPublicKey> iter = pubRing.getPublicKeys();
while (iter.hasNext()) {
PGPPublicKey pk = iter.next();
if (pk.isMasterKey()) {
PGPPublicKey signed = signPublicKey(pk, id);
return PGPPublicKeyRing.insertPublicKey(pubRing, signed);
}
}
}
o = reader.nextObject();
}
throw new PGPException("invalid keyring data.");
}
示例9: updateAccountManager
import org.spongycastle.openpgp.PGPPublicKeyRing; //导入依赖的package包/类
/** Stores the public keyring to the system {@link AccountManager}. */
public void updateAccountManager(Context context)
throws IOException, CertificateEncodingException, InvalidKeyException,
IllegalStateException, NoSuchAlgorithmException, SignatureException,
CertificateException, NoSuchProviderException, PGPException {
AccountManager am = (AccountManager) context.getSystemService(Context.ACCOUNT_SERVICE);
Account account = Authenticator.getDefaultAccount(am);
if (account != null) {
PGPPublicKeyRing pubRing = getPublicKeyRing();
// regenerate bridge certificate
byte[] bridgeCertData = X509Bridge.createCertificate(pubRing,
mPair.signKey.getPrivateKey(), null).getEncoded();
byte[] publicKeyData = pubRing.getEncoded();
am.setUserData(account, Authenticator.DATA_PUBLICKEY,
Base64.encodeToString(publicKeyData, Base64.NO_WRAP));
am.setUserData(account, Authenticator.DATA_BRIDGECERT,
Base64.encodeToString(bridgeCertData, Base64.NO_WRAP));
}
}
示例10: getEncryptCoder
import org.spongycastle.openpgp.PGPPublicKeyRing; //导入依赖的package包/类
/** Returns a {@link Coder} instance for encrypting data. */
public static Coder getEncryptCoder(Context context, EndpointServer server, PersonalKey key, String[] recipients) {
// get recipients public keys from users database
PGPPublicKey keys[] = new PGPPublicKey[recipients.length];
for (int i = 0; i < recipients.length; i++) {
String rcpt = StringUtils.parseName(recipients[i]);
PGPPublicKeyRing ring = getPublicKey(context, rcpt);
if (ring == null)
throw new IllegalArgumentException("public key not found for user " + rcpt);
keys[i] = PGP.getEncryptionKey(ring);
if (keys[i] == null)
throw new IllegalArgumentException("public key not found for user " + rcpt);
}
return new PGPCoder(server, key, keys);
}
示例11: getPublicKey
import org.spongycastle.openpgp.PGPPublicKeyRing; //导入依赖的package包/类
/** Retrieves the public key for a user. */
public static PGPPublicKeyRing getPublicKey(Context context, String userId) {
byte[] keydata = null;
ContentResolver res = context.getContentResolver();
Cursor c = res.query(Users.CONTENT_URI,
new String[] { Users.PUBLIC_KEY },
Users.HASH + "=?",
new String[] { userId },
null);
if (c.moveToFirst())
keydata = c.getBlob(0);
c.close();
try {
return PGP.readPublicKeyring(keydata);
}
catch (Exception e) {
// ignored
}
return null;
}
示例12: extractPublicKeyFromBytes
import org.spongycastle.openpgp.PGPPublicKeyRing; //导入依赖的package包/类
@SuppressWarnings("unchecked")
public static PGPPublicKey extractPublicKeyFromBytes(byte[] keyBlock) throws IOException, PGPException {
PGPPublicKeyRingCollection keyringCol = new PGPPublicKeyRingCollection(PGPUtil.getDecoderStream(new ByteArrayInputStream(Base64.decode(keyBlock, Base64.DEFAULT))));
PGPPublicKey key = null;
Iterator<PGPPublicKeyRing> rIt = keyringCol.getKeyRings();
while(key == null && rIt.hasNext()) {
PGPPublicKeyRing keyring = (PGPPublicKeyRing) rIt.next();
Iterator<PGPPublicKey> kIt = keyring.getPublicKeys();
while(key == null && kIt.hasNext()) {
PGPPublicKey k = (PGPPublicKey) kIt.next();
if(k.isEncryptionKey())
key = k;
}
}
if(key == null) {
throw new IllegalArgumentException("there isn't an encryption key here.");
}
return key;
}
示例13: doInBackground
import org.spongycastle.openpgp.PGPPublicKeyRing; //导入依赖的package包/类
@Override
protected byte[] doInBackground(Void... strings) {
String email = SharedData.USERNAME;
char[] password = mKeyPassword.toCharArray();
try {
Log.d(TAG,"start generating keys");
PGPKeyRingGenerator keyRingGenerator = new KeyManagement().generateKey(email,password);
PGPPublicKeyRing publicKeys = keyRingGenerator.generatePublicKeyRing();
PGPSecretKeyRing secretKeys = keyRingGenerator.generateSecretKeyRing();
//output keys in ascii armored format
File file = new File(getFilesDir(),"pub.asc");
ArmoredOutputStream pubOut = new ArmoredOutputStream(new FileOutputStream(file));
publicKeys.encode(pubOut);
pubOut.close();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ArmoredOutputStream secOut = new ArmoredOutputStream(outputStream);
secretKeys.encode(secOut);
secOut.close();
DatabaseHandler db=new DatabaseHandler(OptionActivity.this,SharedData.DB_PASSWORD,true);
byte[] test=outputStream.toByteArray();
//call the db methods to store
db.insertSecKey(email,test);
SharedPreferences prefs=getSharedPreferences("done", Context.MODE_PRIVATE);
SharedPreferences.Editor editor=prefs.edit();
editor.putBoolean("keys_gen",true);
editor.apply();
editor.commit();
Log.d(TAG,"secret key written to file");
return test;
} catch (Exception e) {
Log.d(TAG,"Error generating keys");
e.printStackTrace();
return null;
}
}
示例14: readPublicKey
import org.spongycastle.openpgp.PGPPublicKeyRing; //导入依赖的package包/类
/**
* A simple routine that opens a key ring file and loads the first available key
* suitable for encryption.
*
* @param input data stream containing the public key data
* @return the first public key found.
* @throws IOException
* @throws PGPException
*/
static PGPPublicKey readPublicKey(InputStream input) throws IOException, PGPException
{
PGPPublicKeyRingCollection pgpPub = new PGPPublicKeyRingCollection(
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 = 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: getPublicKey
import org.spongycastle.openpgp.PGPPublicKeyRing; //导入依赖的package包/类
@Override
public PGPPublicKey getPublicKey(File file) throws Exception {
InputStream input=new FileInputStream(file);
JcaPGPPublicKeyRingCollection pgpPub = new JcaPGPPublicKeyRingCollection(PGPUtil.getDecoderStream(input));
PGPPublicKey pubKey = null;
@SuppressWarnings("unchecked")
Iterator<PGPPublicKeyRing> keyRingIterator = pgpPub.getKeyRings();
while (keyRingIterator.hasNext() && pubKey == null) {
PGPPublicKeyRing keyRing = keyRingIterator.next();
@SuppressWarnings("unchecked")
Iterator<PGPPublicKey> keyIterator = keyRing.getPublicKeys();
while (keyIterator.hasNext()) {
PGPPublicKey key = keyIterator.next();
if (key.isEncryptionKey()) {
pubKey = key;
break;
}
}
}
if(pubKey != null) {
return pubKey;
}
else {
throw new IllegalArgumentException("Can't find encryption key in key ring.");
}
}