本文整理汇总了Java中org.spongycastle.openpgp.PGPPublicKey类的典型用法代码示例。如果您正苦于以下问题:Java PGPPublicKey类的具体用法?Java PGPPublicKey怎么用?Java PGPPublicKey使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
PGPPublicKey类属于org.spongycastle.openpgp包,在下文中一共展示了PGPPublicKey类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: fillKeyDetails
import org.spongycastle.openpgp.PGPPublicKey; //导入依赖的package包/类
private void fillKeyDetails() {
if(!SharedData.KEYS_GENERATED){
Snackbar.make(findViewById(R.id.key_details_keyid_textview),"Keys not generated.",Snackbar.LENGTH_SHORT).show();
return;
}
try {
PGPPublicKey key=MyPGPUtil.readPublicKey(getFilesDir()+"/"+"pub.asc");
((TextView)findViewById(R.id.key_details_keyid_textview)).setText(""+key.getKeyID());
((TextView)findViewById(R.id.key_details_keysize_textview)).setText(""+key.getBitStrength());
if(key.getValidSeconds()==0){
((TextView)findViewById(R.id.key_details_keyalgo_textview)).setText("Key does not expire");
}else{
((TextView)findViewById(R.id.key_details_keyalgo_textview)).setText(""+key.getValidSeconds());
}
((TextView)findViewById(R.id.key_details_keytime_textview)).setText(""+key.getCreationTime());
} catch (IOException | PGPException e) {
e.printStackTrace();
}
}
示例2: generateKeyRingGenerator
import org.spongycastle.openpgp.PGPPublicKey; //导入依赖的package包/类
public final static PGPKeyRingGenerator generateKeyRingGenerator (String keyId, char[] pass) throws PGPException{
RSAKeyPairGenerator kpg = new RSAKeyPairGenerator();
kpg.init(new RSAKeyGenerationParameters(BigInteger.valueOf(0x10001), new SecureRandom(), 4096, 12));
PGPKeyPair rsakp_sign = new BcPGPKeyPair(PGPPublicKey.RSA_SIGN, kpg.generateKeyPair(), new Date());
PGPKeyPair rsakp_enc = new BcPGPKeyPair(PGPPublicKey.RSA_ENCRYPT, kpg.generateKeyPair(), new Date());
PGPSignatureSubpacketGenerator signhashgen = new PGPSignatureSubpacketGenerator();
signhashgen.setKeyFlags(false, KeyFlags.SIGN_DATA|KeyFlags.CERTIFY_OTHER|KeyFlags.SHARED);
signhashgen.setPreferredSymmetricAlgorithms(false, new int[]{SymmetricKeyAlgorithmTags.AES_256, SymmetricKeyAlgorithmTags.AES_192, SymmetricKeyAlgorithmTags.AES_128});
signhashgen.setPreferredHashAlgorithms(false, new int[]{HashAlgorithmTags.SHA256, HashAlgorithmTags.SHA1, HashAlgorithmTags.SHA384, HashAlgorithmTags.SHA512, HashAlgorithmTags.SHA224});
signhashgen.setFeature(false, Features.FEATURE_MODIFICATION_DETECTION);
PGPSignatureSubpacketGenerator enchashgen = new PGPSignatureSubpacketGenerator();
enchashgen.setKeyFlags(false, KeyFlags.ENCRYPT_COMMS | KeyFlags.ENCRYPT_STORAGE);
PGPDigestCalculator sha1Calc = new BcPGPDigestCalculatorProvider().get(HashAlgorithmTags.SHA1);
PGPDigestCalculator sha256Calc = new BcPGPDigestCalculatorProvider().get(HashAlgorithmTags.SHA256);
PBESecretKeyEncryptor pske = (new BcPBESecretKeyEncryptorBuilder(PGPEncryptedData.AES_256, sha256Calc, 0xc0)).build(pass);
PGPKeyRingGenerator keyRingGen = new PGPKeyRingGenerator (PGPSignature.POSITIVE_CERTIFICATION, rsakp_sign,
keyId, sha1Calc, signhashgen.generate(), null, new BcPGPContentSignerBuilder(rsakp_sign.getPublicKey().getAlgorithm(),
HashAlgorithmTags.SHA1), pske);
keyRingGen.addSubKey(rsakp_enc, enchashgen.generate(), null);
return keyRingGen;
}
示例3: PGPPubkeyEncryptionUtil
import org.spongycastle.openpgp.PGPPublicKey; //导入依赖的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());
}
}
示例4: readPublicKeyFromCol
import org.spongycastle.openpgp.PGPPublicKey; //导入依赖的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;
}
示例5: writeKeyPairToParcel
import org.spongycastle.openpgp.PGPPublicKey; //导入依赖的package包/类
private static void writeKeyPairToParcel(PGPKeyPair keypair, Parcel dest) throws IOException {
// write private key
PGPPrivateKey priv = keypair.getPrivateKey();
// key ID
dest.writeLong(priv.getKeyID());
// private key data packet
byte[] privData = priv.getPrivateKeyDataPacket().getEncoded();
dest.writeInt(privData.length);
dest.writeByteArray(privData);
// public key packet
byte[] privPubKey = priv.getPublicKeyPacket().getEncoded();
dest.writeInt(privPubKey.length);
dest.writeByteArray(privPubKey);
// write public key
PGPPublicKey pub = keypair.getPublicKey();
// whole public key
byte[] pubPacket = pub.getEncoded();
dest.writeInt(pubPacket.length);
dest.writeByteArray(pubPacket);
}
示例6: getEncryptionKey
import org.spongycastle.openpgp.PGPPublicKey; //导入依赖的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);
}
示例7: setKey
import org.spongycastle.openpgp.PGPPublicKey; //导入依赖的package包/类
/** Adds/updates a public key. */
public static void setKey(Context context, String jid, byte[] keydata, int trustLevel)
throws IOException, PGPException {
PGPPublicKey pk = PGP.getMasterKey(keydata);
String fingerprint = PGP.getFingerprint(pk);
Date date = pk.getCreationTime();
int autoTrustedLevel = getAutoTrustedLevel(context, jid);
ContentValues values = new ContentValues(3);
values.put(MyUsers.Keys.PUBLIC_KEY, keydata);
values.put(MyUsers.Keys.TIMESTAMP, date.getTime());
if (trustLevel >= 0) {
values.put(MyUsers.Keys.TRUST_LEVEL, trustLevel);
}
else if (autoTrustedLevel >= 0) {
values.put(MyUsers.Keys.TRUST_LEVEL, autoTrustedLevel);
}
context.getContentResolver().insert(MyUsers.Keys.getUri(jid, fingerprint), values);
if (autoTrustedLevel >= 0) {
// delete the autotrust entry
context.getContentResolver().delete(MyUsers.Keys.getUri(jid, VALUE_AUTOTRUST), null, null);
}
}
示例8: fromParcel
import org.spongycastle.openpgp.PGPPublicKey; //导入依赖的package包/类
public static PGPDecryptedKeyPairRing fromParcel(Parcel in) throws PGPException {
ensureKeyConverter();
// TODO read byte data
PrivateKey privSign = (PrivateKey) in.readSerializable();
PublicKey pubSign = (PublicKey) in.readSerializable();
int algoSign = in.readInt();
Date dateSign = new Date(in.readLong());
PGPPublicKey pubKeySign = sKeyConverter.getPGPPublicKey(algoSign, pubSign, dateSign);
PGPPrivateKey privKeySign = sKeyConverter.getPGPPrivateKey(pubKeySign, privSign);
PGPKeyPair signKp = new PGPKeyPair(pubKeySign, privKeySign);
PrivateKey privEnc = (PrivateKey) in.readSerializable();
PublicKey pubEnc = (PublicKey) in.readSerializable();
int algoEnc = in.readInt();
Date dateEnc = new Date(in.readLong());
PGPPublicKey pubKeyEnc = sKeyConverter.getPGPPublicKey(algoEnc, pubEnc, dateEnc);
PGPPrivateKey privKeyEnc = sKeyConverter.getPGPPrivateKey(pubKeyEnc, privEnc);
PGPKeyPair encryptKp = new PGPKeyPair(pubKeyEnc, privKeyEnc);
return new PGPDecryptedKeyPairRing(signKp, encryptKp);
}
示例9: signPublicKey
import org.spongycastle.openpgp.PGPPublicKey; //导入依赖的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.");
}
示例10: getEncryptCoder
import org.spongycastle.openpgp.PGPPublicKey; //导入依赖的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: extractPublicKeyFromBytes
import org.spongycastle.openpgp.PGPPublicKey; //导入依赖的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;
}
示例12: doInBackground
import org.spongycastle.openpgp.PGPPublicKey; //导入依赖的package包/类
@Override
protected Boolean doInBackground(Void... voids) {
try {
PGPSecretKey key=MyPGPUtil.readSecretKey(mSecretKeyFilename);
// if secret key is correct get the public key from it and save it
publishProgress("Getting public key");
PGPPublicKey pub=key.getPublicKey();
//output keys in ascii armored format
File file = new File(getFilesDir(),"pub.asc");
ArmoredOutputStream pubOut = new ArmoredOutputStream(new FileOutputStream(file));
pub.encode(pubOut);
pubOut.close();
publishProgress("Setting up database");
DatabaseHandler db=new DatabaseHandler(KeySelectActivity.this,SharedData.DB_PASSWORD,true);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ArmoredOutputStream secOut = new ArmoredOutputStream(outputStream);
key.encode(secOut);
secOut.close();
byte[] test=outputStream.toByteArray();
//call the db methods to store
db.insertSecKey(SharedData.USERNAME,test);
//save shared preferences
SharedPreferences prefs=getSharedPreferences("done", Context.MODE_PRIVATE);
SharedPreferences.Editor editor=prefs.edit();
editor.putBoolean("keys_gen",true);
editor.apply();
editor.commit();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
示例13: readPublicKey
import org.spongycastle.openpgp.PGPPublicKey; //导入依赖的package包/类
public static PGPPublicKey readPublicKey(String fileName) throws IOException, PGPException
{
InputStream keyIn = new BufferedInputStream(new FileInputStream(fileName));
PGPPublicKey pubKey = readPublicKey(keyIn);
keyIn.close();
return pubKey;
}
示例14: encryptFile
import org.spongycastle.openpgp.PGPPublicKey; //导入依赖的package包/类
public static boolean encryptFile(InputStream in, OutputStream out, File pubKeyFile,
boolean integrityCheck, Date fileDate,String filename){
boolean status;
try{
PGPPublicKey encKey=MyPGPUtil.readPublicKey(new FileInputStream(pubKeyFile));
Security.addProvider(new BouncyCastleProvider());
PGPEncryptedDataGenerator cPk =
new PGPEncryptedDataGenerator(
new JcePGPDataEncryptorBuilder(PGPEncryptedData.CAST5).
setWithIntegrityPacket(integrityCheck).
setSecureRandom(
new SecureRandom()
)
);
cPk.addMethod(new JcePublicKeyKeyEncryptionMethodGenerator(encKey));
OutputStream cOut = cPk.open(out, new byte[1 << 16]);
PGPCompressedDataGenerator comData = new PGPCompressedDataGenerator(
PGPCompressedData.UNCOMPRESSED);
PGPLiteralDataGenerator lData = new PGPLiteralDataGenerator();
OutputStream pOut = lData.open(comData.open(cOut), PGPLiteralData.BINARY,filename ,fileDate, new byte[1 << 16]);
PGPUtil.pipeDocumentFileContents(in,pOut, new byte[1 << 16].length);
comData.close();
cOut.close();
Log.d(TAG,"file successfully encrypted");
status=true;
}catch (Exception ex){
Log.d(TAG, "encryptFile: error in encrypting document file");
ex.printStackTrace();
status=false;
}
return status;
}
示例15: getPublicKey
import org.spongycastle.openpgp.PGPPublicKey; //导入依赖的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.");
}
}