Lkun Lkun - 1 month ago 20
Java Question

Byte Encryption for Txt File - Solved

Active reader, first time asking.
I have an app in which I have to read a .txt file so that I can store some values and keep them. This is working pretty well, except for the fact that I want to make those values non-readable or "non-understandable" for external users.
My idea was to convert the file content into Hex or Binary and, in the reading process, change it back to Char. The thing is that I don't have access to methods such as String.Format due to my compiler.

Here's how I'm currently reading and keeping the values:

byte[] buffer = new byte[1024];
int len =;
String data = null;
int i=0;
data = new String(buffer,0,len);

Class to open and manipulate the file:

public class File {
private boolean debug = false;
private FileConnection fc = null;
private OutputStream os = null;
private InputStream is = null;
private String fileName = "example.txt";
private String pathName = "logs/";
final String rootName = "file:///a:/";

public File(String fileName, String pathName) {
this.fileName = fileName;
this.pathName = pathName;
if (!pathName.endsWith("/")) {
this.pathName += "/"; // add a slash

public boolean isDebug() {
return debug;

public void setDebug(boolean debug) {
this.debug = debug;

public void write(String text) throws IOException {

public void write(byte[] bytes) throws IOException {
if (debug)
System.out.println(new String(bytes));

private FileConnection getFileConnection() throws IOException {
// check if subfolder exists
fc = (FileConnection) + pathName);
if (!fc.exists() || !fc.isDirectory()) {
if (debug)
System.out.println("Dir created");
// open file
fc = (FileConnection) + pathName + fileName);
if (!fc.exists())
return fc;

* release resources
public void close() {
if (is != null)
try {
} catch (IOException e) {
is = null;
if (os != null)
try {
} catch (IOException e) {
os = null;
if (fc != null)
try {
} catch (IOException e) {
fc = null;


public void open(boolean writeAppend) throws IOException {
fc = getFileConnection();
if (!writeAppend)
is = fc.openInputStream();
os = fc.openOutputStream(fc.fileSize());


public int read(byte[] buffer) throws IOException {


public void delete() throws IOException {
fc = (FileConnection) + pathName + fileName);
if (fc.exists())



I would like to know a simple way on how to read this content. Binary or Hex, both would work for me.


So, with some understanding of the question, I believe you're really looking for a form of obfuscation? As mentioned in the comments, the easiest way to do this is likely a form of cipher.

Consider this example implementation of a shift cipher:


int shift = 11;


// Get the data to be wrote to file.
String data = ...

// cipher the data.
char[] chars = data.toCharArray();
for (int i = 0; i < chars.length; ++i) {
    chars[i] = (char)(chars[i] + shift);
String cipher = new String(chars);

// Write the data to the cipher file.


// Read the cipher file.
String data = ...

// Decipher the data.
char[] chars = data.toCharArray();
for (int i = 0; i < chars.length; ++i) {
    chars[i] = (char)(chars[i] - shift);
String decipher = new String(chars);

// Use data as required.

Here's an example implementation on Ideone. The output:

Data    : I can read this IP
Cipher  : T+nly+}plo+st~+T[+<D=9<AC9;9<
Decipher: I can read this IP

I tried to keep this as low level as possible in order to satisfy the Java 3 requirement.

Note that this is NOT secure by any means. Shift ciphers (like most ciphers in a bubble) are trivial to break by malicious entities. Please do not use this if security is an actual concern.