62

My java application use base64 encoding which puts a new line (\n) after every 76 character. I need to put this encoded string in a properties file and the newline breaks the functionality.

When I do a encodedString.replaceAll("\n", ""); things are working fine, but I just want to make sure that this is expected and I am not introducing a hidden issue.

4
  • similar question stackoverflow.com/questions/5110731/… Commented Jun 13, 2016 at 4:10
  • If you are using java.util.Properties to save the value it should handle newlines correctly. Commented Aug 22, 2017 at 17:50
  • I have same problem with Poco::Base64Encoder you need to use option Poco::Base64Encoder encoder{encoded, Poco::BASE64_URL_ENCODING};
    – Gelldur
    Commented Feb 26, 2018 at 15:23
  • You can use Base64.encodeToString(decodedData,Base64.NO_WRAP), NO_WRAP will generated the encoded data without CRLF
    – Rajan
    Commented Jul 19, 2019 at 7:07

5 Answers 5

63

Breaking a base64 encoded string into multiple lines has been necessary for many old programs that couldn't handle long lines. Programs written in Java can usually handle long lines since they don't need to do the memory management themselves. As long as your lines are shorter than 64 million characters there should be no problem.

And since you don't need the newlines, you shouldn't generate them at all, if possible.

5
  • llling, in android they are generated automatically how can I prevent this. Commented Jan 7, 2015 at 12:15
  • 32
    @Pankaj_Nimgade Use the Base64.NO_WRAP flag.
    – Intrepidis
    Commented Apr 8, 2015 at 8:02
  • 3
    Update: in Java 8, use java.util.Base64. Basic and URL and Filename safe encoders do not add line separators. Details here
    – Bazi
    Commented May 19, 2016 at 10:52
  • @ChrisNash - your solution helped me... It worked for me
    – Jigar
    Commented Sep 26, 2016 at 8:30
  • 3
    To decode a line break contained string, java.util.Base64.getMimeDecoder().decode(message) should be used.
    – mavis
    Commented Sep 8, 2020 at 9:24
14

Some of the Base64 encoders append EOL characters like CRLF ('\r\n') to the encoded strings. You can use Base64.encodeBase64URLSafe to get rid of them:

Encodes binary data using a URL-safe variation of the base64 algorithm but does not chunk the output. The url-safe variation emits - and _ instead of + and / characters. Note: no padding is added.

1
8

You just need to Use Base64 encoding in following way

Base64.encodeBase64String("Your data to encrypt in base64")

Change above line with the followings

Base64.encodeBase64String("Your data to encrypt in base64",Base64.NO_WRAP)

This will solve your problem.

1
  • 1
    Maybe this works in Java, but in PERL, it only works ... to an extent. It just corrupts the output string where the linebreak was: " ... XBhb2RlBase64NO_WRAPdGVyMjAw ..." Commented Mar 6, 2021 at 11:16
0

encodedString.replaceAll("\n", "");

DOES introduce a 'hidden issue' in the Java Mime encoder, and I just had to fight around it.

The lines are separated by "\r\n", not just "\n", so your replace leaves the "\r" in places, and those are ignored by most, but not all programs. For example, I was using the GitHub API where I had to generate JSON containing the base42-mime encoding for a file, and I was simply adding (") before and after a encodedString.replaceAll("\n", "") string... well.. it did not work because of the leftover \r...

1
  • 1
    replaceAll means the first argument is a regular expression. So, just make that .replaceAll("\\R", ""). \\R is regexp-ese for 'newline stuff'. It'll take care of all combinations of \r and \n that can show up. If you just want to replace literals, .replace replaces all. (Yes, replaceAll and replace are really bad names for these methods. Nevertheless, z.replace()` replaces all - it's just that replaceAll takes a regexp pattern, and replace just does it literally). Commented Sep 29, 2023 at 15:15
-3

It should not be an issue since many decoders are able to decode the encoded text without the newline delimiter. Safest option is to do the decoding yourself and verify it.

Not the answer you're looking for? Browse other questions tagged or ask your own question.