本文整理汇总了Java中org.bouncycastle.openpgp.PGPObjectFactory类的典型用法代码示例。如果您正苦于以下问题:Java PGPObjectFactory类的具体用法?Java PGPObjectFactory怎么用?Java PGPObjectFactory使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
PGPObjectFactory类属于org.bouncycastle.openpgp包,在下文中一共展示了PGPObjectFactory类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getEncryptedObjects
import org.bouncycastle.openpgp.PGPObjectFactory; //导入依赖的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 );
}
}
示例2: getPublicKey
import org.bouncycastle.openpgp.PGPObjectFactory; //导入依赖的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");
}
示例3: openDecryptor
import org.bouncycastle.openpgp.PGPObjectFactory; //导入依赖的package包/类
/**
* Opens a new {@link Decryptor} (Reading Step 1/3)
*
* <p>This is the first step in opening a ghostryde file. After this method, you'll want to
* call {@link #openDecompressor(Decryptor)}.
*
* @param input is an {@link InputStream} of the ghostryde file data.
* @param privateKey is the private encryption key of the recipient (which is us!)
* @throws IOException
* @throws PGPException
*/
@CheckReturnValue
public Decryptor openDecryptor(@WillNotClose InputStream input, PGPPrivateKey privateKey)
throws IOException, PGPException {
checkNotNull(privateKey, "privateKey");
PGPObjectFactory fact = new BcPGPObjectFactory(checkNotNull(input, "input"));
PGPEncryptedDataList crypts = pgpCast(fact.nextObject(), PGPEncryptedDataList.class);
checkState(crypts.size() > 0);
if (crypts.size() > 1) {
logger.warningfmt("crypts.size() is %d (should be 1)", crypts.size());
}
PGPPublicKeyEncryptedData crypt = pgpCast(crypts.get(0), PGPPublicKeyEncryptedData.class);
if (crypt.getKeyID() != privateKey.getKeyID()) {
throw new PGPException(String.format(
"Message was encrypted for keyid %x but ours is %x",
crypt.getKeyID(), privateKey.getKeyID()));
}
return new Decryptor(
crypt.getDataStream(new BcPublicKeyDataDecryptorFactory(privateKey)),
crypt);
}
示例4: testCompressDecompress
import org.bouncycastle.openpgp.PGPObjectFactory; //导入依赖的package包/类
@Test
public void testCompressDecompress() throws Exception {
// Compress the data and write out a compressed data OpenPGP message.
byte[] data;
try (ByteArrayOutputStream output = new ByteArrayOutputStream()) {
PGPCompressedDataGenerator kompressor = new PGPCompressedDataGenerator(ZIP);
try (OutputStream output2 = kompressor.open(output)) {
output2.write(FALL_OF_HYPERION_A_DREAM.getBytes(UTF_8));
}
data = output.toByteArray();
}
logger.info("Compressed data: " + dumpHex(data));
// Decompress the data.
try (ByteArrayInputStream input = new ByteArrayInputStream(data)) {
PGPObjectFactory pgpFact = new BcPGPObjectFactory(input);
PGPCompressedData object = (PGPCompressedData) pgpFact.nextObject();
InputStream original = object.getDataStream(); // Closing this would close input.
assertThat(CharStreams.toString(new InputStreamReader(original, UTF_8)))
.isEqualTo(FALL_OF_HYPERION_A_DREAM);
assertThat(pgpFact.nextObject()).isNull();
}
}
示例5: verifySignature
import org.bouncycastle.openpgp.PGPObjectFactory; //导入依赖的package包/类
/**
* Verify a PGP signature.
*
* @param file the file
* @param signature the signature
* @param key the public key
* @return true if the signature is verified
* @throws Exception anything preventing the verification to happen
*/
public static boolean verifySignature(
InputStream file,
InputStream signature,
PGPPublicKey key)
throws Exception {
InputStream sigInputStream = PGPUtil.getDecoderStream(signature);
PGPObjectFactory pgpObjectFactory = new PGPObjectFactory(sigInputStream, new BcKeyFingerprintCalculator());
PGPSignatureList sigList = (PGPSignatureList) pgpObjectFactory.nextObject();
PGPSignature pgpSignature = sigList.get(0);
pgpSignature.init(new BcPGPContentVerifierBuilderProvider(), key);
try (InputStream inArtifact = new BufferedInputStream(file)) {
int t;
while ((t = inArtifact.read()) >= 0) {
pgpSignature.update((byte) t);
}
}
return pgpSignature.verify();
}
示例6: OpenPGPSecretKey
import org.bouncycastle.openpgp.PGPObjectFactory; //导入依赖的package包/类
public OpenPGPSecretKey(String keyId, InputStream secretKeyRing, char[] password) throws IOException {
PGPObjectFactory pgpObjectFactory = new BcPGPObjectFactory(PGPUtil.getDecoderStream(secretKeyRing));
for (Object it = pgpObjectFactory.nextObject(); it != null; it = pgpObjectFactory.nextObject()) {
PGPSecretKeyRing pgpSecretKeyRing = (PGPSecretKeyRing) it;
PGPSecretKey pgpSecretKey = pgpSecretKeyRing.getSecretKey();
if (keyId == null || keyId.isEmpty() || Long.valueOf(keyId, 16) == (pgpSecretKey.getKeyID() & MASK)) {
this.secretKey = pgpSecretKey;
break;
}
}
// sanity check
if (secretKey == null) {
throw new IllegalArgumentException("Secret key " + keyId + " not found");
}
this.password = password;
}
示例7: validateData
import org.bouncycastle.openpgp.PGPObjectFactory; //导入依赖的package包/类
private void validateData(byte[] data)
throws IOException, PGPException
{
PGPObjectFactory pgpFact = new PGPObjectFactory(data);
PGPCompressedData c1 = (PGPCompressedData)pgpFact.nextObject();
InputStream pIn = c1.getDataStream();
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
int ch;
while ((ch = pIn.read()) >= 0)
{
bOut.write(ch);
}
if (!areEqual(bOut.toByteArray(), "hello world! !dlrow olleh".getBytes()))
{
fail("compression test failed");
}
}
示例8: readSignatureFile
import org.bouncycastle.openpgp.PGPObjectFactory; //导入依赖的package包/类
private PGPSignatureList readSignatureFile(final File signatureFile) throws PGPVerifyException {
AssertUtil.assertNotNull(signatureFile, "signatureFile");
if (!signatureFile.isFile() || !signatureFile.canRead())
throw new PGPVerifyException("The signature-file does not exist or is not readable: " + signatureFile.getAbsolutePath());
try {
final InputStream in = new BufferedInputStream(castStream(signatureFile.createInputStream()));
try {
final PGPObjectFactory objectFactory = new PGPObjectFactory(
PGPUtil.getDecoderStream(in), new BcKeyFingerprintCalculator());
final PGPSignatureList sl = (PGPSignatureList) objectFactory.nextObject();
return sl;
} finally {
in.close();
}
} catch (final Exception e) {
throw new PGPVerifyException(signatureFile.getAbsolutePath() + ": " + e, e);
}
}
示例9: importKeys
import org.bouncycastle.openpgp.PGPObjectFactory; //导入依赖的package包/类
@Override
public synchronized ImportKeysResult importKeys(IInputStream _in) {
assertNotNull(_in, "in");
InputStream in = castStream(_in);
final ImportKeysResult importKeysResult = new ImportKeysResult();
boolean modified = false;
try {
in = PGPUtil.getDecoderStream(in);
final PGPObjectFactory pgpF = new PGPObjectFactory(in, new BcKeyFingerprintCalculator());
Object o;
while ((o = pgpF.nextObject()) != null) {
if (o instanceof PGPPublicKeyRing)
modified |= importPublicKeyRing(importKeysResult, (PGPPublicKeyRing) o);
else if (o instanceof PGPSecretKeyRing)
modified |= importSecretKeyRing(importKeysResult, (PGPSecretKeyRing) o);
else
throw new IllegalStateException("Unexpected object in InputStream (only PGPPublicKeyRing and PGPSecretKeyRing are supported): " + o);
}
} catch (IOException | PGPException x) {
throw new RuntimeException(x);
}
if (modified) // make sure the localRevision is incremented, even if the timestamp does not change (e.g. because the time resolution of the file system is too low).
incLocalRevision();
return importKeysResult;
}
示例10: asLiteral
import org.bouncycastle.openpgp.PGPObjectFactory; //导入依赖的package包/类
/**
* ***********************************************
*/
private static PGPLiteralData asLiteral( final InputStream clear ) throws IOException, PGPException
{
final PGPObjectFactory plainFact = new PGPObjectFactory( clear, new JcaKeyFingerprintCalculator() );
final Object message = plainFact.nextObject();
if ( message instanceof PGPCompressedData )
{
final PGPCompressedData cData = ( PGPCompressedData ) message;
final PGPObjectFactory pgpFact =
new PGPObjectFactory( cData.getDataStream(), new JcaKeyFingerprintCalculator() );
// Find the first PGPLiteralData object
Object object = null;
for ( int safety = 0; ( safety++ < 1000 ) && !( object instanceof PGPLiteralData );
object = pgpFact.nextObject() )
{
//ignore
}
return ( PGPLiteralData ) object;
}
else if ( message instanceof PGPLiteralData )
{
return ( PGPLiteralData ) message;
}
else if ( message instanceof PGPOnePassSignatureList )
{
throw new PGPException( "encrypted message contains a signed message - not literal data." );
}
else
{
throw new PGPException(
"message is not a simple encrypted file - type unknown: " + message.getClass().getName() );
}
}
示例11: pgpExtractSignature
import org.bouncycastle.openpgp.PGPObjectFactory; //导入依赖的package包/类
/**
* Extracts a {@link PGPSignature} object from a blob of {@code .sig} data.
*
* @throws SignatureException if a signature object couldn't be extracted for any reason.
*/
private static PGPSignature pgpExtractSignature(@Tainted byte[] signature)
throws SignatureException {
try {
ByteArrayInputStream input = new ByteArrayInputStream(signature);
PGPObjectFactory decoder = new BcPGPObjectFactory(PGPUtil.getDecoderStream(input));
Object object = decoder.nextObject();
if (object == null) {
throw new SignatureException(String.format(
"No OpenPGP packets found in signature.\n%s",
dumpHex(signature)));
}
if (!(object instanceof PGPSignatureList)) {
throw new SignatureException(String.format(
"Expected PGPSignatureList packet but got %s\n%s",
object.getClass().getSimpleName(),
dumpHex(signature)));
}
PGPSignatureList sigs = (PGPSignatureList) object;
if (sigs.isEmpty()) {
throw new SignatureException(String.format(
"PGPSignatureList doesn't have a PGPSignature.\n%s",
dumpHex(signature)));
}
return sigs.get(0);
} catch (IOException e) {
throw new SignatureException(String.format(
"Failed to extract PGPSignature object from .sig blob.\n%s",
dumpHex(signature)), e);
}
}
示例12: testSignVerify_Detached
import org.bouncycastle.openpgp.PGPObjectFactory; //导入依赖的package包/类
@Test
public void testSignVerify_Detached() throws Exception {
// Load the keys.
PGPPublicKeyRing publicKeyRing = new BcPGPPublicKeyRing(PUBLIC_KEY);
PGPSecretKeyRing privateKeyRing = new BcPGPSecretKeyRing(PRIVATE_KEY);
PGPPublicKey publicKey = publicKeyRing.getPublicKey();
PGPPrivateKey privateKey = extractPrivateKey(privateKeyRing.getSecretKey());
// Sign the data and write signature data to "signatureFile".
// Note: RSA_GENERAL will encrypt AND sign. RSA_SIGN and RSA_ENCRYPT are deprecated.
PGPSignatureGenerator signer = new PGPSignatureGenerator(
new BcPGPContentSignerBuilder(RSA_GENERAL, SHA256));
signer.init(PGPSignature.BINARY_DOCUMENT, privateKey);
addUserInfoToSignature(publicKey, signer);
signer.update(FALL_OF_HYPERION_A_DREAM.getBytes(UTF_8));
ByteArrayOutputStream output = new ByteArrayOutputStream();
signer.generate().encode(output);
byte[] signatureFileData = output.toByteArray();
logger.info(".sig file data: " + dumpHex(signatureFileData));
// Load algorithm information and signature data from "signatureFileData".
PGPSignature sig;
try (ByteArrayInputStream input = new ByteArrayInputStream(signatureFileData)) {
PGPObjectFactory pgpFact = new BcPGPObjectFactory(input);
PGPSignatureList sigList = (PGPSignatureList) pgpFact.nextObject();
assertThat(sigList.size()).isEqualTo(1);
sig = sigList.get(0);
}
// Use "onePass" and "sig" to verify "publicKey" signed the text.
sig.init(new BcPGPContentVerifierBuilderProvider(), publicKey);
sig.update(FALL_OF_HYPERION_A_DREAM.getBytes(UTF_8));
assertThat(sig.verify()).isTrue();
// Verify that they DIDN'T sign the text "hello monster".
sig.init(new BcPGPContentVerifierBuilderProvider(), publicKey);
sig.update("hello monster".getBytes(UTF_8));
assertThat(sig.verify()).isFalse();
}
示例13: testEncryptDecrypt_ExplicitStyle
import org.bouncycastle.openpgp.PGPObjectFactory; //导入依赖的package包/类
@Test
public void testEncryptDecrypt_ExplicitStyle() throws Exception {
int bufferSize = 64 * 1024;
// Alice loads Bob's "publicKey" into memory.
PGPPublicKeyRing publicKeyRing = new BcPGPPublicKeyRing(PUBLIC_KEY);
PGPPublicKey publicKey = publicKeyRing.getPublicKey();
// Alice encrypts the secret message for Bob using his "publicKey".
PGPEncryptedDataGenerator encryptor = new PGPEncryptedDataGenerator(
new BcPGPDataEncryptorBuilder(AES_128));
encryptor.addMethod(new BcPublicKeyKeyEncryptionMethodGenerator(publicKey));
byte[] encryptedData;
try (ByteArrayOutputStream output = new ByteArrayOutputStream()) {
try (OutputStream output2 = encryptor.open(output, new byte[bufferSize])) {
output2.write(FALL_OF_HYPERION_A_DREAM.getBytes(UTF_8));
}
encryptedData = output.toByteArray();
}
logger.info("Encrypted data: " + dumpHex(encryptedData));
// Bob loads his "privateKey" into memory.
PGPSecretKeyRing privateKeyRing = new BcPGPSecretKeyRing(PRIVATE_KEY);
PGPPrivateKey privateKey = extractPrivateKey(privateKeyRing.getSecretKey());
// Bob decrypt's the OpenPGP message (w/ ciphertext) using his "privateKey".
try (ByteArrayInputStream input = new ByteArrayInputStream(encryptedData)) {
PGPObjectFactory pgpFact = new BcPGPObjectFactory(input);
PGPEncryptedDataList encDataList = (PGPEncryptedDataList) pgpFact.nextObject();
assertThat(encDataList.size()).isEqualTo(1);
PGPPublicKeyEncryptedData encData = (PGPPublicKeyEncryptedData) encDataList.get(0);
assertThat(encData.getKeyID()).isEqualTo(publicKey.getKeyID());
assertThat(encData.getKeyID()).isEqualTo(privateKey.getKeyID());
try (InputStream original =
encData.getDataStream(new BcPublicKeyDataDecryptorFactory(privateKey))) {
assertThat(CharStreams.toString(new InputStreamReader(original, UTF_8)))
.isEqualTo(FALL_OF_HYPERION_A_DREAM);
}
}
}
示例14: verifySignature
import org.bouncycastle.openpgp.PGPObjectFactory; //导入依赖的package包/类
private void verifySignature(PGPObjectFactory pgpFactory, PGPOnePassSignature signature) throws IOException, PGPException, SignatureException {
if (signature != null) {
PGPSignatureList sigList = (PGPSignatureList) pgpFactory.nextObject();
if (!signature.verify(getSignatureWithKeyId(signature.getKeyID(), sigList))) {
throw new SignatureException("Verification of the PGP signature with the key ID " + signature.getKeyID() + " failed. The PGP message may have been tampered.");
}
}
}
示例15: readSignature
import org.bouncycastle.openpgp.PGPObjectFactory; //导入依赖的package包/类
private PGPSignature readSignature(PushCertificate cert) throws IOException {
ArmoredInputStream in =
new ArmoredInputStream(new ByteArrayInputStream(Constants.encode(cert.getSignature())));
PGPObjectFactory factory = new BcPGPObjectFactory(in);
Object obj;
while ((obj = factory.nextObject()) != null) {
if (obj instanceof PGPSignatureList) {
PGPSignatureList sigs = (PGPSignatureList) obj;
if (!sigs.isEmpty()) {
return sigs.get(0);
}
}
}
return null;
}