File encrypted properly but not signed properly!
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();
}
|
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();
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();
}
|