Discussion:
Error in encoded stream: needed at least 2 valid base64 characters, but only got 0 before padding character (=)
VTARIN
2008-10-10 06:55:22 UTC
Permalink
Hello!

Im trying to sign an existing mime (file
http://www.nabble.com/file/p19775520/mime5.mim mime5.mim
), but i get an error when the message has a binary part.
Im using bcmail-jdk14-139.jar and mail-1.4.1.jar
This is my code:

InputStream is = new FileInputStream("mime5.mim");
setMimeIn(new MimeBodyPart(is));
.....

.....
MimeMultipart mm = null;
mm = gen.generate((MimeBodyPart) getMimeIn(),
BouncyCastleProvider.PROVIDER_NAME);

Properties props = System.getProperties();
Session session = Session.getDefaultInstance(props, null);
mime = new MimeMessage(session);
mime.setContent(mm, mm.getContentType());
mime.saveChanges();
mime.writeTo(new
FileOutputStream(getSignatureProperty(FILE_OUTPUT)));


And this is the error i get:


java.io.IOException: Error in encoded stream: needed at least 2 valid base64
characters, but only got 0 before padding character (=), the 7 most recent
characters were: "------="
at
com.sun.mail.util.BASE64DecoderStream.decode(BASE64DecoderStream.java:259)
at com.sun.mail.util.BASE64DecoderStream.read(BASE64DecoderStream.java:148)
at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:264)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:306)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:158)
at java.io.InputStreamReader.read(InputStreamReader.java:167)
at com.sun.mail.handlers.text_plain.getContent(text_plain.java:113)
at
javax.activation.DataSourceDataContentHandler.getContent(DataHandler.java:774)
at javax.activation.DataHandler.getContent(DataHandler.java:521)
at javax.mail.internet.MimeBodyPart.getContent(MimeBodyPart.java:652)
at
org.bouncycastle.mail.smime.handlers.multipart_signed.outputBodyPart(Unknown
Source)
at
org.bouncycastle.mail.smime.handlers.multipart_signed.outputBodyPart(Unknown
Source)
at org.bouncycastle.mail.smime.handlers.multipart_signed.writeTo(Unknown
Source)
at javax.activation.ObjectDataContentHandler.writeTo(DataHandler.java:868)
at javax.activation.DataHandler.writeTo(DataHandler.java:301)
at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:1403)
at javax.mail.internet.MimeMessage.writeTo(MimeMessage.java:1745)
at javax.mail.internet.MimeMessage.writeTo(MimeMessage.java:1721)
--
View this message in context: http://www.nabble.com/Error-in-encoded-stream%3A-needed-at-least-2-valid-base64-characters%2C-but-only-got-0-before-padding-character-%28%3D%29-tp19775520p19775520.html
Sent from the Bouncy Castle - Dev mailing list archive at Nabble.com.
David Hook
2008-10-10 08:24:36 UTC
Permalink
I don't think the multipart is being created correctly, I think JavaMail
is assuming it's text/plain containing the text of the file.

Try:

MimeMultipart mMsg = new MimeMultipart(new
FileDataSource("mime5.mim"));
MimeBodyPart msg = new MimeBodyPart();

msg.setContent(mMsg);

Where msg would be what's returned by getMimeIn()

Regards,

David
Hello! Im trying to sign an existing mime (file mime5.mim ), but i get
an error when the message has a binary part. Im using
InputStream is = new FileInputStream("mime5.mim");
setMimeIn(new MimeBodyPart(is));
.....
.....
MimeMultipart mm = null;
mm = gen.generate((MimeBodyPart) getMimeIn(), BouncyCastleProvider.PROVIDER_NAME);
Properties props = System.getProperties();
Session session = Session.getDefaultInstance(props, null);
mime = new MimeMessage(session);
mime.setContent(mm, mm.getContentType());
mime.saveChanges();
mime.writeTo(new FileOutputStream(getSignatureProperty(FILE_OUTPUT)));
java.io.IOException: Error in encoded stream: needed at least 2 valid base64 characters, but only got 0 before padding character (=), the 7 most recent characters were: "------="
at com.sun.mail.util.BASE64DecoderStream.decode(BASE64DecoderStream.java:259)
at com.sun.mail.util.BASE64DecoderStream.read(BASE64DecoderStream.java:148)
at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:264)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:306)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:158)
at java.io.InputStreamReader.read(InputStreamReader.java:167)
at com.sun.mail.handlers.text_plain.getContent(text_plain.java:113)
at javax.activation.DataSourceDataContentHandler.getContent(DataHandler.java:774)
at javax.activation.DataHandler.getContent(DataHandler.java:521)
at javax.mail.internet.MimeBodyPart.getContent(MimeBodyPart.java:652)
at org.bouncycastle.mail.smime.handlers.multipart_signed.outputBodyPart(Unknown Source)
at org.bouncycastle.mail.smime.handlers.multipart_signed.outputBodyPart(Unknown Source)
at org.bouncycastle.mail.smime.handlers.multipart_signed.writeTo(Unknown Source)
at javax.activation.ObjectDataContentHandler.writeTo(DataHandler.java:868)
at javax.activation.DataHandler.writeTo(DataHandler.java:301)
at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:1403)
at javax.mail.internet.MimeMessage.writeTo(MimeMessage.java:1745)
at javax.mail.internet.MimeMessage.writeTo(MimeMessage.java:1721)
______________________________________________________________________
View this message in context: Error in encoded stream: needed at least
2 valid base64 characters, but only got 0 before padding character (=)
Sent from the Bouncy Castle - Dev mailing list archive at Nabble.com.
David Hook
2008-10-10 08:30:05 UTC
Permalink
Actually the other thing I just noticed. There isn't any multipart
header at the top of the file - this really is just a single body part.

You might just be missing the multipart header.

Regards,

David
Post by David Hook
I don't think the multipart is being created correctly, I think JavaMail
is assuming it's text/plain containing the text of the file.
MimeMultipart mMsg = new MimeMultipart(new
FileDataSource("mime5.mim"));
MimeBodyPart msg = new MimeBodyPart();
msg.setContent(mMsg);
Where msg would be what's returned by getMimeIn()
Regards,
David
Hello! Im trying to sign an existing mime (file mime5.mim ), but i get
an error when the message has a binary part. Im using
InputStream is = new FileInputStream("mime5.mim");
setMimeIn(new MimeBodyPart(is));
.....
.....
MimeMultipart mm = null;
mm = gen.generate((MimeBodyPart) getMimeIn(), BouncyCastleProvider.PROVIDER_NAME);
Properties props = System.getProperties();
Session session = Session.getDefaultInstance(props, null);
mime = new MimeMessage(session);
mime.setContent(mm, mm.getContentType());
mime.saveChanges();
mime.writeTo(new FileOutputStream(getSignatureProperty(FILE_OUTPUT)));
java.io.IOException: Error in encoded stream: needed at least 2 valid base64 characters, but only got 0 before padding character (=), the 7 most recent characters were: "------="
at com.sun.mail.util.BASE64DecoderStream.decode(BASE64DecoderStream.java:259)
at com.sun.mail.util.BASE64DecoderStream.read(BASE64DecoderStream.java:148)
at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:264)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:306)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:158)
at java.io.InputStreamReader.read(InputStreamReader.java:167)
at com.sun.mail.handlers.text_plain.getContent(text_plain.java:113)
at javax.activation.DataSourceDataContentHandler.getContent(DataHandler.java:774)
at javax.activation.DataHandler.getContent(DataHandler.java:521)
at javax.mail.internet.MimeBodyPart.getContent(MimeBodyPart.java:652)
at org.bouncycastle.mail.smime.handlers.multipart_signed.outputBodyPart(Unknown Source)
at org.bouncycastle.mail.smime.handlers.multipart_signed.outputBodyPart(Unknown Source)
at org.bouncycastle.mail.smime.handlers.multipart_signed.writeTo(Unknown Source)
at javax.activation.ObjectDataContentHandler.writeTo(DataHandler.java:868)
at javax.activation.DataHandler.writeTo(DataHandler.java:301)
at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:1403)
at javax.mail.internet.MimeMessage.writeTo(MimeMessage.java:1745)
at javax.mail.internet.MimeMessage.writeTo(MimeMessage.java:1721)
______________________________________________________________________
View this message in context: Error in encoded stream: needed at least
2 valid base64 characters, but only got 0 before padding character (=)
Sent from the Bouncy Castle - Dev mailing list archive at Nabble.com.
Loading...