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


Java PGPLiteralData类代码示例

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


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

示例1: encrypt

import org.bouncycastle.openpgp.PGPLiteralData; //导入依赖的package包/类
public static byte[] encrypt( final byte[] message, final PGPPublicKey publicKey, boolean armored )
        throws PGPException
{
    try
    {
        final ByteArrayInputStream in = new ByteArrayInputStream( message );
        final ByteArrayOutputStream bOut = new ByteArrayOutputStream();
        final PGPLiteralDataGenerator literal = new PGPLiteralDataGenerator();
        final PGPCompressedDataGenerator comData = new PGPCompressedDataGenerator( CompressionAlgorithmTags.ZIP );
        final OutputStream pOut =
                literal.open( comData.open( bOut ), PGPLiteralData.BINARY, "filename", in.available(), new Date() );
        Streams.pipeAll( in, pOut );
        comData.close();
        final byte[] bytes = bOut.toByteArray();
        final PGPEncryptedDataGenerator generator = new PGPEncryptedDataGenerator(
                new JcePGPDataEncryptorBuilder( SymmetricKeyAlgorithmTags.AES_256 ).setWithIntegrityPacket( true )
                                                                                   .setSecureRandom(
                                                                                           new SecureRandom() )

                                                                                   .setProvider( provider ) );
        generator.addMethod( new JcePublicKeyKeyEncryptionMethodGenerator( publicKey ).setProvider( provider ) );
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        OutputStream theOut = armored ? new ArmoredOutputStream( out ) : out;
        OutputStream cOut = generator.open( theOut, bytes.length );
        cOut.write( bytes );
        cOut.close();
        theOut.close();
        return out.toByteArray();
    }
    catch ( Exception e )
    {
        throw new PGPException( "Error in encrypt", e );
    }
}
 
开发者ID:subutai-io,项目名称:base,代码行数:35,代码来源:PGPEncryptionUtil.java

示例2: asLiteral

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

示例3: checkLiteralData

import org.bouncycastle.openpgp.PGPLiteralData; //导入依赖的package包/类
private void checkLiteralData(PGPLiteralData ld, byte[] data)
    throws IOException
{
    ByteArrayOutputStream bOut = new ByteArrayOutputStream();

    if (!ld.getFileName().equals(PGPLiteralData.CONSOLE))
    {
        throw new RuntimeException("wrong filename in packet");
    }

    InputStream    inLd = ld.getDataStream();
    int ch;

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

    if (!areEqual(bOut.toByteArray(), data))
    {
        fail("wrong plain text in decrypted packet");
    }
}
 
开发者ID:ttt43ttt,项目名称:gwt-crypto,代码行数:24,代码来源:BcPGPRSATest.java

示例4: produceSign

import org.bouncycastle.openpgp.PGPLiteralData; //导入依赖的package包/类
private static void produceSign( byte[] data, BCPGOutputStream bcOut, PGPSignatureGenerator signGen )
        throws IOException, PGPException
{
    PGPLiteralDataGenerator literalGen = new PGPLiteralDataGenerator();

    OutputStream os = literalGen.open( bcOut, PGPLiteralData.BINARY, "", data.length, new Date() );

    InputStream is = new ByteArrayInputStream( data );

    int ch;

    while ( ( ch = is.read() ) >= 0 )
    {
        signGen.update( ( byte ) ch );
        os.write( ch );
    }

    literalGen.close();

    signGen.generate().encode( bcOut );
}
 
开发者ID:subutai-io,项目名称:base,代码行数:22,代码来源:PGPSign.java

示例5: createEncryptedNonCompressedData

import org.bouncycastle.openpgp.PGPLiteralData; //导入依赖的package包/类
void createEncryptedNonCompressedData(ByteArrayOutputStream bos, String keyringPath) throws Exception, IOException, PGPException,
        UnsupportedEncodingException {
    PGPEncryptedDataGenerator encGen = new PGPEncryptedDataGenerator(new JcePGPDataEncryptorBuilder(SymmetricKeyAlgorithmTags.CAST5)
            .setSecureRandom(new SecureRandom()).setProvider(getProvider()));
    encGen.addMethod(new JcePublicKeyKeyEncryptionMethodGenerator(readPublicKey(keyringPath)));
    OutputStream encOut = encGen.open(bos, new byte[512]);
    PGPLiteralDataGenerator litData = new PGPLiteralDataGenerator();
    OutputStream litOut = litData.open(encOut, PGPLiteralData.BINARY, PGPLiteralData.CONSOLE, new Date(), new byte[512]);

    try {
        litOut.write("Test Message Without Compression".getBytes("UTF-8"));
        litOut.flush();
    } finally {
        IOHelper.close(litOut);
        IOHelper.close(encOut, bos);
    }
}
 
开发者ID:HydAu,项目名称:Camel,代码行数:18,代码来源:PGPDataFormatTest.java

示例6: compress

import org.bouncycastle.openpgp.PGPLiteralData; //导入依赖的package包/类
private static byte[] compress(byte[] clearData, String fileName, int algorithm) throws IOException
{
    ByteArrayOutputStream bOut = new ByteArrayOutputStream();
    PGPCompressedDataGenerator comData = new PGPCompressedDataGenerator(algorithm);
    OutputStream cos = comData.open(bOut); // open it with the final destination

    PGPLiteralDataGenerator lData = new PGPLiteralDataGenerator();

    // we want to generate compressed data. This might be a user option later,
    // in which case we would pass in bOut.
    OutputStream  pOut = lData.open(cos, // the compressed output stream
                                    PGPLiteralData.BINARY,
                                    fileName,  // "filename" to store
                                    clearData.length, // length of clear data
                                    new Date()  // current time
                                  );

    pOut.write(clearData);
    pOut.close();

    comData.close();

    return bOut.toByteArray();
}
 
开发者ID:NoYouShutup,项目名称:CryptMeme,代码行数:25,代码来源:ByteArrayHandler.java

示例7: compressFile

import org.bouncycastle.openpgp.PGPLiteralData; //导入依赖的package包/类
static byte[] compressFile(String fileName, int algorithm) throws IOException
{
	ByteArrayOutputStream bOut = new ByteArrayOutputStream();
	PGPCompressedDataGenerator comData = new PGPCompressedDataGenerator(algorithm);
	PGPUtil.writeFileToLiteralData(comData.open(bOut), PGPLiteralData.BINARY, new File(fileName));
	comData.close();
	return bOut.toByteArray();
}
 
开发者ID:AnonymOnline,项目名称:saveOrganizer,代码行数:9,代码来源:PGPUtils.java

示例8: FileMetadata

import org.bouncycastle.openpgp.PGPLiteralData; //导入依赖的package包/类
/** Constructs a metadata object from Bouncy Castle message data. */
public FileMetadata(PGPLiteralData data) {
    this(data.getFileName(), Format.byCode((char) data.getFormat()));

    if (data.getModificationTime() != null)
        setLastModified(data.getModificationTime().getTime());
}
 
开发者ID:justinludwig,项目名称:jpgpj,代码行数:8,代码来源:FileMetadata.java

示例9: decrypt

import org.bouncycastle.openpgp.PGPLiteralData; //导入依赖的package包/类
public static byte[] decrypt( final byte[] encryptedMessage, final InputStream secretKeyRing,
                              final String secretPwd ) throws PGPException
{
    try
    {
        final PGPLiteralData msg = asLiteral( encryptedMessage, secretKeyRing, secretPwd );
        final ByteArrayOutputStream out = new ByteArrayOutputStream();
        Streams.pipeAll( msg.getInputStream(), out );
        return out.toByteArray();
    }
    catch ( Exception e )
    {
        throw new PGPException( "Error in decrypt", e );
    }
}
 
开发者ID:subutai-io,项目名称:base,代码行数:16,代码来源:PGPEncryptionUtil.java

示例10: encryptFile

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

示例11: testExceptionDecryptorIncorrectInputFormatSymmetricEncryptedData

import org.bouncycastle.openpgp.PGPLiteralData; //导入依赖的package包/类
@Test
public void testExceptionDecryptorIncorrectInputFormatSymmetricEncryptedData() throws Exception {

    byte[] payload = "Not Correct Format".getBytes("UTF-8");
    ByteArrayOutputStream bos = new ByteArrayOutputStream();

    PGPEncryptedDataGenerator encGen = new PGPEncryptedDataGenerator(new JcePGPDataEncryptorBuilder(SymmetricKeyAlgorithmTags.CAST5)
            .setSecureRandom(new SecureRandom()).setProvider(getProvider()));

    encGen.addMethod(new JcePBEKeyEncryptionMethodGenerator("pw".toCharArray()));

    OutputStream encOut = encGen.open(bos, new byte[1024]);
    PGPCompressedDataGenerator comData = new PGPCompressedDataGenerator(CompressionAlgorithmTags.ZIP);
    OutputStream comOut = new BufferedOutputStream(comData.open(encOut));
    PGPLiteralDataGenerator litData = new PGPLiteralDataGenerator();
    OutputStream litOut = litData.open(comOut, PGPLiteralData.BINARY, PGPLiteralData.CONSOLE, new Date(), new byte[1024]);
    litOut.write(payload);
    litOut.flush();
    litOut.close();
    comOut.close();
    encOut.close();
    MockEndpoint mock = getMockEndpoint("mock:exception");
    mock.expectedMessageCount(1);
    template.sendBody("direct:subkeyUnmarshal", bos.toByteArray());
    assertMockEndpointsSatisfied();

    checkThrownException(mock, IllegalArgumentException.class, null, "The input message body has an invalid format.");
}
 
开发者ID:HydAu,项目名称:Camel,代码行数:29,代码来源:PGPDataFormatTest.java

示例12: compressFile

import org.bouncycastle.openpgp.PGPLiteralData; //导入依赖的package包/类
static byte[] compressFile(String fileName, int algorithm) throws IOException
{
    ByteArrayOutputStream bOut = new ByteArrayOutputStream();
    PGPCompressedDataGenerator comData = new PGPCompressedDataGenerator(algorithm);
    PGPUtil.writeFileToLiteralData(comData.open(bOut), PGPLiteralData.BINARY,
        new File(fileName));
    comData.close();
    return bOut.toByteArray();
}
 
开发者ID:petrsmid,项目名称:unicredit-connector,代码行数:10,代码来源:BouncyCastlePGPExampleUtil.java

示例13: encryptString

import org.bouncycastle.openpgp.PGPLiteralData; //导入依赖的package包/类
public String encryptString(PGPPublicKey key, String clearText)
		throws IOException, PGPException {

	byte[] clearData = clearText.getBytes(StandardCharsets.UTF_8);

	ByteArrayOutputStream encOut = new ByteArrayOutputStream();
	OutputStream out = new ArmoredOutputStream(encOut);
	ByteArrayOutputStream bOut = new ByteArrayOutputStream();

	PGPCompressedDataGenerator comData = new PGPCompressedDataGenerator(
			PGPCompressedDataGenerator.ZIP);
	OutputStream cos = comData.open(bOut);
	PGPLiteralDataGenerator lData = new PGPLiteralDataGenerator();

	OutputStream pOut = lData.open(cos, PGPLiteralData.BINARY,
			PGPLiteralData.CONSOLE, clearData.length, new Date());
	pOut.write(clearData);

	lData.close();
	comData.close();

	PGPEncryptedDataGenerator encGen = new PGPEncryptedDataGenerator(
			new JcePGPDataEncryptorBuilder(PGPEncryptedData.CAST5)
					.setWithIntegrityPacket(true)
					.setSecureRandom(new SecureRandom()).setProvider(BouncyCastleProvider.PROVIDER_NAME));

	encGen.addMethod(new JcePublicKeyKeyEncryptionMethodGenerator(key)
			.setProvider(BouncyCastleProvider.PROVIDER_NAME));

	byte[] bytes = bOut.toByteArray();
	OutputStream cOut = encGen.open(out, bytes.length);
	cOut.write(bytes);
	cOut.close();
	out.close();

	return new String(encOut.toByteArray());
}
 
开发者ID:mpw96,项目名称:geocaching,代码行数:38,代码来源:StringEncryptor.java

示例14: bufferTest

import org.bouncycastle.openpgp.PGPLiteralData; //导入依赖的package包/类
private void bufferTest(
    PGPLiteralDataGenerator generator, 
    byte[] buf, 
    int i)
    throws IOException
{
    ByteArrayOutputStream bOut = new ByteArrayOutputStream();
    OutputStream out = generator.open(
        new UncloseableOutputStream(bOut),
        PGPLiteralData.BINARY,
        PGPLiteralData.CONSOLE,
        i,
        new Date());

    out.write(buf, 0, i);
    
    generator.close();
    
    PGPObjectFactory        fact = new PGPObjectFactory(bOut.toByteArray());
    PGPLiteralData          data = (PGPLiteralData)fact.nextObject();
    InputStream             in = data.getInputStream();

    for (int count = 0; count != i; count++)
    {
        if (in.read() != (buf[count] & 0xff))
        {
            fail("failed readback test - length = " + i);
        }
    }
}
 
开发者ID:NoYouShutup,项目名称:CryptMeme,代码行数:31,代码来源:PGPPacketTest.java

示例15: testSig

import org.bouncycastle.openpgp.PGPLiteralData; //导入依赖的package包/类
private void testSig(
    int           encAlgorithm,
    int           hashAlgorithm,
    PGPPublicKey  pubKey,
    PGPPrivateKey privKey)
    throws Exception
{            
    ByteArrayOutputStream bOut = new ByteArrayOutputStream();
    ByteArrayInputStream  testIn = new ByteArrayInputStream(TEST_DATA);
    PGPSignatureGenerator sGen = new PGPSignatureGenerator(new JcaPGPContentSignerBuilder(encAlgorithm, hashAlgorithm).setProvider("BC"));
    
    sGen.init(PGPSignature.BINARY_DOCUMENT, privKey);
    sGen.generateOnePassVersion(false).encode(bOut);

    PGPLiteralDataGenerator    lGen = new PGPLiteralDataGenerator();
    OutputStream               lOut = lGen.open(
        new UncloseableOutputStream(bOut),
        PGPLiteralData.BINARY,
        "_CONSOLE",
        TEST_DATA.length * 2,
        new Date());

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

    lOut.write(TEST_DATA);
    sGen.update(TEST_DATA);
    
    lGen.close();

    sGen.generate().encode(bOut);

    verifySignature(bOut.toByteArray(), hashAlgorithm, pubKey, TEST_DATA);
}
 
开发者ID:NoYouShutup,项目名称:CryptMeme,代码行数:39,代码来源:PGPSignatureTest.java


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