Skip to Main Content

Java Security

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

encrypting & decrypting file

843810Jan 24 2003 — edited Jan 25 2003
I am usually pretty good at looking at the classes and figuring stuff out but the whole cryptology approach is confusing to me.

1) I am trying to encrypt a file place it in a directory (where it is then automatically emailed to a location)

2) decrypt the file and place it another dirctory

So far i've encrypted my data but not sure what to do from here (partial code below)

Do I need to generate public and private keys on the host and client side?

I would appreciate if someone could write me some pseudo code on what steps at leas I should try to be talking to accomplish this


File fileIn = null;
FileReader inputReader = null;

File fileOut = null;
FileOutputStream outputWriter = null;
CipherOutputStream outputCrypt = null;

try {
KeyGenerator keygen = KeyGenerator.getInstance("DES");
SecretKey desKey = keygen.generateKey();

Cipher desCipher;

// Create the cipher
desCipher = Cipher.getInstance("DES/ECB/PKCS5Padding");

// Initialize the cipher for encryption
desCipher.init(Cipher.ENCRYPT_MODE, desKey);

fileIn = new File("c:\\downloads\\state1.txt");
inputReader = new FileReader(fileIn);

fileOut = new File("c:\\downloads\\myNewFile.txt");
outputWriter = new FileOutputStream(fileOut);
outputCrypt = new CipherOutputStream(outputWriter, desCipher);

//System.out.println(inputReader.read());
int r;
Integer myValues = null;
String stringValues = null;
while ((r = inputReader.read()) != -1) {
myValues = new Integer(inputReader.read());
stringValues = myValues.toString();
System.out.println(stringValues);
outputCrypt.write(inputReader.read());
// System.out.print(inputReader.read());
}
}

catch (IOException ex) {
System.out.println("A File Exception occured: " +ex);
}

catch (NoSuchAlgorithmException ex) {
System.out.println("A File Exception occured: " +ex);
}

catch (NoSuchPaddingException ex) {
System.out.println("A File Exception occured: " +ex);
}

catch (InvalidKeyException ex) {
System.out.println("A File Exception occured: " +ex);
}

finally {
try {
inputReader.close();
outputWriter.close();
outputCrypt.close();
}

catch (IOException ex) {
System.out.println("Could not close files exception: " +ex);
}
}

Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 22 2003
Added on Jan 24 2003
1 comment
509 views