Results 1 to 5 of 5
- 04-23-2010, 10:44 AM #1
Member
- Join Date
- Apr 2010
- Posts
- 19
- Rep Power
- 0
- 04-23-2010, 10:48 AM #2
Please post the code so that we can correct you.
Ramya:cool:
- 04-23-2010, 11:10 AM #3
Member
- Join Date
- Apr 2010
- Posts
- 19
- Rep Power
- 0
Hi,
below is the code...
i am doing encryption and decryption
import java.io.DataInputStream;
import java.io.File;
import java.io.FileReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.security.InvalidKeyException;
import java.security.Key;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
public class LocalEncrypter {
private static String algorithm = "DESede";
private static Key key = null;
private static Cipher cipher = null;
private static void setUp() throws Exception {
key = KeyGenerator.getInstance(algorithm).generateKey();
cipher = Cipher.getInstance(algorithm);
}
public static void main(String[] args)
throws Exception {
setUp();
if (args.length !=1) {
System.out.println(
"USAGE: java LocalEncrypter " +
"[String]");
System.exit(1);
}
File keyFile= new File("c:\harsha\javatry\data.txt");
DataInputStream in = new DataInputStream(new FileInputStream(keyFile));
byte[] rawkey = new byte[(int) keyFile.length()];
in.readFully(rawkey);
in.close();
DESedeKeySpec keyspec = new DESedeKeySpec(rawkey);
SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("DESede");
SecretKey key = keyfactory.generateSecret(keyspec);
//encryption
byte[] encryptionBytes = null;
encryptionBytes = encrypt(keyFile);
FileOutputStream out= new FileOutputStream("c:\harsha\javatry\Encrypt.txt");
FileOutputStream out1=new FileOutputStream("Key.txt");
PrintStream pr= new PrintStream(out);
pr.println(encryptionBytes);
pr=new PrintStream(out1);
pr.println(key);
pr.close();
//decryption
String decryp=decrypt(encryptionBytes);
FileOutputStream out2= new FileOutputStream("c:\harsha\javatry\Decrypt.csv");
PrintStream pr1=new PrintStream(out2);
pr1.println(decryp);
pr1.close();
}
private static byte[] encrypt(File keyF)
throws InvalidKeyException,
BadPaddingException,
IllegalBlockSizeException,IOException {
DataInputStream in = new DataInputStream(new FileInputStream(keyF));
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] encrData = new byte[(int) keyF.length()];
in.readFully(encrData);
in.close();
return cipher.doFinal(encrData);
}
private static String decrypt(byte[] encrBytes)
throws InvalidKeyException,
BadPaddingException,
IllegalBlockSizeException, IOException{
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] decrBytes = cipher.doFinal(encrBytes);
String recovered =new String(decrBytes);
return recovered;
//return cipher.doFinal(encryptionBytes);
}
}
regards,
harsha
- 04-23-2010, 11:39 AM #4
- 04-23-2010, 11:48 AM #5
Member
- Join Date
- Apr 2010
- Posts
- 19
- Rep Power
- 0
Similar Threads
-
File reading / writing
By MattBSibley in forum New To JavaReplies: 5Last Post: 04-19-2010, 05:20 AM -
reading file from a path
By new_coder in forum New To JavaReplies: 5Last Post: 08-17-2009, 04:52 AM -
Reading and writing to a file
By jigglywiggly in forum New To JavaReplies: 13Last Post: 03-09-2009, 10:44 AM -
Reading/Writing to file
By Doctor Cactus in forum New To JavaReplies: 2Last Post: 10-28-2008, 02:05 PM -
Help with File reading and writing
By baltimore in forum New To JavaReplies: 1Last Post: 07-31-2007, 06:47 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks