Discussion:
SMIMEEnveloped null pointer exception
Suleman Butt
2014-08-12 15:16:10 UTC
Permalink
Hi All,

On my reading the smime.p7m, I get the nullpointer exception at :
SMIMEEnveloped env = new SMIMEEnveloped(message);

The method --message.getContentType() produces "text/plain" as output, but
then at the SMIMEEnveloped(message) null pointer exception is raised.

I am using: bcprov-jdk16-1.45 and bcmail-jdk16-1.46.

Security.addProvider(new BouncyCastleProvider());
Properties props = System.getProperties();
Session session = Session.getDefaultInstance(props,
null);

MimeMessage message = new MimeMessage(session, new
FileInputStream("C:/temp/smime.p7m"));
Part strippedMessage = null;
System.out.println("Starting message decryption.." +
message.getContentType());

if (message.isMimeType("application/x-pkcs7-mime") ||
message.isMimeType("application/pkcs7-mime") ||
message.isMimeType("text/plain")){
try {

SMIMEEnveloped env = new
SMIMEEnveloped(message);

....
--
Regards Suleman
David Hook
2014-08-13 01:09:36 UTC
Permalink
This post might be inappropriate. Click to display it.
Suleman
2014-08-13 15:47:28 UTC
Permalink
Post by David Hook
It doesn't sound like it's an EnvelopedData message. The
NullPointerException indicates the API is unable to find any content.
Exactly what is in the message?
Regards,
David
Post by Suleman Butt
Hi All,
SMIMEEnveloped env = new SMIMEEnveloped(message);
The method --message.getContentType() produces "text/plain" as output,
but then at the SMIMEEnveloped(message) null pointer exception is raised.
I am using: bcprov-jdk16-1.45 and bcmail-jdk16-1.46.
Security.addProvider(new BouncyCastleProvider());
Properties props = System.getProperties();
Session session =
Session.getDefaultInstance(props, null);
MimeMessage message = new MimeMessage(session, new
FileInputStream("C:/temp/smime.p7m"));
Part strippedMessage = null;
System.out.println("Starting message decryption.."
+ message.getContentType());
if (message.isMimeType("application/x-pkcs7-mime")
|| message.isMimeType("application/pkcs7-mime") ||
message.isMimeType("text/plain")){
try {
SMIMEEnveloped env = new
SMIMEEnveloped(message);
....
--
Regards Suleman
[CODE]
I finally managed to get it working through CMSEnvelopedDataParser.

Here is the snippet:

CMSEnvelopedDataParser ep = new CMSEnvelopedDataParser(
new FileInputStream(encryptFile));

RecipientInformationStore recipients = ep.getRecipientInfos();

Collection<RecipientInformation> c = recipients.getRecipients();
Iterator<RecipientInformation> it = c.iterator();

if (it.hasNext()) {
RecipientInformation recipient = (RecipientInformation) it
.next();

// byte[] decryptedData = recipient.getContent(privateKey,new
// BouncyCastleProvider());
byte[] decryptedData = recipient
.getContent(new JceKeyTransEnvelopedRecipient(
privateKey).setProvider("BC"));

InputStream decryptedInputStream = new ByteArrayInputStream(
decryptedData);

BASE64Decoder decoder = new BASE64Decoder();
byte[] decodedBytes = decoder
.decodeBuffer(decryptedInputStream);

decryptedInputStream = new ByteArrayInputStream(decodedBytes);

BufferedReader reader = new BufferedReader(
new InputStreamReader(decryptedInputStream));
StringBuilder out = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
out.append(line);
}
System.out.println(out.toString()); // Prints the string content
// read from input stream
reader.close();

PrintWriter outP = new PrintWriter(decryptFile);

outP.println(out.toString());

}


[/CODE]

Loading...