当前位置: 首页>>代码示例>>Java>>正文


Java PGPPublicKeyEncryptedData类代码示例

本文整理汇总了Java中org.bouncycastle.openpgp.PGPPublicKeyEncryptedData的典型用法代码示例。如果您正苦于以下问题:Java PGPPublicKeyEncryptedData类的具体用法?Java PGPPublicKeyEncryptedData怎么用?Java PGPPublicKeyEncryptedData使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


PGPPublicKeyEncryptedData类属于org.bouncycastle.openpgp包,在下文中一共展示了PGPPublicKeyEncryptedData类的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: asLiteral

import org.bouncycastle.openpgp.PGPPublicKeyEncryptedData; //导入依赖的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 );
}
 
开发者ID:subutai-io,项目名称:base,代码行数:23,代码来源:PGPEncryptionUtil.java

示例2: getEncryptedObjects

import org.bouncycastle.openpgp.PGPPublicKeyEncryptedData; //导入依赖的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 );
    }
}
 
开发者ID:subutai-io,项目名称:base,代码行数:18,代码来源:PGPEncryptionUtil.java

示例3: decrypt

import org.bouncycastle.openpgp.PGPPublicKeyEncryptedData; //导入依赖的package包/类
public static byte[] decrypt( byte encData[], PGPPrivateKey privateKey ) throws PGPException, IOException
{
    PGPPublicKeyEncryptedData pgpEncData = getPGPEncryptedData( encData );

    InputStream is = getInputStream( privateKey, pgpEncData );

    // IMPORTANT: pipe() should be before verify(). Otherwise we get "java.io.EOFException: Unexpected end of ZIP
    // input stream".
    byte data[] = pipe( is );

    if ( !pgpEncData.verify() )
    {
        throw new PGPDataValidationException( "Data integrity check failed" );
    }

    return data;
}
 
开发者ID:subutai-io,项目名称:base,代码行数:18,代码来源:PGPDecrypt.java

示例4: openDecryptor

import org.bouncycastle.openpgp.PGPPublicKeyEncryptedData; //导入依赖的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);
}
 
开发者ID:google,项目名称:nomulus,代码行数:32,代码来源:Ghostryde.java

示例5: decrypt

import org.bouncycastle.openpgp.PGPPublicKeyEncryptedData; //导入依赖的package包/类
/**
 * Decrypts the encrypted data as the returned input stream.
 */
protected InputStream decrypt(PGPPublicKeyEncryptedData data, Subkey subkey)
throws IOException, PGPException {
    if (data == null || subkey == null)
        throw new DecryptionException("no suitable decryption key found");

    if (log.isLoggable(Level.INFO))
        log.info("using decryption key " + subkey);
    return data.getDataStream(buildPublicKeyDecryptor(subkey));
}
 
开发者ID:justinludwig,项目名称:jpgpj,代码行数:13,代码来源:Decryptor.java

示例6: testEncryptDecrypt_ExplicitStyle

import org.bouncycastle.openpgp.PGPPublicKeyEncryptedData; //导入依赖的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);
    }
  }
}
 
开发者ID:google,项目名称:nomulus,代码行数:41,代码来源:BouncyCastleTest.java

示例7: testDecrypt

import org.bouncycastle.openpgp.PGPPublicKeyEncryptedData; //导入依赖的package包/类
private void testDecrypt(PGPSecretKeyRing secretKeyRing)
        throws Exception
    {
        PGPObjectFactory pgpF = new PGPObjectFactory(testMessage);

        PGPEncryptedDataList encList = (PGPEncryptedDataList)pgpF.nextObject();

        PGPPublicKeyEncryptedData encP = (PGPPublicKeyEncryptedData)encList.get(0);

        PGPSecretKey secretKey = secretKeyRing.getSecretKey(); // secretKeyRing.getSecretKey(encP.getKeyID());
//
//        PGPPrivateKey pgpPrivKey = secretKey.extractPrivateKey()extractPrivateKey(null);
//
//        clear = encP.getDataStream(pgpPrivKey, "BC");
//
//        bOut.reset();
//
//        while ((ch = clear.read()) >= 0)
//        {
//            bOut.write(ch);
//        }
//
//        out = bOut.toByteArray();
//
//        if (!areEqual(out, text))
//        {
//            fail("wrong plain text in generated packet");
//        }
    }
 
开发者ID:NoYouShutup,项目名称:CryptMeme,代码行数:30,代码来源:PGPECDHTest.java

示例8: getInputStream

import org.bouncycastle.openpgp.PGPPublicKeyEncryptedData; //导入依赖的package包/类
private static InputStream getInputStream( PGPPrivateKey privateKey, PGPPublicKeyEncryptedData pgpEncData )
        throws PGPException, IOException
{
    InputStream is = pgpEncData
            .getDataStream( new JcePublicKeyDataDecryptorFactoryBuilder().setProvider( "BC" ).build( privateKey ) );

    JcaPGPObjectFactory objectFactory = new JcaPGPObjectFactory( is );

    Object message = objectFactory.nextObject();

    PGPCompressedData compressedData = ( PGPCompressedData ) message;

    JcaPGPObjectFactory pgpObjectFactory = new JcaPGPObjectFactory( compressedData.getDataStream() );

    PGPLiteralData literalData = ( PGPLiteralData ) pgpObjectFactory.nextObject();

    return literalData.getInputStream();
}
 
开发者ID:subutai-io,项目名称:base,代码行数:19,代码来源:PGPDecrypt.java

示例9: Decryptor

import org.bouncycastle.openpgp.PGPPublicKeyEncryptedData; //导入依赖的package包/类
Decryptor(@WillCloseWhenClosed InputStream input, PGPPublicKeyEncryptedData crypt) {
  super(input);
  this.crypt = checkNotNull(crypt, "crypt");
}
 
开发者ID:google,项目名称:nomulus,代码行数:5,代码来源:Ghostryde.java

示例10: testEncryptDecrypt_KeyRingStyle

import org.bouncycastle.openpgp.PGPPublicKeyEncryptedData; //导入依赖的package包/类
@Test
public void testEncryptDecrypt_KeyRingStyle() throws Exception {
  int bufferSize = 64 * 1024;

  // Alice loads Bob's "publicKey" into memory from her public key ring.
  PGPPublicKeyRingCollection publicKeyRings = new BcPGPPublicKeyRingCollection(
      PGPUtil.getDecoderStream(new ByteArrayInputStream(PUBLIC_KEY)));
  PGPPublicKeyRing publicKeyRing =
      publicKeyRings.getKeyRings("[email protected]", true, true).next();
  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 chain of private keys into memory.
  PGPSecretKeyRingCollection privateKeyRings = new BcPGPSecretKeyRingCollection(
      PGPUtil.getDecoderStream(new ByteArrayInputStream(PRIVATE_KEY)));

  // 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);
    // Bob loads the private key to which the message is addressed.
    PGPPrivateKey privateKey =
        extractPrivateKey(privateKeyRings.getSecretKey(encData.getKeyID()));
    try (InputStream original =
        encData.getDataStream(new BcPublicKeyDataDecryptorFactory(privateKey))) {
      assertThat(CharStreams.toString(new InputStreamReader(original, UTF_8)))
          .isEqualTo(FALL_OF_HYPERION_A_DREAM);
    }
  }
}
 
开发者ID:google,项目名称:nomulus,代码行数:45,代码来源:BouncyCastleTest.java

示例11: decryptAndVerify

import org.bouncycastle.openpgp.PGPPublicKeyEncryptedData; //导入依赖的package包/类
@Override
public String decryptAndVerify(String messageIn) throws IOException, SignatureVerificationException {
	
	try {
		/* Stage zero: Convert to ASCII armored format and open a decoding stream */
		InputStream is = new ByteArrayInputStream(Base64.decode(messageIn));
		InputStream decoderStream = PGPUtil.getDecoderStream(is);
		
		/* Stage one: Init a decrypting stream */
		PGPObjectFactory pgpFactory = new PGPObjectFactory(decoderStream);
		PGPEncryptedDataList cryptedDataList = (PGPEncryptedDataList) pgpFactory.nextObject();
		PGPPublicKeyEncryptedData cryptedData = (PGPPublicKeyEncryptedData) cryptedDataList.get(0);
		InputStream clearStream = cryptedData.getDataStream(getCryptingPrivateKey(), _provider);
		
		/* Stage two: Seperate the XML data from the signatures */
		PGPObjectFactory plainFact = new PGPObjectFactory(clearStream);

		PGPOnePassSignatureList onePassSignatureList = (PGPOnePassSignatureList) plainFact.nextObject();
		PGPLiteralData literalData = (PGPLiteralData) plainFact.nextObject();
		
		String xmlMessage = IOUtils.toString(literalData.getInputStream());
		PGPSignatureList signatureList = (PGPSignatureList) plainFact.nextObject();
		
		/* Stage three: Verify signature */
		PGPOnePassSignature ops = onePassSignatureList.get(0);
		PGPPublicKey key = _remotePublicKeyRing.getPublicKey(ops.getKeyID());	
		ops.initVerify(key, _provider);
		ops.update(xmlMessage.getBytes());
		
		if (!ops.verify(signatureList.get(0))) {
			throw new SignatureVerificationException("Failed to verify message signature. Message authenticity cannot be thrusted.");
		}
		
		return xmlMessage;
		
	} catch (PGPException pgpException) {
		throw new IOException("PGP subsystem problem.", pgpException);
		
	} catch (SignatureException signException) {
		throw new IOException("PGP subsystem problem.", signException);
		
	} catch (Throwable t) {
		throw new IOException("Unknown error occured in PGP subsystem: " + t.getMessage(), t);
		
	}
	
}
 
开发者ID:neopeak,项目名称:brownsocks-payments-enets,代码行数:48,代码来源:BCPGPProvider.java

示例12: encryptAndDecrypt

import org.bouncycastle.openpgp.PGPPublicKeyEncryptedData; //导入依赖的package包/类
@Test
public void encryptAndDecrypt() throws Exception {
	// both keys have property encryptionKey==true
	final String[] keyIds = {
			"d7a92a24aa97ddbd", // master-key
			"a58da7d810b74edf" // sub-key
	};

	for (final String keyId : keyIds) {
		final PGPDataEncryptorBuilder encryptorBuilder = new BcPGPDataEncryptorBuilder(SymmetricKeyAlgorithmTags.TWOFISH);
		final PGPEncryptedDataGenerator encryptedDataGenerator = new PGPEncryptedDataGenerator(encryptorBuilder);
		final PGPKeyEncryptionMethodGenerator keyEncryptionMethodGenerator = new BcPublicKeyKeyEncryptionMethodGenerator(
				getPgpPublicKeyOrFail(bytesToLong(decodeHexStr(keyId))));

		encryptedDataGenerator.addMethod(keyEncryptionMethodGenerator);

		final byte[] plain = new byte[1 + random.nextInt(1024 * 1024)];
		random.nextBytes(plain);

		final File encryptedFile = File.createTempFile("encrypted_", ".tmp");
		try (final OutputStream encryptedOut = new FileOutputStream(encryptedFile);) {
			try (final OutputStream plainOut = encryptedDataGenerator.open(encryptedOut, new byte[1024 * 16]);) {
				plainOut.write(plain);
			}
		}

		final byte[] decrypted;
		try (InputStream in = new FileInputStream(encryptedFile)) {
			final PGPEncryptedDataList encryptedDataList = new PGPEncryptedDataList(new BCPGInputStream(in));
			final Iterator<?> encryptedDataObjects = encryptedDataList.getEncryptedDataObjects();
			assertThat(encryptedDataObjects.hasNext()).isTrue();
			final PGPPublicKeyEncryptedData encryptedData = (PGPPublicKeyEncryptedData) encryptedDataObjects.next();
			assertThat(encryptedDataObjects.hasNext()).isFalse();

			final PublicKeyDataDecryptorFactory dataDecryptorFactory = new BcPublicKeyDataDecryptorFactory(
					getPgpPrivateKeyOrFail(encryptedData.getKeyID(), "test12345".toCharArray()));

			try (InputStream plainIn = encryptedData.getDataStream(dataDecryptorFactory);) {
				final ByteArrayOutputStream out = new ByteArrayOutputStream();
				transferStreamData(plainIn, out);
				decrypted = out.toByteArray();
			}
		}

		assertThat(decrypted).isEqualTo(plain);

		encryptedFile.delete(); // delete it, if this test did not fail
	}
}
 
开发者ID:subshare,项目名称:subshare,代码行数:50,代码来源:GnuPgTest.java

示例13: getPGPEncryptedData

import org.bouncycastle.openpgp.PGPPublicKeyEncryptedData; //导入依赖的package包/类
private static PGPPublicKeyEncryptedData getPGPEncryptedData( byte data[] ) throws IOException
{
    InputStream in = PGPUtil.getDecoderStream( new ByteArrayInputStream( data ) );

    JcaPGPObjectFactory objectFactory = new JcaPGPObjectFactory( in );

    PGPEncryptedDataList encryptedDataList = ( PGPEncryptedDataList ) objectFactory.nextObject();

    Iterator it = encryptedDataList.getEncryptedDataObjects();

    return ( PGPPublicKeyEncryptedData ) it.next();
}
 
开发者ID:subutai-io,项目名称:base,代码行数:13,代码来源:PGPDecrypt.java


注:本文中的org.bouncycastle.openpgp.PGPPublicKeyEncryptedData类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。