Quantcast
Channel: Active questions tagged utf-8 - Stack Overflow
Viewing all articles
Browse latest Browse all 1064

Base64.Decoder returning foreign characters

$
0
0

I am building a small application to turn the text in a text file to Base64 then back to normal. The decoded text always returns some Chinese characters in the beginning of the first line.

public EncryptionEngine(File appFile){    this.appFile= appFile;}public void encrypt(){    try {        byte[] fileText = Files.readAllBytes(appFile.toPath());// get file text as bytes        Base64.Encoder encoder = Base64.getEncoder();        PrintWriter writer = new PrintWriter(appFile);        writer.print("");//erase old, readable text        writer.print(encoder.encodeToString(fileText));// insert encoded text        writer.close();    } catch (IOException e) {        e.printStackTrace();    }}public void deycrpt(){    try {        byte[] fileText = Files.readAllBytes(appFile.toPath());        String s = new String (fileText, StandardCharsets.UTF_8);//String s = new String (fileText);        Base64.Decoder decoder = Base64.getDecoder();        byte[] decodedByteArray = decoder.decode(s);        PrintWriter writer = new PrintWriter(appFile);        writer.print("");        writer.print(new String (decodedByteArray,StandardCharsets.UTF_8)); //writer.print(new String (decodedByteArray));        writer.close();    } catch (IOException e) {        e.printStackTrace();    }}

Text FileBefore before encrypt():

cheese

tomatoes

potatoes

hams

yams

Text File after encrypt()//5jAGgAZQBlAHMAZQANAAoAdABvAG0AYQB0AG8AZQBzAA0ACgBwAG8AdABhAHQAbwBlAHMADQAKAGgAYQBtAHMADQAKAHkAYQBtAHMA

Text File After decrypt

뿯붿cheese

tomatoes

potatoes

hams

yams

Before encrypt() :

After decrypt() :


Viewing all articles
Browse latest Browse all 1064

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>