本文整理汇总了Java中org.bouncycastle.openpgp.PGPLiteralDataGenerator.close方法的典型用法代码示例。如果您正苦于以下问题:Java PGPLiteralDataGenerator.close方法的具体用法?Java PGPLiteralDataGenerator.close怎么用?Java PGPLiteralDataGenerator.close使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.bouncycastle.openpgp.PGPLiteralDataGenerator
的用法示例。
在下文中一共展示了PGPLiteralDataGenerator.close方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: produceSign
import org.bouncycastle.openpgp.PGPLiteralDataGenerator; //导入方法依赖的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 );
}
示例2: encryptString
import org.bouncycastle.openpgp.PGPLiteralDataGenerator; //导入方法依赖的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());
}
示例3: bufferTest
import org.bouncycastle.openpgp.PGPLiteralDataGenerator; //导入方法依赖的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);
}
}
}
示例4: testSig
import org.bouncycastle.openpgp.PGPLiteralDataGenerator; //导入方法依赖的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);
}
示例5: generateV3BinarySig
import org.bouncycastle.openpgp.PGPLiteralDataGenerator; //导入方法依赖的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();
}
示例6: generateTest
import org.bouncycastle.openpgp.PGPLiteralDataGenerator; //导入方法依赖的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");
}
}
示例7: signAndEncrypt
import org.bouncycastle.openpgp.PGPLiteralDataGenerator; //导入方法依赖的package包/类
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 );
}
}
示例8: sign
import org.bouncycastle.openpgp.PGPLiteralDataGenerator; //导入方法依赖的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 );
}
}
示例9: signFile
import org.bouncycastle.openpgp.PGPLiteralDataGenerator; //导入方法依赖的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: run
import org.bouncycastle.openpgp.PGPLiteralDataGenerator; //导入方法依赖的package包/类
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);
}
}
示例11: testTextSig
import org.bouncycastle.openpgp.PGPLiteralDataGenerator; //导入方法依赖的package包/类
private void testTextSig(
int encAlgorithm,
int hashAlgorithm,
PGPPublicKey pubKey,
PGPPrivateKey privKey,
byte[] data,
byte[] canonicalData)
throws Exception
{
PGPSignatureGenerator sGen = new PGPSignatureGenerator(new JcaPGPContentSignerBuilder(encAlgorithm, HashAlgorithmTags.SHA1).setProvider("BC"));
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
ByteArrayInputStream testIn = new ByteArrayInputStream(data);
Date creationTime = new Date();
sGen.init(PGPSignature.CANONICAL_TEXT_DOCUMENT, privKey);
sGen.generateOnePassVersion(false).encode(bOut);
PGPLiteralDataGenerator lGen = new PGPLiteralDataGenerator();
OutputStream lOut = lGen.open(
new UncloseableOutputStream(bOut),
PGPLiteralData.TEXT,
"_CONSOLE",
data.length * 2,
creationTime);
int ch;
while ((ch = testIn.read()) >= 0)
{
lOut.write(ch);
sGen.update((byte)ch);
}
lOut.write(data);
sGen.update(data);
lGen.close();
PGPSignature sig = sGen.generate();
if (sig.getCreationTime().getTime() == 0)
{
fail("creation time not set in v4 signature");
}
sig.encode(bOut);
verifySignature(bOut.toByteArray(), hashAlgorithm, pubKey, canonicalData);
}
示例12: testTextSigV3
import org.bouncycastle.openpgp.PGPLiteralDataGenerator; //导入方法依赖的package包/类
private void testTextSigV3(
int encAlgorithm,
int hashAlgorithm,
PGPPublicKey pubKey,
PGPPrivateKey privKey,
byte[] data,
byte[] canonicalData)
throws Exception
{
PGPV3SignatureGenerator sGen = new PGPV3SignatureGenerator(new JcaPGPContentSignerBuilder(encAlgorithm, HashAlgorithmTags.SHA1).setProvider("BC"));
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
ByteArrayInputStream testIn = new ByteArrayInputStream(data);
sGen.init(PGPSignature.CANONICAL_TEXT_DOCUMENT, privKey);
sGen.generateOnePassVersion(false).encode(bOut);
PGPLiteralDataGenerator lGen = new PGPLiteralDataGenerator();
OutputStream lOut = lGen.open(
new UncloseableOutputStream(bOut),
PGPLiteralData.TEXT,
"_CONSOLE",
data.length * 2,
new Date());
int ch;
while ((ch = testIn.read()) >= 0)
{
lOut.write(ch);
sGen.update((byte)ch);
}
lOut.write(data);
sGen.update(data);
lGen.close();
PGPSignature sig = sGen.generate();
if (sig.getCreationTime().getTime() == 0)
{
fail("creation time not set in v3 signature");
}
sig.encode(bOut);
verifySignature(bOut.toByteArray(), hashAlgorithm, pubKey, canonicalData);
}
示例13: doSigGenerateTest
import org.bouncycastle.openpgp.PGPLiteralDataGenerator; //导入方法依赖的package包/类
private void doSigGenerateTest(String privateKeyFile, String publicKeyFile, int digest)
throws Exception
{
PGPSecretKeyRing secRing = loadSecretKey(privateKeyFile);
PGPPublicKeyRing pubRing = loadPublicKey(publicKeyFile);
String data = "hello world!";
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
ByteArrayInputStream testIn = new ByteArrayInputStream(data.getBytes());
PGPSignatureGenerator sGen = new PGPSignatureGenerator(PublicKeyAlgorithmTags.DSA, digest, "BC");
sGen.initSign(PGPSignature.BINARY_DOCUMENT, secRing.getSecretKey().extractPrivateKey("test".toCharArray(), "BC"));
BCPGOutputStream bcOut = new BCPGOutputStream(bOut);
sGen.generateOnePassVersion(false).encode(bcOut);
PGPLiteralDataGenerator lGen = new PGPLiteralDataGenerator();
Date testDate = new Date((System.currentTimeMillis() / 1000) * 1000);
OutputStream lOut = lGen.open(
new UncloseableOutputStream(bcOut),
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(bcOut);
PGPObjectFactory pgpFact = new PGPObjectFactory(bOut.toByteArray());
PGPOnePassSignatureList p1 = (PGPOnePassSignatureList)pgpFact.nextObject();
PGPOnePassSignature ops = p1.get(0);
assertEquals(digest, ops.getHashAlgorithm());
assertEquals(PublicKeyAlgorithmTags.DSA, ops.getKeyAlgorithm());
PGPLiteralData p2 = (PGPLiteralData)pgpFact.nextObject();
if (!p2.getModificationTime().equals(testDate))
{
fail("Modification time not preserved");
}
InputStream dIn = p2.getInputStream();
ops.initVerify(pubRing.getPublicKey(), "BC");
while ((ch = dIn.read()) >= 0)
{
ops.update((byte)ch);
}
PGPSignatureList p3 = (PGPSignatureList)pgpFact.nextObject();
PGPSignature sig = p3.get(0);
assertEquals(digest, sig.getHashAlgorithm());
assertEquals(PublicKeyAlgorithmTags.DSA, sig.getKeyAlgorithm());
assertTrue(ops.verify(sig));
}
示例14: encryptAndSign
import org.bouncycastle.openpgp.PGPLiteralDataGenerator; //导入方法依赖的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();
}
示例15: generateTest
import org.bouncycastle.openpgp.PGPLiteralDataGenerator; //导入方法依赖的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 PGPObjectFactory(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");
}
}