本文整理汇总了Java中org.bouncycastle.openpgp.PGPPublicKeyRingCollection.getPublicKey方法的典型用法代码示例。如果您正苦于以下问题:Java PGPPublicKeyRingCollection.getPublicKey方法的具体用法?Java PGPPublicKeyRingCollection.getPublicKey怎么用?Java PGPPublicKeyRingCollection.getPublicKey使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.bouncycastle.openpgp.PGPPublicKeyRingCollection
的用法示例。
在下文中一共展示了PGPPublicKeyRingCollection.getPublicKey方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getKey
import org.bouncycastle.openpgp.PGPPublicKeyRingCollection; //导入方法依赖的package包/类
public PGPPublicKey getKey(long keyID) throws IOException, PGPException {
File keyFile = null;
PGPPublicKey key = null;
try {
String path = String.format("%02X/%02X/%016X.asc",
(byte) (keyID >> 56), (byte) (keyID >> 48 & 0xff), keyID);
keyFile = new File(cachePath, path);
if (!keyFile.exists()) {
receiveKey(keyFile, keyID);
}
InputStream keyIn = PGPUtil.getDecoderStream(new FileInputStream(keyFile));
PGPPublicKeyRingCollection pgpRing = new PGPPublicKeyRingCollection(keyIn, new BcKeyFingerprintCalculator());
key = pgpRing.getPublicKey(keyID);
} finally {
if (key == null) {
deleteFile(keyFile);
}
}
return key;
}
示例2: getKey
import org.bouncycastle.openpgp.PGPPublicKeyRingCollection; //导入方法依赖的package包/类
PGPPublicKey getKey(long keyID) throws IOException, PGPException {
File keyFile = null;
PGPPublicKey key = null;
try {
String path = String.format("%02X/%02X/%016X.asc",
(byte) (keyID >> 56), (byte) (keyID >> 48 & 0xff), keyID);
keyFile = new File(cachePath, path);
if (!keyFile.exists()) {
receiveKey(keyFile, keyID);
}
InputStream keyIn = PGPUtil.getDecoderStream(new FileInputStream(keyFile));
PGPPublicKeyRingCollection pgpRing = new PGPPublicKeyRingCollection(keyIn, new BcKeyFingerprintCalculator());
key = pgpRing.getPublicKey(keyID);
} finally {
if (key == null) {
deleteFile(keyFile);
}
}
return key;
}
示例3: findPublicKeyWithKeyId
import org.bouncycastle.openpgp.PGPPublicKeyRingCollection; //导入方法依赖的package包/类
private static PGPPublicKey findPublicKeyWithKeyId(InputStream input, long keyid) throws IOException, PGPException,
NoSuchProviderException {
PGPPublicKeyRingCollection pgpSec =
new PGPPublicKeyRingCollection(PGPUtil.getDecoderStream(input),
new BcKeyFingerprintCalculator());
return pgpSec.getPublicKey(keyid);
}
示例4: encrypt
import org.bouncycastle.openpgp.PGPPublicKeyRingCollection; //导入方法依赖的package包/类
public static final byte[] encrypt(File pubring, long keyid, byte[] in){
if(!pubring.exists() || !pubring.isFile()) return null;
try {
byte[] ret = null;
FileInputStream fis = new FileInputStream(pubring);
InputStream is = PGPUtil.getDecoderStream(fis);
PGPPublicKeyRingCollection ring = new PGPPublicKeyRingCollection(is);
PGPPublicKey pubkey = ring.getPublicKey(keyid);
if(pubkey.isMasterKey()) {
System.err.println("Tried to use a non-encryption key. This should never happen.");
return null;
}
PublicKey key = new JcaPGPKeyConverter().getPublicKey(pubkey);
Cipher cipher = Cipher.getInstance(key.getAlgorithm() + "/ECB/PKCS1Padding");
cipher.init(Cipher.ENCRYPT_MODE, key);
ret = cipher.doFinal(in);
is.close();
fis.close();
return ret;
} catch (Exception e) {
System.err.println(e.getLocalizedMessage());
return null;
}
}
示例5: validateLicense
import org.bouncycastle.openpgp.PGPPublicKeyRingCollection; //导入方法依赖的package包/类
/**
* Validate pgp signature of license
*
* @param licenseText base64 encoded pgp signed license
* @return The plain license in json (if validation is successful)
* @throws PGPException if validation fails
*/
public static String validateLicense(String licenseText) throws PGPException {
licenseText = licenseText.trim().replaceAll("\\r|\\n", "");
licenseText = licenseText.replace("---- SCHNIPP (Armored PGP signed JSON as base64) ----","");
licenseText = licenseText.replace("---- SCHNAPP ----","");
try {
final byte[] armoredPgp = BaseEncoding.base64().decode(licenseText);
final ArmoredInputStream in = new ArmoredInputStream(new ByteArrayInputStream(armoredPgp));
//
// read the input, making sure we ignore the last newline.
//
// https://github.com/bcgit/bc-java/blob/master/pg/src/test/java/org/bouncycastle/openpgp/test/PGPClearSignedSignatureTest.java
final ByteArrayOutputStream bout = new ByteArrayOutputStream();
int ch;
while ((ch = in.read()) >= 0 && in.isClearText()) {
bout.write((byte) ch);
}
final KeyFingerPrintCalculator c = new BcKeyFingerprintCalculator();
final PGPObjectFactory factory = new PGPObjectFactory(in, c);
final PGPSignatureList sigL = (PGPSignatureList) factory.nextObject();
final PGPPublicKeyRingCollection pgpRings = new PGPPublicKeyRingCollection(new ArmoredInputStream(
LicenseHelper.class.getResourceAsStream("/KEYS")), c);
if (sigL == null || pgpRings == null || sigL.size() == 0 || pgpRings.size() == 0) {
throw new PGPException("Cannot find license signature");
}
final PGPSignature sig = sigL.get(0);
final PGPPublicKey publicKey = pgpRings.getPublicKey(sig.getKeyID());
if (publicKey == null || sig == null) {
throw new PGPException("license signature key mismatch");
}
sig.init(new BcPGPContentVerifierBuilderProvider(), publicKey);
final ByteArrayOutputStream lineOut = new ByteArrayOutputStream();
final InputStream sigIn = new ByteArrayInputStream(bout.toByteArray());
int lookAhead = readInputLine(lineOut, sigIn);
processLine(sig, lineOut.toByteArray());
if (lookAhead != -1) {
do {
lookAhead = readInputLine(lineOut, lookAhead, sigIn);
sig.update((byte) '\r');
sig.update((byte) '\n');
processLine(sig, lineOut.toByteArray());
} while (lookAhead != -1);
}
if (!sig.verify()) {
throw new PGPException("Invalid license signature");
}
return bout.toString();
} catch (final Exception e) {
throw new PGPException(e.toString(), e);
}
}
示例6: getPublicKey
import org.bouncycastle.openpgp.PGPPublicKeyRingCollection; //导入方法依赖的package包/类
/**
* Get a public key from a public key block.
*
* @param block the public key block
* @param keyID the key id
* @return the public key
* @throws Exception anything that would prevent to obtain the key
*/
public static PGPPublicKey getPublicKey(byte[] block, long keyID) throws Exception {
InputStream keyIn = PGPUtil.getDecoderStream(new ByteArrayInputStream(block));
PGPPublicKeyRingCollection pgpRing = new PGPPublicKeyRingCollection(keyIn, new BcKeyFingerprintCalculator());
return pgpRing.getPublicKey(keyID);
}