• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Java PGPCompressedData类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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



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

示例1: testCompressDecompress

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


示例2: validateData

import org.bouncycastle.openpgp.PGPCompressedData; //导入依赖的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");
    }
}
 
开发者ID:NoYouShutup,项目名称:CryptMeme,代码行数:21,代码来源:PGPCompressionTest.java


示例3: asLiteral

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


示例4: encryptFile

import org.bouncycastle.openpgp.PGPCompressedData; //导入依赖的package包/类
/**
* Encrypt the given file 
* @param unencryptedFileName - Name of the unecrypted file
* @param encryptedFileName - Name of the encrypted file, will have .enc as extension
* @throws IOException 
* @throws NoSuchProviderException
* @throws PGPException
* @throws CryptDecryptException 
*/
  public void encryptFile(final String unencryptedFileName, final String encryptedFileName)
      throws IOException, NoSuchProviderException, PGPException, CryptDecryptException {
  	if(enableDebugLog)Trace.logInfo("CryptDecryptUtil.encryptFile", "Entry");

  	// Initialise PGP provider and read public key
  	if(!initialized) initialise(false);

FileOutputStream encrytedFile = new FileOutputStream(encryptedFileName);
        
// Compress the input plain text file in ZIP format.
  	ByteArrayOutputStream bOut = new ByteArrayOutputStream();
      PGPCompressedDataGenerator comData = new PGPCompressedDataGenerator(PGPCompressedData.ZIP);
      PGPUtil.writeFileToLiteralData(comData.open(bOut), PGPLiteralData.BINARY, new File(unencryptedFileName) );
      comData.close();

      // Encrypt the file using Triple-DES algorithm
      BcPGPDataEncryptorBuilder dataEncryptor = new BcPGPDataEncryptorBuilder(PGPEncryptedData.TRIPLE_DES);
      dataEncryptor.setWithIntegrityPacket(false);
      dataEncryptor.setSecureRandom(new SecureRandom());
      PGPEncryptedDataGenerator encryptedDataGenerator = new PGPEncryptedDataGenerator(dataEncryptor);
      encryptedDataGenerator.addMethod(new BcPublicKeyKeyEncryptionMethodGenerator(publicKey));
      byte[] bytes = bOut.toByteArray();
      OutputStream cOut = encryptedDataGenerator.open(encrytedFile, bytes.length);
      cOut.write(bytes);
      cOut.close();
      encrytedFile.close();
      
      if(enableDebugLog)Trace.logInfo("CryptDecryptUtil.encryptFile", "Exit");
  }
 
开发者ID:ibm-messaging,项目名称:mq-mft,代码行数:39,代码来源:CryptDecryptUtil.java


示例5: testCompression

import org.bouncycastle.openpgp.PGPCompressedData; //导入依赖的package包/类
private void testCompression(
    int type)
    throws IOException, PGPException
{
    ByteArrayOutputStream bOut = new ByteArrayOutputStream();
    PGPCompressedDataGenerator cPacket = new PGPCompressedDataGenerator(type);

    OutputStream out = cPacket.open(new UncloseableOutputStream(bOut));

    out.write("hello world!".getBytes());

    out.close();

    PGPObjectFactory pgpFact = new PGPObjectFactory(bOut.toByteArray());
    PGPCompressedData c1 = (PGPCompressedData)pgpFact.nextObject();
    InputStream pIn = c1.getDataStream();

    bOut.reset();

    int ch;
    while ((ch = pIn.read()) >= 0)
    {
        bOut.write(ch);
    }

    if (!areEqual(bOut.toByteArray(), "hello world!".getBytes()))
    {
        fail("compression test failed");
    }
}
 
开发者ID:NoYouShutup,项目名称:CryptMeme,代码行数:31,代码来源:PGPCompressionTest.java


示例6: sign

import org.bouncycastle.openpgp.PGPCompressedData; //导入依赖的package包/类
public static byte[] sign( byte[] message, PGPSecretKey secretKey, String secretPwd, boolean armor )
        throws PGPException
{
    try
    {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        OutputStream theOut = armor ? new ArmoredOutputStream( out ) : out;

        PGPPrivateKey pgpPrivKey = secretKey.extractPrivateKey(
                new JcePBESecretKeyDecryptorBuilder().setProvider( provider ).build( secretPwd.toCharArray() ) );
        PGPSignatureGenerator sGen = new PGPSignatureGenerator(
                new JcaPGPContentSignerBuilder( secretKey.getPublicKey().getAlgorithm(), PGPUtil.SHA1 )
                        .setProvider( provider ) );

        sGen.init( PGPSignature.BINARY_DOCUMENT, pgpPrivKey );

        Iterator it = secretKey.getPublicKey().getUserIDs();
        if ( it.hasNext() )
        {
            PGPSignatureSubpacketGenerator spGen = new PGPSignatureSubpacketGenerator();

            spGen.setSignerUserID( false, ( String ) it.next() );
            sGen.setHashedSubpackets( spGen.generate() );
        }

        PGPCompressedDataGenerator cGen = new PGPCompressedDataGenerator( PGPCompressedData.ZLIB );

        BCPGOutputStream bOut = new BCPGOutputStream( cGen.open( theOut ) );

        sGen.generateOnePassVersion( false ).encode( bOut );

        PGPLiteralDataGenerator lGen = new PGPLiteralDataGenerator();
        OutputStream lOut =
                lGen.open( bOut, PGPLiteralData.BINARY, "filename", new Date(), new byte[4096] );         //
        InputStream fIn = new ByteArrayInputStream( message );
        int ch;

        while ( ( ch = fIn.read() ) >= 0 )
        {
            lOut.write( ch );
            sGen.update( ( byte ) ch );
        }

        lGen.close();

        sGen.generate().encode( bOut );

        cGen.close();

        theOut.close();

        return out.toByteArray();
    }
    catch ( Exception e )
    {
        throw new PGPException( "Error in sign", e );
    }
}
 
开发者ID:subutai-io,项目名称:base,代码行数:59,代码来源:PGPEncryptionUtil.java


示例7: sign

import org.bouncycastle.openpgp.PGPCompressedData; //导入依赖的package包/类
public static byte[] sign( byte data[], PGPPrivateKey privateKey ) throws IOException, PGPException
{
    ByteArrayOutputStream bos = new ByteArrayOutputStream();

    ArmoredOutputStream aos = new ArmoredOutputStream( bos );

    PGPCompressedDataGenerator compressGen = new PGPCompressedDataGenerator( PGPCompressedData.ZLIB );

    BCPGOutputStream bcOut = new BCPGOutputStream( compressGen.open( aos ) );

    PGPSignatureGenerator signGen = getSignatureGenerator( privateKey, bcOut );

    produceSign( data, bcOut, signGen );

    compressGen.close();

    aos.close();

    return bos.toByteArray();
}
 
开发者ID:subutai-io,项目名称:base,代码行数:21,代码来源:PGPSign.java


示例8: getInputStream

import org.bouncycastle.openpgp.PGPCompressedData; //导入依赖的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: signFile

import org.bouncycastle.openpgp.PGPCompressedData; //导入依赖的package包/类
public static byte[] signFile(byte[] content, String fileName, InputStream keyIn,
		char[] pass, Provider securityProvider, TimeProvider timeProvider) throws IOException, NoSuchAlgorithmException,
		NoSuchProviderException, PGPException, SignatureException {

	ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
	ArmoredOutputStream armoredOut = new ArmoredOutputStream(byteArrayOut);
	PGPLiteralDataGenerator literalDataGenerator = new PGPLiteralDataGenerator();
	PGPCompressedDataGenerator compressedDataGenerator = new PGPCompressedDataGenerator(PGPCompressedData.ZLIB);

	try {
		PGPSecretKey pgpSecretKey = BouncyCastlePGPExampleUtil.readSecretKey(keyIn);
		PGPPrivateKey pgpPrivateKey = pgpSecretKey.extractPrivateKey(pass, securityProvider);
		PGPSignatureGenerator signatureGenerator = new PGPSignatureGenerator(pgpSecretKey
				.getPublicKey().getAlgorithm(), PGPUtil.SHA1, securityProvider);

		signatureGenerator.initSign(PGPSignature.BINARY_DOCUMENT, pgpPrivateKey);

		Iterator<?> it = pgpSecretKey.getPublicKey().getUserIDs();
		if (it.hasNext()) { //get first user ID
			PGPSignatureSubpacketGenerator spGen = new PGPSignatureSubpacketGenerator();

			spGen.setSignerUserID(false, (String) it.next());
			signatureGenerator.setHashedSubpackets(spGen.generate());
		} else {
			throw new PGPException("No user ID found in the public key of the certificate.");
		}
		

		BCPGOutputStream bOut = new BCPGOutputStream(compressedDataGenerator.open(armoredOut));

		signatureGenerator.generateOnePassVersion(false).encode(bOut);

		OutputStream literalOut = literalDataGenerator.open(bOut, PGPLiteralData.BINARY, fileName, timeProvider.nowDate(), content);

		literalOut.write(content);
		signatureGenerator.update(content);

		signatureGenerator.generate().encode(bOut);

	} finally {
		literalDataGenerator.close();
		compressedDataGenerator.close();
		armoredOut.close();
	}
	
	return byteArrayOut.toByteArray();
}
 
开发者ID:petrsmid,项目名称:unicredit-connector,代码行数:48,代码来源:BouncyCastleSigner.java


示例10: encryptAndSign

import org.bouncycastle.openpgp.PGPCompressedData; //导入依赖的package包/类
/**
 * Encrypt, sign and write input stream data to output stream.
 * Input and output stream are closed.
 */
private static void encryptAndSign(
        InputStream plainInput, OutputStream encryptedOutput,
        PersonalKey myKey, List<PGPUtils.PGPCoderKey> receiverKeys)
        throws IOException, PGPException {

    // setup data encryptor & generator
    BcPGPDataEncryptorBuilder encryptor = new BcPGPDataEncryptorBuilder(PGPEncryptedData.AES_192);
    encryptor.setWithIntegrityPacket(true);
    encryptor.setSecureRandom(new SecureRandom());

    // add public key recipients
    PGPEncryptedDataGenerator encGen = new PGPEncryptedDataGenerator(encryptor);
    receiverKeys.forEach(key ->
        encGen.addMethod(new BcPublicKeyKeyEncryptionMethodGenerator(key.encryptKey)));

    OutputStream encryptedOut = encGen.open(encryptedOutput, new byte[BUFFER_SIZE]);

    // setup compressed data generator
    PGPCompressedDataGenerator compGen = new PGPCompressedDataGenerator(PGPCompressedData.ZIP);
    OutputStream compressedOut = compGen.open(encryptedOut, new byte[BUFFER_SIZE]);

    // setup signature generator
    int algo = myKey.getSigningAlgorithm();
    PGPSignatureGenerator sigGen = new PGPSignatureGenerator(
            new BcPGPContentSignerBuilder(algo, HashAlgorithmTags.SHA256));
    sigGen.init(PGPSignature.BINARY_DOCUMENT, myKey.getPrivateSigningKey());

    PGPSignatureSubpacketGenerator spGen = new PGPSignatureSubpacketGenerator();
    spGen.setSignerUserID(false, myKey.getUserId());
    sigGen.setUnhashedSubpackets(spGen.generate());

    sigGen.generateOnePassVersion(false).encode(compressedOut);

    // Initialize literal data generator
    PGPLiteralDataGenerator literalGen = new PGPLiteralDataGenerator();
    OutputStream literalOut = literalGen.open(
        compressedOut,
        PGPLiteralData.BINARY,
        "",
        new Date(),
        new byte[BUFFER_SIZE]);

    // read the "in" stream, compress, encrypt and write to the "out" stream
    // this must be done if clear data is bigger than the buffer size
    // but there are other ways to optimize...
    byte[] buf = new byte[BUFFER_SIZE];
    int len;
    while ((len = plainInput.read(buf)) > 0) {
        literalOut.write(buf, 0, len);
        sigGen.update(buf, 0, len);
    }

    literalGen.close();

    // generate the signature, compress, encrypt and write to the "out" stream
    sigGen.generate().encode(compressedOut);
    compGen.close();
    encGen.close();
}
 
开发者ID:kontalk,项目名称:desktopclient-java,代码行数:64,代码来源:Encryptor.java


示例11: getObjectFactory

import org.bouncycastle.openpgp.PGPCompressedData; //导入依赖的package包/类
private static JcaPGPObjectFactory getObjectFactory( byte signedData[] ) throws IOException, PGPException
{
    InputStream in = PGPUtil.getDecoderStream( new ByteArrayInputStream( signedData ) );

    JcaPGPObjectFactory pgpFact = new JcaPGPObjectFactory( in );

    PGPCompressedData compressedData = ( PGPCompressedData ) pgpFact.nextObject();

    return new JcaPGPObjectFactory( compressedData.getDataStream() );
}
 
开发者ID:subutai-io,项目名称:base,代码行数:11,代码来源:PGPVerify.java


示例12: openDecompressor

import org.bouncycastle.openpgp.PGPCompressedData; //导入依赖的package包/类
/**
 * Opens a new {@link Decompressor} (Reading Step 2/3)
 *
 * <p>This is the second step in reading a ghostryde file. After this method, you'll want to
 * call {@link #openInput(Decompressor)}.
 *
 * @param input is the value returned by {@link #openDecryptor}.
 * @throws IOException
 * @throws PGPException
 */
@CheckReturnValue
public Decompressor openDecompressor(@WillNotClose Decryptor input)
    throws IOException, PGPException {
  PGPObjectFactory fact = new BcPGPObjectFactory(checkNotNull(input, "input"));
  PGPCompressedData compressed = pgpCast(fact.nextObject(), PGPCompressedData.class);
  return new Decompressor(compressed.getDataStream());
}
 
开发者ID:google,项目名称:nomulus,代码行数:18,代码来源:Ghostryde.java



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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Java HelloService类代码示例发布时间:2022-05-21
下一篇:
Java IRenameStrategy类代码示例发布时间:2022-05-21
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap