Skip to main content

SecretKey I/O

1 reply [Last post]
thotheolh
Offline
Joined: 2007-12-28

Hi, I am just starting to learn abit of cryptography in Java on myself because my client would want me to implement some encryption on my program using Blowfish. I managed to get the Blowfish encryption and decryption working but I would like to write the SecretKey to a text file like .txt format and then read it when required. Many programs using encryption writes their key to some text file and read it from that place and I would like to follow. I have a dead line left of 1 week and I would like some help.

Below are my codes...

FileIO.java // handles all sorts of I/O i need...

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
*
* @author Owner
*/
public class FileIO {

File file;
StringBuffer data;

public StringBuffer read(String filepath) throws FileNotFoundException, IOException {
BufferedReader br;

file = new File(filepath);
br = new BufferedReader(new FileReader(file));
data = new StringBuffer();
boolean endOfFile = false;
String fragment = "";
while (!endOfFile) {
fragment = br.readLine();
if (fragment == null) {
endOfFile = true;

} else {
data.append(fragment);
}

}
br.close();

return data;
}

public StringBuffer read(URL filepath) throws FileNotFoundException, IOException {
BufferedReader br;

file = new File(filepath.toString());
br = new BufferedReader(new FileReader(file));
data = new StringBuffer();
boolean endOfFile = false;
String fragment = "";
while (!endOfFile) {
fragment = br.readLine();
data.append(fragment);
if (fragment == null) {
endOfFile = true;
}

}
br.close();

return data;
}

public boolean write(String filepath, StringBuffer data) throws IOException {
boolean ok = false;
FileWriter fw = null;
PrintWriter pw = null;
file = new File(filepath);
fw = new FileWriter(file);
pw = new PrintWriter(fw);
pw.println(data.toString());

ok = true;

fw.close();
pw.close();
return ok;
}

public boolean deleteFile(String filepath) {
boolean ok = false;

file = new File(filepath);
if (file.exists()) {
ok = file.delete();

} else {
}

return ok;
}

public byte[] readByte(String filepath) throws FileNotFoundException, IOException, Exception {
FileInputStream fis = null;
int c = -1;
byte[] b = null;
int fSize = -1;
File InFile = null;

InFile = new File(filepath);

if (!InFile.exists()) {
throw new Exception("File Not Found");
}

fSize = (int) InFile.length();

try {

fis = new FileInputStream(InFile);
int avai = fis.available();

if (fSize == avai) {
b = new byte[fSize];
for (int i = 0; (c = fis.read()) != -1; i++) {
b[i] = (byte) c;
}
}

} catch (Exception exp) {

exp.printStackTrace();

} finally {

if (fis != null) {
fis.close();
}

fis = null;

}

return b;

}

public boolean writeByte(String filepath, byte[] b) {
boolean ok = false;
FileOutputStream fos = null;
try {

fos = new FileOutputStream(filepath);
fos.write(b);

fos.close();

} catch (IOException ex) {
Logger.getLogger(FileIO.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
fos.close();
} catch (IOException ex) {
Logger.getLogger(FileIO.class.getName()).log(Level.SEVERE, null, ex);
}
}
return ok;
}
}

Main method...

FileIO io = new FileIO();
SecretKey key = KeyGenerator.getInstance("Blowfish").generateKey();
byte[] keyArray = key.getEncoded();
io.writeByte("data/key.txt", keyArray);
System.out.println("Key save: "+keyArray);
byte[] keyArray1 = io.readByte("data/key.txt");
SecretKeySpec key1 = new SecretKeySpec(keyArray1,"Blowfish");
System.out.println("Key retrieve: "+keyArray1);

I decided to test my writeBytes and readBytes for writing my SecretKey to and from my file called key.txt.

The println results...

Key save: [B@16f8cd0
Key retrieve: [B@85af80

I get two different results from the save and retrieve. Are my codes fine or is it totally wrong ? Please suggest some solutions to fix any errors.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
tarbo
Offline
Joined: 2006-12-18

The string representations aren't the same because the keys aren't stored in the same memory address, which is the case because you return a new byte array. If you wish to check two arrays for equality, consider using [url=http://java.sun.com/javase/6/docs/api/java/util/Arrays.html]java.util.Arrays.equals[/url].