Encryption and Decryption of FileSystem using 3DES

Hi all,

I have a requirement for my project where i need to encrypt and decrypt File Systems, Database Tables using 3DES. I tried to encrypt and decrypt using 3DES, but failing to decrypt non-printable characters . '?' is getting replaced in the place of non-printable characters in decrypted file. Can anybody suggest me how to handle those non-printable characters.

How i am encrypting and decrypting :

1. Generating secretkey using keygenerator

2. creating new instance of IvParameterspec

3. getting new cipher instance with "DESede/CBC/PKCS5Padding"

4. ci.init(encrypt_mode,key,ivspec)

5. encrypting file using cipheroutputstream and fileinputstreams

6. doing same in decryption

files in which i am failing to decrypt properly are pdb,doc,excel files.

one more issue:

I was able to encrypt only files under folders. Is there any way to encrypt folder at a time including sub-folders and hide them after encryption

i was totally struck with these issues.

[1041 byte] By [sun_naa] at [2007-11-27 10:36:05]
# 1

Encryption deals with bytes, not characters so the problem of non-printable characters being placed placed in a file only occurs if you wrongly convert your bytes to characters. You will need to show code if you are to get a more detailed answer.

sabre150a at 2007-7-28 18:38:53 > top of Java-index,Security,Cryptography...
# 2

Hi Sabre thanks for the reply. Sorry i got messed with the user id and i have changed it.

Encryption code

byte [] iv = {(byte) 0x01, (byte) 0x02, (byte) 0x03, (byte) 0x04, (byte) 0x01,(byte) 0x02, (byte) 0x03, (byte) 0x04 };

IvParameterSpec ivspec = new IvParameterSpec(iv);

Here is the requested code of decryption.

DataInputStream dis = new DataInputStream(new FileInputStream("encryptedfile"));

// encrypted file contains IV, so extract it

byte[] iv2 = new byte[8];

dis.read(iv2);

IvParameterSpec ivspec = new IvParameterSpec(iv2);

Cipher ci = getCipherInstance(); // by this step we got cipher instance, ivspec

ci.init(Cipher.DECRYPT_MODE, key, ivspec);

CipherInputStream cis = new CipherInputStream(dis, ci);

FileOutputStream fos = new FileOutputStream(decryptedfile);

int data = 0;

int iCounter = 0;

while ((data = cis.read()) != -1) {

fos.write(data);

iCounter++;

}

cis.close();

fos.close();

dis.close();

Please let me know if need more information.

sun-sona at 2007-7-28 18:38:53 > top of Java-index,Security,Cryptography...
# 3

sorry missed some part of code. Here is the getCipherInstance() method code

Cipher cipherDes = Cipher.getInstance("DESede/CBC/PKCS5Padding");

Thanks

sun-sona at 2007-7-28 18:38:53 > top of Java-index,Security,Cryptography...
# 4

You have only shown a fraction of your code. Five points -

1) dis.read(iv2) does not guarantee to read the whole of the IV. You need to use readFully().

2) I don't understand your use of the IV. You read the IV from the input and then ignore it!

3) You don't show the encryption code. Just because the encryption code compiles and doesn't throw an exception does not mean that it is not the cause of your problems.

4) Reading and writing a byte at a time is very inefficient.

5) You don't need to close dis since it will be closed when you close cis.

sabre150a at 2007-7-28 18:38:53 > top of Java-index,Security,Cryptography...
# 5

Thanks for the reply. I am pretty new to this encryption and decryption. Can you tell me the right usage on ivparameterspec. I have written this as a sample so i catching all exceptions in the main method.

Message was edited by:

sun-son

sun-sona at 2007-7-28 18:38:53 > top of Java-index,Security,Cryptography...
# 6

Thanks for the reply. I am pretty new to this encryption and decryption. Can you tell me the right usage on ivparameterspec.

if the ivparameterspec is not the right usage can u tell me more on the usage

Encryption full code

byte [] iv ={(byte) 0x01, (byte) 0x02, (byte) 0x03,

(byte) 0x04, (byte) 0x01, (byte) 0x02, (byte) 0x03,

(byte) 0x04 };

IvParameterSpec ivspec = new IvParameterSpec(iv);

ci.init(Cipher.ENCRYPT_MODE, key, ivspec);

DataOutputStream output = new DataOutputStream(new FileOutputStream(encryptedfile));

output.write(iv);

CipherOutputStream cos = new CipherOutputStream(output, ci);

FileInputStream fis = new FileInputStream(originalfile);

int data = 0;

while ((data = fis.read()) != -1) {

cos.write(data);

}

fis.close();

cos.close();

Thanks

sun-sona at 2007-7-28 18:38:53 > top of Java-index,Security,Cryptography...
# 7

On decryption you initialize the cipher with the IV you have read from the DataInputStream.

sabre150a at 2007-7-28 18:38:53 > top of Java-index,Security,Cryptography...
# 8

can you tell me how to do if i am not following a proper way..pls......

sun-sona at 2007-7-28 18:38:53 > top of Java-index,Security,Cryptography...
# 9

> can you tell me how to do if i am not following a

> proper way..pls......

See reply #7!

sabre150a at 2007-7-28 18:38:53 > top of Java-index,Security,Cryptography...
# 10

Thanks for the help Sabre. Its working now

sun-sona at 2007-7-28 18:38:53 > top of Java-index,Security,Cryptography...