本文整理匯總了Java中org.bouncycastle.openpgp.PGPUtil類的典型用法代碼示例。如果您正苦於以下問題:Java PGPUtil類的具體用法?Java PGPUtil怎麽用?Java PGPUtil使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
PGPUtil類屬於org.bouncycastle.openpgp包,在下文中一共展示了PGPUtil類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: asLiteral
import org.bouncycastle.openpgp.PGPUtil; //導入依賴的package包/類
private static PGPLiteralData asLiteral( final byte[] message, final InputStream secretKeyRing,
final String secretPwd ) throws IOException, PGPException
{
PGPPrivateKey key = null;
PGPPublicKeyEncryptedData encrypted = null;
final PGPSecretKeyRingCollection keys =
new PGPSecretKeyRingCollection( PGPUtil.getDecoderStream( secretKeyRing ),
new JcaKeyFingerprintCalculator() );
for ( final Iterator<PGPPublicKeyEncryptedData> i = getEncryptedObjects( message );
( key == null ) && i.hasNext(); )
{
encrypted = i.next();
key = getPrivateKey( keys, encrypted.getKeyID(), secretPwd );
}
if ( key == null )
{
throw new IllegalArgumentException( "secret key for message not found." );
}
final InputStream stream = encrypted
.getDataStream( new JcePublicKeyDataDecryptorFactoryBuilder().setProvider( provider ).build( key ) );
return asLiteral( stream );
}
示例2: getEncryptedObjects
import org.bouncycastle.openpgp.PGPUtil; //導入依賴的package包/類
@SuppressWarnings( "unchecked" )
private static Iterator<PGPPublicKeyEncryptedData> getEncryptedObjects( final byte[] message ) throws IOException
{
try
{
final PGPObjectFactory factory =
new PGPObjectFactory( PGPUtil.getDecoderStream( new ByteArrayInputStream( message ) ),
new JcaKeyFingerprintCalculator() );
final Object first = factory.nextObject();
final Object list = ( first instanceof PGPEncryptedDataList ) ? first : factory.nextObject();
return ( ( PGPEncryptedDataList ) list ).getEncryptedDataObjects();
}
catch ( IOException e )
{
throw new IOException( e );
}
}
示例3: readSecretKey
import org.bouncycastle.openpgp.PGPUtil; //導入依賴的package包/類
static PGPSecretKey readSecretKey() throws Exception {
InputStream input = new ByteArrayInputStream(getSecKeyRing());
PGPSecretKeyRingCollection pgpSec = new PGPSecretKeyRingCollection(PGPUtil.getDecoderStream(input),
new BcKeyFingerprintCalculator());
@SuppressWarnings("rawtypes")
Iterator keyRingIter = pgpSec.getKeyRings();
while (keyRingIter.hasNext()) {
PGPSecretKeyRing keyRing = (PGPSecretKeyRing) keyRingIter.next();
@SuppressWarnings("rawtypes")
Iterator keyIter = keyRing.getSecretKeys();
while (keyIter.hasNext()) {
PGPSecretKey key = (PGPSecretKey) keyIter.next();
if (key.isSigningKey()) {
return key;
}
}
}
throw new IllegalArgumentException("Can't find signing key in key ring.");
}
示例4: readPublicKey
import org.bouncycastle.openpgp.PGPUtil; //導入依賴的package包/類
public static PGPPublicKey readPublicKey(InputStream in)
throws IOException, PGPException {
in = PGPUtil.getDecoderStream(in);
PGPPublicKeyRingCollection pgpPub = new PGPPublicKeyRingCollection(in, new JcaKeyFingerprintCalculator() );
Iterator rIt = pgpPub.getKeyRings();
while (rIt.hasNext()) {
PGPPublicKeyRing kRing = (PGPPublicKeyRing) rIt.next();
Iterator kIt = kRing.getPublicKeys();
while (kIt.hasNext()) {
PGPPublicKey k = (PGPPublicKey) kIt.next();
if (k.isEncryptionKey()) {
return k;
}
}
}
throw new IllegalArgumentException(
"Can't find encryption key in key ring.");
}
示例5: getSecretKey
import org.bouncycastle.openpgp.PGPUtil; //導入依賴的package包/類
static PGPSecretKey getSecretKey(String privateKeyData) throws IOException, PGPException {
PGPPrivateKey privKey = null;
try (InputStream privStream = new ArmoredInputStream(new ByteArrayInputStream(privateKeyData.getBytes("UTF-8")))) {
PGPSecretKeyRingCollection pgpSec = new PGPSecretKeyRingCollection(PGPUtil.getDecoderStream(privStream), new JcaKeyFingerprintCalculator());
Iterator keyRingIter = pgpSec.getKeyRings();
while (keyRingIter.hasNext()) {
PGPSecretKeyRing keyRing = (PGPSecretKeyRing)keyRingIter.next();
Iterator keyIter = keyRing.getSecretKeys();
while (keyIter.hasNext()) {
PGPSecretKey key = (PGPSecretKey)keyIter.next();
if (key.isSigningKey()) {
return key;
}
}
}
}
throw new IllegalArgumentException("Can't find signing key in key ring.");
}
示例6: verify
import org.bouncycastle.openpgp.PGPUtil; //導入依賴的package包/類
/**
* Verify a signature. Only return true if signature matches calculated signature of signedData
* and if it was signed by the publicKey
*
* @param signedData
* @param signature
* @return
*/
public boolean verify(InputStream signedData, InputStream signature) {
try {
signature = PGPUtil.getDecoderStream(signature);
JcaPGPObjectFactory pgpFact = new JcaPGPObjectFactory(signature);
PGPSignature sig = ((PGPSignatureList) pgpFact.nextObject()).get(0);
PGPPublicKey key = pgpPubRingCollection.getPublicKey(sig.getKeyID());
sig.init(new JcaPGPContentVerifierBuilderProvider().setProvider("BC"), key);
byte[] buff = new byte[1024];
int read = 0;
while ((read = signedData.read(buff)) != -1) {
sig.update(buff, 0, read);
}
signedData.close();
return sig.verify();
}
catch (Exception ex) {
// can we put a logger here please?
return false;
}
}
示例7: readPublicKey
import org.bouncycastle.openpgp.PGPUtil; //導入依賴的package包/類
public static PGPPublicKey readPublicKey( InputStream is ) throws IOException, PGPException
{
PGPPublicKeyRingCollection pgpPub =
new PGPPublicKeyRingCollection( PGPUtil.getDecoderStream( is ), new JcaKeyFingerprintCalculator() );
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." );
}
示例8: readSecretKey
import org.bouncycastle.openpgp.PGPUtil; //導入依賴的package包/類
private static PGPSecretKey readSecretKey( InputStream is ) throws IOException, PGPException
{
PGPSecretKeyRingCollection pgpSec =
new PGPSecretKeyRingCollection( PGPUtil.getDecoderStream( is ), new JcaKeyFingerprintCalculator() );
Iterator keyRingIter = pgpSec.getKeyRings();
while ( keyRingIter.hasNext() )
{
PGPSecretKeyRing keyRing = ( PGPSecretKeyRing ) keyRingIter.next();
Iterator keyIter = keyRing.getSecretKeys();
while ( keyIter.hasNext() )
{
PGPSecretKey key = ( PGPSecretKey ) keyIter.next();
if ( key.isSigningKey() )
{
return key;
}
}
}
throw new IllegalArgumentException( "Can't find signing key in key ring." );
}
示例9: getKey
import org.bouncycastle.openpgp.PGPUtil; //導入依賴的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;
}
示例10: getPublicKey
import org.bouncycastle.openpgp.PGPUtil; //導入依賴的package包/類
public PGPPublicKeyRing getPublicKey(final InputStream in) {
Object obj;
try {
final PGPObjectFactory pgpFact = new PGPObjectFactory(
PGPUtil.getDecoderStream(in));
obj = pgpFact.nextObject();
}
catch (final Exception e) {
throw new IllegalStateException(e);
}
if (obj instanceof PGPPublicKeyRing) {
final PGPPublicKeyRing keyRing = (PGPPublicKeyRing) obj;
rememberKey(keyRing);
return keyRing;
}
throw new IllegalStateException("Pblic key not available");
}
示例11: signExternal
import org.bouncycastle.openpgp.PGPUtil; //導入依賴的package包/類
public byte[] signExternal(String input) throws IOException, PGPException {
PGPSecretKey signKey = readSecretKey();
PGPPrivateKey privKey = signKey.extractPrivateKey(
new JcePBESecretKeyDecryptorBuilder().setProvider("BC").build(config.passphrase.toCharArray()));
PGPSignatureGenerator sigGenerator = new PGPSignatureGenerator(
new JcaPGPContentSignerBuilder(signKey.getPublicKey().getAlgorithm(), PGPUtil.SHA256).setProvider("BC"));
sigGenerator.init(PGPSignature.BINARY_DOCUMENT, privKey);
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
try (ArmoredOutputStream aOut = new ArmoredOutputStream(buffer)) {
BCPGOutputStream bOut = new BCPGOutputStream(aOut);
sigGenerator.update(input.getBytes(Charsets.UTF_8));
sigGenerator.generate().encode(bOut);
}
return buffer.toByteArray();
}
示例12: readSecretKey
import org.bouncycastle.openpgp.PGPUtil; //導入依賴的package包/類
private PGPSecretKey readSecretKey() throws IOException, PGPException {
PGPSecretKeyRingCollection pgpSec = new PGPSecretKeyRingCollection(
PGPUtil.getDecoderStream(new ByteArrayInputStream(config.keypair.getBytes())),
new JcaKeyFingerprintCalculator());
Iterator<PGPSecretKeyRing> keyRings = pgpSec.getKeyRings();
while (keyRings.hasNext()) {
PGPSecretKeyRing keyRing = (PGPSecretKeyRing) keyRings.next();
Iterator<PGPSecretKey> keys = keyRing.getSecretKeys();
while (keys.hasNext()) {
PGPSecretKey key = (PGPSecretKey) keys.next();
if (key.isSigningKey()) {
return key;
}
}
}
throw new IllegalStateException("Can't find signing key in key ring.");
}
示例13: readPublicKey
import org.bouncycastle.openpgp.PGPUtil; //導入依賴的package包/類
static PGPPublicKey readPublicKey(String keyringPath) throws Exception {
InputStream input = new ByteArrayInputStream(getKeyRing(keyringPath));
PGPPublicKeyRingCollection pgpPub = new PGPPublicKeyRingCollection(PGPUtil.getDecoderStream(input),
new BcKeyFingerprintCalculator());
@SuppressWarnings("rawtypes")
Iterator keyRingIter = pgpPub.getKeyRings();
while (keyRingIter.hasNext()) {
PGPPublicKeyRing keyRing = (PGPPublicKeyRing) keyRingIter.next();
@SuppressWarnings("rawtypes")
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.");
}
示例14: extractSecrectKeyRings
import org.bouncycastle.openpgp.PGPUtil; //導入依賴的package包/類
private static List<PGPSecretKeyRing> extractSecrectKeyRings(InputStream inputStream) {
InputStream decodedInput;
try {
decodedInput = PGPUtil.getDecoderStream(inputStream);
} catch (final IOException e) {
throw JkUtilsThrowable.unchecked(e);
}
final KeyFingerPrintCalculator fingerPrintCalculator = new JcaKeyFingerprintCalculator();
final InnerPGPObjectFactory pgpFact = new InnerPGPObjectFactory(decodedInput,
fingerPrintCalculator);
PGPSecretKeyRing secKeyRing;
final List<PGPSecretKeyRing> result = new LinkedList<>();
while ((secKeyRing = pgpFact.nextSecretKey()) != null) {
result.add(secKeyRing);
}
return result;
}
示例15: createKeyFrom
import org.bouncycastle.openpgp.PGPUtil; //導入依賴的package包/類
public PGPPublicKey createKeyFrom(InputStream in) throws IOException, PGPException {
InputStream pgpData = PGPUtil.getDecoderStream(in);
PGPPublicKeyRingCollection pgpPub = new PGPPublicKeyRingCollection(
PGPUtil.getDecoderStream(pgpData), new JcaKeyFingerprintCalculator());
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.");
}