本文整理汇总了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();
}
}
示例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");
}
}
示例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() );
}
}
示例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");
}
示例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");
}
}
示例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 );
}
}
示例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();
}
示例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();
}
示例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();
}
示例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();
}
示例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() );
}
示例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());
}