Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it ok to remove newline in Base64 encoding

Tags:

java

base64

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.

like image 799
Atul Soman Avatar asked Nov 13 '13 11:11

Atul Soman


People also ask

Can Base64 have line breaks?

RFC 2045, which defined Base64, REQUIRES a newline after 76 characters (max). What makes you think your example is not the correct way? @MSalters RFC 4648 specifically addresses that issue.

Is Base64 padding necessary?

Need for Padding in Base64:It is mandatory for a proper conversion into Base64 that the resulting data must be converted into sequences of 24 bits each. However, at times, it happens that this length is not satisfied, i.e., a few bits might not be there, or the total bits of the encoded data are fewer than 24.

Does Base64 reduce quality?

Encoding to/from Base64 is completely lossless. The quality loss happens probably when you save it. To prevent that, use an ImageWriter directly ( ImageIO.

What characters are allowed in Base64?

Base64 only contains A–Z , a–z , 0–9 , + , / and = . So the list of characters not to be used is: all possible characters minus the ones mentioned above. For special purposes .


1 Answers

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.

like image 88
Roland Illig Avatar answered Oct 05 '22 12:10

Roland Illig