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

Java PGPLiteralData类代码示例

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

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



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


示例16: generateV3BinarySig

import org.bouncycastle.openpgp.PGPLiteralData; //导入依赖的package包/类
private byte[] generateV3BinarySig(PGPPrivateKey privKey, int encAlgorithm, int hashAlgorithm) 
    throws Exception
{
    ByteArrayOutputStream   bOut = new ByteArrayOutputStream();
    ByteArrayInputStream    testIn = new ByteArrayInputStream(TEST_DATA);
    PGPV3SignatureGenerator sGen = new PGPV3SignatureGenerator(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);
    
    return bOut.toByteArray();
}
 
开发者ID:NoYouShutup,项目名称:CryptMeme,代码行数:35,代码来源:PGPSignatureTest.java


示例17: encryptText

import org.bouncycastle.openpgp.PGPLiteralData; //导入依赖的package包/类
public String encryptText(String text) throws FileIOException
{
	final ByteArrayOutputStream output = new ByteArrayOutputStream();
	final byte[] bytes = text.getBytes();
	_encryptData(output, new ByteArrayInputStream(bytes), bytes.length, PGPLiteralData.CONSOLE, null, ENCRYPT_ALGORITHM, ENCRYPT_ARMOR);

	text = Base64.encodeBase64String(output.toByteArray());
	text = text.replace('/', '_');
	return text;
}
 
开发者ID:HolgerHees,项目名称:cloudsync,代码行数:11,代码来源:Crypt.java


示例18: decryptMessage

import org.bouncycastle.openpgp.PGPLiteralData; //导入依赖的package包/类
/**
 * decrypt the passed in message stream
 */
private byte[] decryptMessage(
    byte[]    message,
    Date      date)
    throws Exception
{
    PGPObjectFactory         pgpF = new BcPGPObjectFactory(message);
    PGPEncryptedDataList     enc = (PGPEncryptedDataList)pgpF.nextObject();
    PGPPBEEncryptedData      pbe = (PGPPBEEncryptedData)enc.get(0);

    InputStream clear = pbe.getDataStream(new BcPBEDataDecryptorFactory(pass, new BcPGPDigestCalculatorProvider()));
    
    PGPObjectFactory         pgpFact = new BcPGPObjectFactory(clear);
    
    PGPLiteralData           ld = (PGPLiteralData)pgpFact.nextObject();
    
    ByteArrayOutputStream    bOut = new ByteArrayOutputStream();
    if (!ld.getFileName().equals("test.txt")
        && !ld.getFileName().equals("_CONSOLE"))
    {
        fail("wrong filename in packet");
    }
    if (!ld.getModificationTime().equals(date))
    {
        fail("wrong modification time in packet: " + ld.getModificationTime().getTime() + " " + date.getTime());
    }

    InputStream              unc = ld.getInputStream();
    int                      ch;
    
    while ((ch = unc.read()) >= 0)
    {
        bOut.write(ch);
    }

    if (pbe.isIntegrityProtected() && !pbe.verify())
    {
        fail("integrity check failed");
    }

    return bOut.toByteArray();
}
 
开发者ID:ttt43ttt,项目名称:gwt-crypto,代码行数:45,代码来源:BcPGPPBETest.java


示例19: decryptMessageBuffered

import org.bouncycastle.openpgp.PGPLiteralData; //导入依赖的package包/类
private byte[] decryptMessageBuffered(
    byte[]    message,
    Date      date)
    throws Exception
{
    PGPObjectFactory         pgpF = new PGPObjectFactory(message, new BcKeyFingerprintCalculator());
    PGPEncryptedDataList     enc = (PGPEncryptedDataList)pgpF.nextObject();
    PGPPBEEncryptedData      pbe = (PGPPBEEncryptedData)enc.get(0);

    InputStream clear = pbe.getDataStream(new BcPBEDataDecryptorFactory(pass, new BcPGPDigestCalculatorProvider()));

    PGPObjectFactory         pgpFact = new PGPObjectFactory(clear, new BcKeyFingerprintCalculator());

    PGPLiteralData           ld = (PGPLiteralData)pgpFact.nextObject();

    ByteArrayOutputStream    bOut = new ByteArrayOutputStream();
    if (!ld.getFileName().equals("test.txt")
        && !ld.getFileName().equals("_CONSOLE"))
    {
        fail("wrong filename in packet");
    }
    if (!ld.getModificationTime().equals(date))
    {
        fail("wrong modification time in packet: " + ld.getModificationTime().getTime() + " " + date.getTime());
    }

    InputStream              unc = ld.getInputStream();
    byte[]                   buf = new byte[1024];
    int                      len;

    while ((len = unc.read(buf)) >= 0)
    {
        bOut.write(buf, 0, len);
    }

    if (pbe.isIntegrityProtected() && !pbe.verify())
    {
        fail("integrity check failed");
    }

    return bOut.toByteArray();
}
 
开发者ID:ttt43ttt,项目名称:gwt-crypto,代码行数:43,代码来源:BcPGPPBETest.java


示例20: generateTest

import org.bouncycastle.openpgp.PGPLiteralData; //导入依赖的package包/类
/**
 * Generated signature test
 * 
 * @param sKey
 * @param pgpPrivKey
 */
public void generateTest(
    PGPSecretKeyRing sKey,
    PGPPublicKey     pgpPubKey,
    PGPPrivateKey    pgpPrivKey)
    throws Exception
{
    String                  data = "hello world!";
    ByteArrayOutputStream   bOut = new ByteArrayOutputStream();
    ByteArrayInputStream    testIn = new ByteArrayInputStream(data.getBytes());
    PGPSignatureGenerator   sGen = new PGPSignatureGenerator(new BcPGPContentSignerBuilder(PublicKeyAlgorithmTags.DSA, HashAlgorithmTags.SHA1));

    sGen.init(PGPSignature.BINARY_DOCUMENT, pgpPrivKey);

    PGPSignatureSubpacketGenerator spGen = new PGPSignatureSubpacketGenerator();
    
    Iterator        it = sKey.getSecretKey().getPublicKey().getUserIDs();
    String          primaryUserID = (String)it.next();
    
    spGen.setSignerUserID(true, primaryUserID);
    
    sGen.setHashedSubpackets(spGen.generate());

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

    PGPLiteralDataGenerator lGen = new PGPLiteralDataGenerator();
    
    Date testDate = new Date((System.currentTimeMillis() / 1000) * 1000);
    OutputStream lOut = lGen.open(
        new UncloseableOutputStream(bOut),
        PGPLiteralData.BINARY,
        "_CONSOLE",
        data.getBytes().length,
        testDate);

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

    lGen.close();

    sGen.generate().encode(bOut);

    PGPObjectFactory        pgpFact = new BcPGPObjectFactory(bOut.toByteArray());

    PGPOnePassSignatureList p1 = (PGPOnePassSignatureList)pgpFact.nextObject();
    PGPOnePassSignature     ops = p1.get(0);
    
    PGPLiteralData          p2 = (PGPLiteralData)pgpFact.nextObject();
    if (!p2.getModificationTime().equals(testDate))
    {
        fail("Modification time not preserved");
    }

    InputStream             dIn = p2.getInputStream();

    ops.init(new BcPGPContentVerifierBuilderProvider(), pgpPubKey);
    
    while ((ch = dIn.read()) >= 0)
    {
        ops.update((byte)ch);
    }

    PGPSignatureList p3 = (PGPSignatureList)pgpFact.nextObject();

    if (!ops.verify(p3.get(0)))
    {
        fail("Failed generated signature check");
    }
}
 
开发者ID:ttt43ttt,项目名称:gwt-crypto,代码行数:79,代码来源:BcPGPDSATest.java



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


鲜花

握手

雷人

路过

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

请发表评论

全部评论

专题导读
上一篇:
Java DefaultSelectionEventManager类代码示例发布时间:2022-05-21
下一篇:
Java PackedSwitchPayload类代码示例发布时间: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