File encrypted properly but not signed properly!

本文介绍了一段用于实现PGP文件签名后再进行加密的Java代码。该代码使用了Bouncy Castle库,通过生成RSA主密钥对和子密钥对来完成文件的签名过程,并利用客户的公钥进行加密。然而,尽管文件被正确加密,但签名部分似乎存在问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

File encrypted properly but not signed properly!

 

http://bouncy-castle.1462172.n4.nabble.com/File-encrypted-properly-but-not-signed-properly-td1468153.html

Hello all,
I am using the below code to sign-then-encrypt a file. The customer has provided their public key. I am also generating a RSA Master key pair and a sub-key key pair. I use the Master Private key to sign the file. Now as per the customer,  the file looks to be encrypted properly however it is not signed!!!!
 
I am not able to understand what is wrong with the below code. If you require, I can also forward the code which generates the key pairs.
 
public void signThenEncryptFile(){

PGPPublicKey pgpEncryptionKey = keyManager.retrievePublicKey(pgpBankKeyId); // get bank public key

PGPSecretKey pgpSigningKey = keyManager.retrievePrivateKey(isdpgpkey.getIsdPgpMasterKeyId()); // get the generated Master private key

 

if(pgpEncryptionKey == null || pgpSigningKey == null){

throw new CryptoException("Unable to find the encryption key or the signing key!");

}

 

logger.info("Successfully retrieved the PGP encryption key and signing key");

 

// generator for encrypted objects

PGPEncryptedDataGenerator cPk = new PGPEncryptedDataGenerator(PGPEncryptedData.CAST5, withIntegrityCheck, new SecureRandom(), "BC");

 

// add a public key encrypted session key to the encrypted object

cPk.addMethod(pgpEncryptionKey);

 

// Return an outputstream which will encrypt the data as it is written to it.

// The stream will be written out in chunks according to the size of the passed in buffer.

OutputStream cOut = cPk.open(out, new byte[1 << 16]);

 

if(logger.isDebugEnabled())

logger.debug("Generated an Outputstream with encrypted data");

 

// get the private key

PGPPrivateKey pgpPrivKey = pgpSigningKey.extractPrivateKey(isdpgpkey.getPassPhrase().toCharArray(), "BC");

 

if(logger.isDebugEnabled())

logger.debug("Extracted the private key to be used for signing");

 

// initialize the PGP signature generator

PGPSignatureGenerator sGen = new PGPSignatureGenerator(pgpSigningKey.getPublicKey().getAlgorithm(), PGPUtil.SHA1, "BC");

sGen.initSign(PGPSignature.BINARY_DOCUMENT, pgpPrivKey);

 

if(logger.isDebugEnabled())

logger.debug("Initialized the PGP signature generator");

 

Iterator it = pgpSigningKey.getPublicKey().getUserIDs();

if (it.hasNext())

{

PGPSignatureSubpacketGenerator spGen = new PGPSignatureSubpacketGenerator();

spGen.setSignerUserID(false, (String)it.next());

sGen.setHashedSubpackets(spGen.generate());

}

// generate compressed data packets

PGPCompressedDataGenerator cGen = new PGPCompressedDataGenerator(PGPCompressedData.ZLIB);

 

// return an outputstream which will save the data being written to the compressed object.

BCPGOutputStream bOut = new BCPGOutputStream(cGen.open(cOut));

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

 

if(logger.isDebugEnabled())

logger.debug("Initialized the PGP compressed data generator");

 

//File file = new File(inputFileName);

PGPLiteralDataGenerator lGen = new PGPLiteralDataGenerator();

 

// Open a literal data packet, returning a stream to store the data inside the packet as an indefinite length stream.

// The stream is written out as a series of partial packets with a chunk size determined by the size of the passed in buffer.

OutputStream lOut = lGen.open(bOut, PGPLiteralData.BINARY, inputFileName, new Date(),new byte[1<<20]);

 

if(logger.isDebugEnabled())

logger.debug("Signing the encrypted data");

 

// use of buffering to speed up write

byte[] buffer = new byte[1<<20];

 

FileInputStream fIn = new FileInputStream(file);

int bytesRead = 0;

while((bytesRead = fIn.read(buffer)) != -1) {

lOut.write(buffer,0,bytesRead);

sGen.update(buffer,0,bytesRead);

lOut.flush();

}

// close the streams

lGen.close();

sGen.generate().encode(bOut);

cGen.close();

cOut.close();

out.close();
}
 


Hello all,
I am using the below code to sign-then-encrypt a file. The customer has provided their public key. I am also generating a RSA Master key pair and a sub-key key pair. I use the Master Private key to sign the file. Now as per the customer,  the file looks to be encrypted properly however it is not signed!!!!
 
I am not able to understand what is wrong with the below code. If you require, I can also forward the code which generates the key pairs.
 
public void signThenEncryptFile(){

PGPPublicKey pgpEncryptionKey = keyManager.retrievePublicKey(pgpBankKeyId); // get bank public key

PGPSecretKey pgpSigningKey = keyManager.retrievePrivateKey(isdpgpkey.getIsdPgpMasterKeyId()); // get the generated Master private key

 

if(pgpEncryptionKey == null || pgpSigningKey == null){

throw new CryptoException("Unable to find the encryption key or the signing key!");

}

 

logger.info("Successfully retrieved the PGP encryption key and signing key");

 

// generator for encrypted objects

PGPEncryptedDataGenerator cPk = new PGPEncryptedDataGenerator(PGPEncryptedData.CAST5, withIntegrityCheck, new SecureRandom(), "BC");

 

// add a public key encrypted session key to the encrypted object

cPk.addMethod(pgpEncryptionKey);

 

// Return an outputstream which will encrypt the data as it is written to it.

// The stream will be written out in chunks according to the size of the passed in buffer.

OutputStream cOut = cPk.open(out, new byte[1 << 16]);

 

if(logger.isDebugEnabled())

logger.debug("Generated an Outputstream with encrypted data");

 

// get the private key

PGPPrivateKey pgpPrivKey = pgpSigningKey.extractPrivateKey(isdpgpkey.getPassPhrase().toCharArray(), "BC");

 

if(logger.isDebugEnabled())

logger.debug("Extracted the private key to be used for signing");

 

// initialize the PGP signature generator

PGPSignatureGenerator sGen = new PGPSignatureGenerator(pgpSigningKey.getPublicKey().getAlgorithm(), PGPUtil.SHA1, "BC");

sGen.initSign(PGPSignature.BINARY_DOCUMENT, pgpPrivKey);

 

if(logger.isDebugEnabled())

logger.debug("Initialized the PGP signature generator");

 

Iterator it = pgpSigningKey.getPublicKey().getUserIDs();

if (it.hasNext())

{

PGPSignatureSubpacketGenerator spGen = new PGPSignatureSubpacketGenerator();

spGen.setSignerUserID(false, (String)it.next());

sGen.setHashedSubpackets(spGen.generate());

}

// generate compressed data packets

PGPCompressedDataGenerator cGen = new PGPCompressedDataGenerator(PGPCompressedData.ZLIB);

 

// return an outputstream which will save the data being written to the compressed object.

BCPGOutputStream bOut = new BCPGOutputStream(cGen.open(cOut));

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

 

if(logger.isDebugEnabled())

logger.debug("Initialized the PGP compressed data generator");

 

//File file = new File(inputFileName);

PGPLiteralDataGenerator lGen = new PGPLiteralDataGenerator();

 

// Open a literal data packet, returning a stream to store the data inside the packet as an indefinite length stream.

// The stream is written out as a series of partial packets with a chunk size determined by the size of the passed in buffer.

OutputStream lOut = lGen.open(bOut, PGPLiteralData.BINARY, inputFileName, new Date(),new byte[1<<20]);

 

if(logger.isDebugEnabled())

logger.debug("Signing the encrypted data");

 

// use of buffering to speed up write

byte[] buffer = new byte[1<<20];

 

FileInputStream fIn = new FileInputStream(file);

int bytesRead = 0;

while((bytesRead = fIn.read(buffer)) != -1) {

lOut.write(buffer,0,bytesRead);

sGen.update(buffer,0,bytesRead);

lOut.flush();

}

// close the streams

lGen.close();

sGen.generate().encode(bOut);

cGen.close();

cOut.close();

out.close();
}
 


Hello all,
I am using the below code to sign-then-encrypt a file. The customer has provided their public key. I am also generating a RSA Master key pair and a sub-key key pair. I use the Master Private key to sign the file. Now as per the customer,  the file looks to be encrypted properly however it is not signed!!!!
 
I am not able to understand what is wrong with the below code. If you require, I can also forward the code which generates the key pairs.
 
public void signThenEncryptFile(){

PGPPublicKey pgpEncryptionKey = keyManager.retrievePublicKey(pgpBankKeyId); // get bank public key

PGPSecretKey pgpSigningKey = keyManager.retrievePrivateKey(isdpgpkey.getIsdPgpMasterKeyId()); // get the generated Master private key

 

if(pgpEncryptionKey == null || pgpSigningKey == null){

throw new CryptoException("Unable to find the encryption key or the signing key!");

}

 

logger.info("Successfully retrieved the PGP encryption key and signing key");

 

// generator for encrypted objects

PGPEncryptedDataGenerator cPk = new PGPEncryptedDataGenerator(PGPEncryptedData.CAST5, withIntegrityCheck, new SecureRandom(), "BC");

 

// add a public key encrypted session key to the encrypted object

cPk.addMethod(pgpEncryptionKey);

 

// Return an outputstream which will encrypt the data as it is written to it.

// The stream will be written out in chunks according to the size of the passed in buffer.

OutputStream cOut = cPk.open(out, new byte[1 << 16]);

 

if(logger.isDebugEnabled())

logger.debug("Generated an Outputstream with encrypted data");

 

// get the private key

PGPPrivateKey pgpPrivKey = pgpSigningKey.extractPrivateKey(isdpgpkey.getPassPhrase().toCharArray(), "BC");

 

if(logger.isDebugEnabled())

logger.debug("Extracted the private key to be used for signing");

 

// initialize the PGP signature generator

PGPSignatureGenerator sGen = new PGPSignatureGenerator(pgpSigningKey.getPublicKey().getAlgorithm(), PGPUtil.SHA1, "BC");

sGen.initSign(PGPSignature.BINARY_DOCUMENT, pgpPrivKey);

 

if(logger.isDebugEnabled())

logger.debug("Initialized the PGP signature generator");

 

Iterator it = pgpSigningKey.getPublicKey().getUserIDs();

if (it.hasNext())

{

PGPSignatureSubpacketGenerator spGen = new PGPSignatureSubpacketGenerator();

spGen.setSignerUserID(false, (String)it.next());

sGen.setHashedSubpackets(spGen.generate());

}

// generate compressed data packets

PGPCompressedDataGenerator cGen = new PGPCompressedDataGenerator(PGPCompressedData.ZLIB);

 

// return an outputstream which will save the data being written to the compressed object.

BCPGOutputStream bOut = new BCPGOutputStream(cGen.open(cOut));

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

 

if(logger.isDebugEnabled())

logger.debug("Initialized the PGP compressed data generator");

 

//File file = new File(inputFileName);

PGPLiteralDataGenerator lGen = new PGPLiteralDataGenerator();

 

// Open a literal data packet, returning a stream to store the data inside the packet as an indefinite length stream.

// The stream is written out as a series of partial packets with a chunk size determined by the size of the passed in buffer.

OutputStream lOut = lGen.open(bOut, PGPLiteralData.BINARY, inputFileName, new Date(),new byte[1<<20]);

 

if(logger.isDebugEnabled())

logger.debug("Signing the encrypted data");

 

// use of buffering to speed up write

byte[] buffer = new byte[1<<20];

 

FileInputStream fIn = new FileInputStream(file);

int bytesRead = 0;

while((bytesRead = fIn.read(buffer)) != -1) {

lOut.write(buffer,0,bytesRead);

sGen.update(buffer,0,bytesRead);

lOut.flush();

}

// close the streams

lGen.close();

sGen.generate().encode(bOut);

cGen.close();

cOut.close();

out.close();
}
Hello all,
I am using the below code to sign-then-encrypt a file. The customer has provided their public key. I am also generating a RSA Master key pair and a sub-key key pair. I use the Master Private key to sign the file. Now as per the customer,  the file looks to be encrypted properly however it is not signed!!!!
 
I am not able to understand what is wrong with the below code. If you require, I can also forward the code which generates the key pairs.
 
public void signThenEncryptFile(){

PGPPublicKey pgpEncryptionKey = keyManager.retrievePublicKey(pgpBankKeyId); // get bank public key

PGPSecretKey pgpSigningKey = keyManager.retrievePrivateKey(isdpgpkey.getIsdPgpMasterKeyId()); // get the generated Master private key

 

if(pgpEncryptionKey == null || pgpSigningKey == null){

throw new CryptoException("Unable to find the encryption key or the signing key!");

}

 

logger.info("Successfully retrieved the PGP encryption key and signing key");

 

// generator for encrypted objects

PGPEncryptedDataGenerator cPk = new PGPEncryptedDataGenerator(PGPEncryptedData.CAST5, withIntegrityCheck, new SecureRandom(), "BC");

 

// add a public key encrypted session key to the encrypted object

cPk.addMethod(pgpEncryptionKey);

 

// Return an outputstream which will encrypt the data as it is written to it.

// The stream will be written out in chunks according to the size of the passed in buffer.

OutputStream cOut = cPk.open(out, new byte[1 << 16]);

 

if(logger.isDebugEnabled())

logger.debug("Generated an Outputstream with encrypted data");

 

// get the private key

PGPPrivateKey pgpPrivKey = pgpSigningKey.extractPrivateKey(isdpgpkey.getPassPhrase().toCharArray(), "BC");

 

if(logger.isDebugEnabled())

logger.debug("Extracted the private key to be used for signing");

 

// initialize the PGP signature generator

PGPSignatureGenerator sGen = new PGPSignatureGenerator(pgpSigningKey.getPublicKey().getAlgorithm(), PGPUtil.SHA1, "BC");

sGen.initSign(PGPSignature.BINARY_DOCUMENT, pgpPrivKey);

 

if(logger.isDebugEnabled())

logger.debug("Initialized the PGP signature generator");

 

Iterator it = pgpSigningKey.getPublicKey().getUserIDs();

if (it.hasNext())

{

PGPSignatureSubpacketGenerator spGen = new PGPSignatureSubpacketGenerator();

spGen.setSignerUserID(false, (String)it.next());

sGen.setHashedSubpackets(spGen.generate());

}

// generate compressed data packets

PGPCompressedDataGenerator cGen = new PGPCompressedDataGenerator(PGPCompressedData.ZLIB);

 

// return an outputstream which will save the data being written to the compressed object.

BCPGOutputStream bOut = new BCPGOutputStream(cGen.open(cOut));

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

 

if(logger.isDebugEnabled())

logger.debug("Initialized the PGP compressed data generator");

 

//File file = new File(inputFileName);

PGPLiteralDataGenerator lGen = new PGPLiteralDataGenerator();

 

// Open a literal data packet, returning a stream to store the data inside the packet as an indefinite length stream.

// The stream is written out as a series of partial packets with a chunk size determined by the size of the passed in buffer.

OutputStream lOut = lGen.open(bOut, PGPLiteralData.BINARY, inputFileName, new Date(),new byte[1<<20]);

 

if(logger.isDebugEnabled())

logger.debug("Signing the encrypted data");

 

// use of buffering to speed up write

byte[] buffer = new byte[1<<20];

 

FileInputStream fIn = new FileInputStream(file);

int bytesRead = 0;

while((bytesRead = fIn.read(buffer)) != -1) {

lOut.write(buffer,0,bytesRead);

sGen.update(buffer,0,bytesRead);

lOut.flush();

}

// close the streams

lGen.close();

sGen.generate().encode(bOut);

cGen.close();

cOut.close();

out.close();
}
 

Hello all,
I am using the below code to sign-then-encrypt a file. The customer has provided their public key. I am also generating a RSA Master key pair and a sub-key key pair. I use the Master Private key to sign the file. Now as per the customer,  the file looks to be encrypted properly however it is not signed!!!!
 
I am not able to understand what is wrong with the below code. If you require, I can also forward the code which generates the key pairs.
 
public void signThenEncryptFile(){

PGPPublicKey pgpEncryptionKey = keyManager.retrievePublicKey(pgpBankKeyId); // get bank public key

PGPSecretKey pgpSigningKey = keyManager.retrievePrivateKey(isdpgpkey.getIsdPgpMasterKeyId()); // get the generated Master private key

 

if(pgpEncryptionKey == null || pgpSigningKey == null){

throw new CryptoException("Unable to find the encryption key or the signing key!");

}

 

logger.info("Successfully retrieved the PGP encryption key and signing key");

 

// generator for encrypted objects

PGPEncryptedDataGenerator cPk = new PGPEncryptedDataGenerator(PGPEncryptedData.CAST5, withIntegrityCheck, new SecureRandom(), "BC");

 

// add a public key encrypted session key to the encrypted object

cPk.addMethod(pgpEncryptionKey);

 

// Return an outputstream which will encrypt the data as it is written to it.

// The stream will be written out in chunks according to the size of the passed in buffer.

OutputStream cOut = cPk.open(out, new byte[1 << 16]);

 

if(logger.isDebugEnabled())

logger.debug("Generated an Outputstream with encrypted data");

 

// get the private key

PGPPrivateKey pgpPrivKey = pgpSigningKey.extractPrivateKey(isdpgpkey.getPassPhrase().toCharArray(), "BC");

 

if(logger.isDebugEnabled())

logger.debug("Extracted the private key to be used for signing");

 

// initialize the PGP signature generator

PGPSignatureGenerator sGen = new PGPSignatureGenerator(pgpSigningKey.getPublicKey().getAlgorithm(), PGPUtil.SHA1, "BC");

sGen.initSign(PGPSignature.BINARY_DOCUMENT, pgpPrivKey);

 

if(logger.isDebugEnabled())

logger.debug("Initialized the PGP signature generator");

 

Iterator it = pgpSigningKey.getPublicKey().getUserIDs();

if (it.hasNext())

{

PGPSignatureSubpacketGenerator spGen = new PGPSignatureSubpacketGenerator();

spGen.setSignerUserID(false, (String)it.next());

sGen.setHashedSubpackets(spGen.generate());

}

// generate compressed data packets

PGPCompressedDataGenerator cGen = new PGPCompressedDataGenerator(PGPCompressedData.ZLIB);

 

// return an outputstream which will save the data being written to the compressed object.

BCPGOutputStream bOut = new BCPGOutputStream(cGen.open(cOut));

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

 

if(logger.isDebugEnabled())

logger.debug("Initialized the PGP compressed data generator");

 

//File file = new File(inputFileName);

PGPLiteralDataGenerator lGen = new PGPLiteralDataGenerator();

 

// Open a literal data packet, returning a stream to store the data inside the packet as an indefinite length stream.

// The stream is written out as a series of partial packets with a chunk size determined by the size of the passed in buffer.

OutputStream lOut = lGen.open(bOut, PGPLiteralData.BINARY, inputFileName, new Date(),new byte[1<<20]);

 

if(logger.isDebugEnabled())

logger.debug("Signing the encrypted data");

 

// use of buffering to speed up write

byte[] buffer = new byte[1<<20];

 

FileInputStream fIn = new FileInputStream(file);

int bytesRead = 0;

while((bytesRead = fIn.read(buffer)) != -1) {

lOut.write(buffer,0,bytesRead);

sGen.update(buffer,0,bytesRead);

lOut.flush();

}

// close the streams

lGen.close();

sGen.generate().encode(bOut);

cGen.close();

cOut.close();

out.close();
}
 



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值