本文整理汇总了Java中org.spongycastle.openpgp.PGPException类的典型用法代码示例。如果您正苦于以下问题:Java PGPException类的具体用法?Java PGPException怎么用?Java PGPException使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
PGPException类属于org.spongycastle.openpgp包,在下文中一共展示了PGPException类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: findSecretKey
import org.spongycastle.openpgp.PGPException; //导入依赖的package包/类
public static PGPPrivateKey findSecretKey(PGPSecretKeyRingCollection pgpSec, long keyID, char[] pass)
throws PGPException, NoSuchProviderException
{
PGPSecretKey pgpSecKey = pgpSec.getSecretKey(keyID);
PGPPrivateKey x;
if (pgpSecKey == null)
{
return null;
}
try{
Log.d("decrypt", "findSecretKey: fixing something");
x =pgpSecKey.extractPrivateKey(new JcePBESecretKeyDecryptorBuilder().setProvider("BC").build(pass));
}catch (Exception ex){
ex.printStackTrace();
return null;
}
return x;
}
示例2: fillKeyDetails
import org.spongycastle.openpgp.PGPException; //导入依赖的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();
}
}
示例3: generateKeyRingGenerator
import org.spongycastle.openpgp.PGPException; //导入依赖的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;
}
示例4: verifySignature
import org.spongycastle.openpgp.PGPException; //导入依赖的package包/类
public static boolean verifySignature(
String fileName,
String inputFileName,
String keyFileName)
throws GeneralSecurityException, IOException, PGPException
{
InputStream in = new BufferedInputStream(new FileInputStream(inputFileName));
InputStream keyIn = new BufferedInputStream(new FileInputStream(keyFileName));
boolean result = verifySignature(fileName, in, keyIn);
keyIn.close();
in.close();
return result;
}
示例5: encrypt
import org.spongycastle.openpgp.PGPException; //导入依赖的package包/类
public String encrypt(String plainText) throws NoSuchAlgorithmException, IOException, PGPException {
byte[] rawText = plainText.getBytes();
//This needs, like, three metric fucktons of explanation and/or cleaning up
ByteArrayOutputStream encOut = new ByteArrayOutputStream();
OutputStream out = new ArmoredOutputStream(encOut);
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
PGPCompressedDataGenerator comData = new PGPCompressedDataGenerator(PGPCompressedData.ZIP);
OutputStream cos = comData.open(bOut);
PGPLiteralDataGenerator lData = new PGPLiteralDataGenerator();
OutputStream pOut = lData.open(cos, PGPLiteralData.BINARY, PGPLiteralData.CONSOLE, rawText.length, new Date());
pOut.write(rawText);
lData.close();
comData.close();
BcPGPDataEncryptorBuilder builder = new BcPGPDataEncryptorBuilder(PGPEncryptedData.AES_256);
builder.setWithIntegrityPacket(true);
builder.setSecureRandom(new SecureRandom());
PGPEncryptedDataGenerator cpk = new PGPEncryptedDataGenerator(builder);
cpk.addMethod(new BcPublicKeyKeyEncryptionMethodGenerator(publicKey));
byte[] bytes = bOut.toByteArray();
OutputStream cOut = cpk.open(out, bytes.length);
cOut.write(bytes);
cOut.close();
out.close();
return new String(encOut.toByteArray(), Charset.forName("UTF-8"));
}
示例6: readPublicKeyFromCol
import org.spongycastle.openpgp.PGPException; //导入依赖的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;
}
示例7: loadDefaultPersonalKey
import org.spongycastle.openpgp.PGPException; //导入依赖的package包/类
public static PersonalKey loadDefaultPersonalKey(Context ctx, String passphrase)
throws PGPException, IOException, CertificateException, NoSuchProviderException {
AccountManager m = AccountManager.get(ctx);
Account acc = getDefaultAccount(m);
String privKeyData = m.getUserData(acc, DATA_PRIVATEKEY);
String pubKeyData = m.getUserData(acc, DATA_PUBLICKEY);
String bridgeCertData = m.getUserData(acc, DATA_BRIDGECERT);
if (privKeyData != null && pubKeyData != null && bridgeCertData != null)
return PersonalKey
.load(Base64.decode(privKeyData, Base64.DEFAULT),
Base64.decode(pubKeyData, Base64.DEFAULT),
passphrase,
Base64.decode(bridgeCertData, Base64.DEFAULT)
);
else
return null;
}
示例8: exportDefaultPersonalKey
import org.spongycastle.openpgp.PGPException; //导入依赖的package包/类
public static void exportDefaultPersonalKey(Context ctx, OutputStream dest, String passphrase, String exportPassphrase, boolean bridgeCertificate)
throws CertificateException, NoSuchProviderException, PGPException,
IOException, KeyStoreException, NoSuchAlgorithmException {
AccountManager m = AccountManager.get(ctx);
Account acc = getDefaultAccount(m);
String privKeyData = m.getUserData(acc, DATA_PRIVATEKEY);
byte[] privateKey = Base64.decode(privKeyData, Base64.DEFAULT);
byte[] bridgeCert = null;
if (bridgeCertificate) {
// bridge certificate is just plain data
String bridgeCertData = m.getUserData(acc, DATA_BRIDGECERT);
bridgeCert = Base64.decode(bridgeCertData, Base64.DEFAULT);
}
String pubKeyData = m.getUserData(acc, DATA_PUBLICKEY);
byte[] publicKey = Base64.decode(pubKeyData, Base64.DEFAULT);
// trusted keys
Map<String, Keyring.TrustedFingerprint> trustedKeys = Keyring.getTrustedKeys(ctx);
PersonalKeyExporter exp = new PersonalKeyExporter();
exp.save(privateKey, publicKey, dest, passphrase, exportPassphrase, bridgeCert, trustedKeys, acc.name);
}
示例9: initConnection
import org.spongycastle.openpgp.PGPException; //导入依赖的package包/类
private void initConnection() throws XMPPException, SmackException,
PGPException, KeyStoreException, NoSuchProviderException,
NoSuchAlgorithmException, CertificateException,
IOException, InterruptedException {
if (!mConnector.isConnected() || mConnector.isServerDirty()) {
mConnector.setListener(this);
PersonalKey key = null;
if (mImportedPrivateKey != null && mImportedPublicKey != null) {
PGPKeyPairRing ring;
try {
ring = PGPKeyPairRing.loadArmored(mImportedPrivateKey, mImportedPublicKey);
}
catch (IOException e) {
// try not armored
ring = PGPKeyPairRing.load(mImportedPrivateKey, mImportedPublicKey);
}
key = PersonalKey.load(ring.secretKey, ring.publicKey, mPassphrase, mBridgeCert);
}
else if (mKey != null) {
key = mKey.copy(mBridgeCert);
}
mConnector.connectOnce(key, mStep == STEP_LOGIN_TEST);
}
}
示例10: convertPrivateKey
import org.spongycastle.openpgp.PGPException; //导入依赖的package包/类
@SuppressWarnings("unchecked")
public static PrivateKey convertPrivateKey(byte[] privateKeyData, String passphrase)
throws PGPException, IOException {
PGPDigestCalculatorProvider digestCalc = new JcaPGPDigestCalculatorProviderBuilder().build();
PBESecretKeyDecryptor decryptor = new JcePBESecretKeyDecryptorBuilder(digestCalc)
.setProvider(PGP.PROVIDER)
.build(passphrase.toCharArray());
// load the secret key ring
PGPSecretKeyRing secRing = new PGPSecretKeyRing(privateKeyData, sFingerprintCalculator);
// search and decrypt the master (signing key)
// secret keys
Iterator<PGPSecretKey> skeys = secRing.getSecretKeys();
while (skeys.hasNext()) {
PGPSecretKey key = skeys.next();
PGPSecretKey sec = secRing.getSecretKey();
if (key.isMasterKey())
return convertPrivateKey(sec.extractPrivateKey(decryptor));
}
throw new PGPException("no suitable private key found.");
}
示例11: createCertificate
import org.spongycastle.openpgp.PGPException; //导入依赖的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));
}
示例12: updateAccountManager
import org.spongycastle.openpgp.PGPException; //导入依赖的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));
}
}
示例13: updateAccountManager
import org.spongycastle.openpgp.PGPException; //导入依赖的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));
}
}
示例14: signPublicKey
import org.spongycastle.openpgp.PGPException; //导入依赖的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.");
}
示例15: setKey
import org.spongycastle.openpgp.PGPException; //导入依赖的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);
}
}