So I have a program that is supposed to encode UTF-8 Base64, but is not. My Encoder code is as follows:
public class Encoder {
public static void Encode() throws IOException{
byte[] bytes = Base64.encodeBase64(readFile("C:\\Users\\Dragon\\Desktop\\Binary\\Diamond.png", StandardCharsets.UTF_8).getBytes(StandardCharsets.UTF_8));
String binary = new String(bytes);
PrintWriter out = new PrintWriter("C:\\Users\\Dragon\\Desktop\\Binary\\Base64.txt");
out.println(binary);
out.close();
System.out.println("Your File has been saved at C:\\Users\\Dragon\\Desktop\\Binary\\Base64.txt");
}
static String readFile(String path, Charset encoding)
throws IOException
{
byte[] encoded = Files.readAllBytes(Paths.get(path));
return new String(encoded, encoding);
}
}
And when I decode it with this website (That has UTF-8 Functionality): http://www.base64decode.org/ I get: Input (My code made this):
77+9UE5HDQoaCgAAAA1JSERSAAAAEAAAABAIBgAAAB/vv73vv71hAAAAGHRFWHRBdXRob3IAbWluZWNyYWZ0aW5mby5jb23vv71mJ0sAAADvv71JREFUOO+/vWPvv73vv73vv70/AyUY77+9QAM877+9Bu+/ve+/vTHvv708XgNAGu+/vQHvv70hOA3vv71pXu+/ve+/vTkcX++/ve+/ve+/ve+/ve+/vSjvv71gNQDvv70Z77+9Ae+/vRkE77+977+9XgRj77+9De+/vWkEGQTvv70gA++/vV3vv71hAO+/ve+/vS/vv71Q77+977+977+9RhRX77+9NQBkIy4DYO+/ve+/vWkASO+/ve+/vdmNWA3vv73vv73vv70GY++/vQbvv70CDO+/vQEZ77+9DAVhEBvvv70B77+9QARpBu+/vQLvv70A77+9MDHvv71hADMA77+9R1wG77+9bCfvv73vv73vv73vv73vv73vv71077+977+9Mjbvv703KVPvv70b77+977+9AB0QYe+/vV4VTO+/vQAAAABJRU5E77+9QmDvv70=
Expected output from website:
‰PNG
IHDR óÿa tEXtAuthor minecraftinfo.comßf'K ²IDAT8Ëcøÿÿ?%Á@<æÿ‘1º<^@к!8
€i^ôí9_üõù¿Áí½(†`5 ¦¤¦¼Þ^c¢
€ià ]a ºæž/P°ÂÔFWà5 d#.`®Ài H³ÎÙX
®Îc¼À¤ƒa«°@iÙÓ Ã01œa 3 äG\Àl'˜Œît¢“26Œ7)SœÉÅ aÍ^L‹ IEND®B`‚
Actual output From website:
�PNG
IHDR��atEXtAuthorminecraftinfo.com�f'K�IDAT8�c���?%�@<���1�<^@��!8
�i^��9_�����(�`5�����^c�
�i� �]�a��/�P���FW�5d#.`��iH��ٍX
���c����a��@i���01�a3�G\�l'������t��26�7)S���a�^L�IEND�B`�
However, when I encode the same exact thing using this site and not my code, It DOES have UTF-8 Formatting and decodes correctly. So what am I doing wrong?
You Base64.encodeBase64
take byte[]
as input. You should pass result of Files.readAllBytes(Paths.get(path))
to Base64.encodeBase64
directly.