本文整理汇总了Java中org.bouncycastle.bcpg.CompressionAlgorithmTags.ZIP属性的典型用法代码示例。如果您正苦于以下问题:Java CompressionAlgorithmTags.ZIP属性的具体用法?Java CompressionAlgorithmTags.ZIP怎么用?Java CompressionAlgorithmTags.ZIP使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类org.bouncycastle.bcpg.CompressionAlgorithmTags
的用法示例。
在下文中一共展示了CompressionAlgorithmTags.ZIP属性的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: encrypt
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 );
}
}
示例2: PGPCompressedDataGenerator
public PGPCompressedDataGenerator(
int algorithm,
int compression)
{
switch (algorithm)
{
case CompressionAlgorithmTags.UNCOMPRESSED:
case CompressionAlgorithmTags.ZIP:
case CompressionAlgorithmTags.ZLIB:
case CompressionAlgorithmTags.BZIP2:
break;
default:
throw new IllegalArgumentException("unknown compression algorithm");
}
if (compression != Deflater.DEFAULT_COMPRESSION)
{
if ((compression < Deflater.NO_COMPRESSION) || (compression > Deflater.BEST_COMPRESSION))
{
throw new IllegalArgumentException("unknown compression level: " + compression);
}
}
this.algorithm = algorithm;
this.compression = compression;
}
示例3: doOpen
private void doOpen() throws IOException
{
pkOut.write(algorithm);
switch (algorithm)
{
case CompressionAlgorithmTags.UNCOMPRESSED:
dOut = pkOut;
break;
case CompressionAlgorithmTags.ZIP:
dOut = new SafeDeflaterOutputStream(pkOut, compression, true);
break;
case CompressionAlgorithmTags.ZLIB:
dOut = new SafeDeflaterOutputStream(pkOut, compression, false);
break;
case CompressionAlgorithmTags.BZIP2:
dOut = new SafeCBZip2OutputStream(pkOut);
break;
default:
// Constructor should guard against this possibility
throw new IllegalStateException();
}
}
示例4: testExceptionDecryptorIncorrectInputFormatSymmetricEncryptedData
@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.");
}
示例5: signAndEncrypt
public static byte[] signAndEncrypt( final byte[] message, final PGPSecretKey secretKey, final String secretPwd,
final PGPPublicKey publicKey, final boolean armored ) throws PGPException
{
try
{
final ByteArrayOutputStream out = new ByteArrayOutputStream();
final PGPEncryptedDataGenerator encryptedDataGenerator = new PGPEncryptedDataGenerator(
new JcePGPDataEncryptorBuilder( SymmetricKeyAlgorithmTags.AES_256 ).setWithIntegrityPacket( true )
.setSecureRandom(
new SecureRandom() )
.setProvider( provider ) );
encryptedDataGenerator.addMethod(
new JcePublicKeyKeyEncryptionMethodGenerator( publicKey ).setSecureRandom( new SecureRandom() )
.setProvider( provider ) );
final OutputStream theOut = armored ? new ArmoredOutputStream( out ) : out;
final OutputStream encryptedOut = encryptedDataGenerator.open( theOut, new byte[4096] );
final PGPCompressedDataGenerator compressedDataGenerator =
new PGPCompressedDataGenerator( CompressionAlgorithmTags.ZIP );
final OutputStream compressedOut = compressedDataGenerator.open( encryptedOut, new byte[4096] );
final PGPPrivateKey privateKey = secretKey.extractPrivateKey(
new JcePBESecretKeyDecryptorBuilder().setProvider( provider ).build( secretPwd.toCharArray() ) );
final PGPSignatureGenerator signatureGenerator = new PGPSignatureGenerator(
new JcaPGPContentSignerBuilder( secretKey.getPublicKey().getAlgorithm(), HashAlgorithmTags.SHA1 )
.setProvider( provider ) );
signatureGenerator.init( PGPSignature.BINARY_DOCUMENT, privateKey );
final Iterator<?> it = secretKey.getPublicKey().getUserIDs();
if ( it.hasNext() )
{
final PGPSignatureSubpacketGenerator spGen = new PGPSignatureSubpacketGenerator();
spGen.setSignerUserID( false, ( String ) it.next() );
signatureGenerator.setHashedSubpackets( spGen.generate() );
}
signatureGenerator.generateOnePassVersion( false ).encode( compressedOut );
final PGPLiteralDataGenerator literalDataGenerator = new PGPLiteralDataGenerator();
final OutputStream literalOut = literalDataGenerator
.open( compressedOut, PGPLiteralData.BINARY, "filename", new Date(), new byte[4096] );
final InputStream in = new ByteArrayInputStream( message );
final byte[] buf = new byte[4096];
for ( int len; ( len = in.read( buf ) ) > 0; )
{
literalOut.write( buf, 0, len );
signatureGenerator.update( buf, 0, len );
}
in.close();
literalDataGenerator.close();
signatureGenerator.generate().encode( compressedOut );
compressedDataGenerator.close();
encryptedDataGenerator.close();
theOut.close();
return out.toByteArray();
}
catch ( Exception e )
{
throw new PGPException( "Error in signAndEncrypt", e );
}
}
示例6: compress
private static byte[] compress( byte data[] ) throws IOException
{
PGPCompressedDataGenerator compressGen = new PGPCompressedDataGenerator( CompressionAlgorithmTags.ZIP );
ByteArrayOutputStream bos = new ByteArrayOutputStream();
OutputStream compressOut = compressGen.open( bos );
OutputStream os =
new PGPLiteralDataGenerator().open( compressOut, PGPLiteralData.BINARY, "", data.length, new Date() );
os.write( data );
os.close();
compressGen.close();
return bos.toByteArray();
}
示例7: run
public void run(final char[] passphrase, final InputStream inputStream, final OutputStream outputStream) throws IOException, CryptoException {
try {
final OutputStream armor = new ArmoredOutputStream(outputStream);
final PGPEncryptedDataGenerator encryptedDataGenerator = new PGPEncryptedDataGenerator(new JcePGPDataEncryptorBuilder(
SymmetricKeyAlgorithmTags.AES_128).setWithIntegrityPacket(true).setSecureRandom(new SecureRandom()).setProvider(new BouncyCastleProvider()));
encryptedDataGenerator.addMethod(new JcePublicKeyKeyEncryptionMethodGenerator(publicKey).setSecureRandom(new SecureRandom()).setProvider(
new BouncyCastleProvider()));
final OutputStream encryptedOut = encryptedDataGenerator.open(armor, new byte[4096]);
final PGPCompressedDataGenerator compressedDataGenerator = new PGPCompressedDataGenerator(CompressionAlgorithmTags.ZIP);
final OutputStream compressedOut = compressedDataGenerator.open(encryptedOut, new byte[4096]);
final PGPPrivateKey privateKey = secretKey.extractPrivateKey(new JcePBESecretKeyDecryptorBuilder().setProvider(new BouncyCastleProvider())
.build(passphrase));
final PGPSignatureGenerator signatureGenerator = new PGPSignatureGenerator(new JcaPGPContentSignerBuilder(secretKey.getPublicKey()
.getAlgorithm(), HashAlgorithmTags.SHA1).setProvider(new BouncyCastleProvider()));
signatureGenerator.init(PGPSignature.BINARY_DOCUMENT, privateKey);
final Iterator<?> it = secretKey.getPublicKey().getUserIDs();
if (it.hasNext()) {
final PGPSignatureSubpacketGenerator spGen = new PGPSignatureSubpacketGenerator();
spGen.setSignerUserID(false, (String) it.next());
signatureGenerator.setHashedSubpackets(spGen.generate());
}
signatureGenerator.generateOnePassVersion(false).encode(compressedOut);
final PGPLiteralDataGenerator literalDataGenerator = new PGPLiteralDataGenerator();
final OutputStream literalOut = literalDataGenerator.open(compressedOut, PGPLiteralData.BINARY, "", new Date(), new byte[4096]);
final byte[] buf = new byte[4096];
for (int len = 0; (len = inputStream.read(buf)) > 0;) {
literalOut.write(buf, 0, len);
signatureGenerator.update(buf, 0, len);
}
literalDataGenerator.close();
signatureGenerator.generate().encode(compressedOut);
compressedDataGenerator.close();
encryptedDataGenerator.close();
armor.close();
}
catch (final Exception e) {
throw new CryptoException(e);
}
}