Lkun Lkun - 9 days ago 6
Java Question

Byte encryption for text file

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 = myFile.read(buffer);
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) {
super();
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 {
write(text.getBytes());
}


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


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

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

}

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

}

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

return is.read(buffer);
}

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


}

}


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

Answer

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:

Common

int shift = 11;

Writing

// 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.
...

Reading

// 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 192.168.0.1
Cipher  : T+nly+}plo+st~+T[+<D=9<AC9;9<
Decipher: I can read this IP 192.168.0.1

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.