Simple encryption... need help...

hello friends,

this may be a simple problem, but, as i m new plz forgive me...

i have to accept three command line agrumetns like...

name > string

age > integer

date > string

now, i want to encrypt these data in a two different text files with two different encryption methods...

and later i need to decrypt the data from both the textfiles...

can anyone help me how can i achieve this task ?

Thanks,

Rohan

[482 byte] By [Doct0ra] at [2007-11-27 5:53:34]
# 1

Rohan,

Before asking a fundamental question like this, you should read the JCA documentation that comes with the JDK (http://java.sun.com/javase/6/docs/technotes/guides/security/crypto/CryptoSpec.html) and/or read a book on the subject that will not only provide you the answer, but educate you on cryptography and its subtleties (David Hook's book on "Beginning Cryptography with Java" is an excellent beginning).

Doing anything less is asking to be spoon-fed.

arshad.noora at 2007-7-12 15:46:52 > top of Java-index,Security,Cryptography...
# 2

hi arshad,

thanks for the guidence...

i just needed the proper direction... not the code...

Thanks again...

i went through some books and tried a sample code from a book...

here is the code...

/**

*

*/

package des;

import java.io.*;

import java.security.*;

import javax.crypto.*;

import sun.misc.*;

/**

* @author Rohan.Nakar

*

*/

public class SecretWriting

{

public static void main(String[] args) throws Exception

{

//checking argumetns

if(args.length < 2)

{

System.out.println("Usage: SecureWriting -e | -d plainText");

return;

}

//get or create key

Key key;

try

{

ObjectInputStream in = new ObjectInputStream( new FileInputStream("SecretKey.ser"));

key = (Key)in.readObject();

in.close();

}

catch (FileNotFoundException fnfe)

{

KeyGenerator generator = KeyGenerator.getInstance("DES");

generator.init(new SecureRandom());

key = generator.generateKey();

ObjectOutputStream out = new ObjectOutputStream( new FileOutputStream("SecretKey.ser"));

out.writeObject(key);

out.close();

}

//get a cipher object

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

//Encrypt or decrypt the input string

if (args[0].indexOf("e") != -1)

{

cipher.init(Cipher.ENCRYPT_MODE, key);

String amalgam = args[1];

for(int i = 2; i < args.length; i++)

amalgam += " " + args[i];

byte[] stringBytes = amalgam.getBytes("UTF8");

byte[] raw = cipher.doFinal(stringBytes);

BASE64Encoder encoder = new BASE64Encoder();

String base64 = encoder.encode(raw);

System.out.println(base64);

}

else if (args[0].indexOf("d") != -1)

{

cipher.init(Cipher.DECRYPT_MODE, key);

BASE64Decoder decoder = new BASE64Decoder();

byte[] raw = decoder.decodeBuffer(args[1]);

byte[] stringBytes = cipher.doFinal(raw);

String result = new String(stringBytes, "UTF8");

System.out.println(result);

}

}

}

this is giving me an exception...

java.security.NoSuchAlgorithmException: Algorithm DES not available

at javax.crypto.SunJCE_b.a(DashoA6275)

at javax.crypto.KeyGenerator.getInstance(DashoA6275)

at des.SecretWriting.main(SecretWriting.java:40)

Exception in thread "main"

any help...

Thanks in advance...

Doct0ra at 2007-7-12 15:46:52 > top of Java-index,Security,Cryptography...
# 3

Your code seems to be working fine for me. The exception that is being thrown by your program is thrown when the Provider does not support the algorithm that you are trying to use. What version of JDK are you using? May be the older implementations of SunJCE did not support DES algorithm. Try using the the latest JDK.

Also two things that are not directly related to your problem but are good to know.

1) DES is not considered a secure algorithm any longer. Try using AES or Triple-DES.

2) Avoid using packages that begin with sun, like sun.misc.*. These are used internally by Java APIs and are subject to change without notice. This can make your program fail when the JRE is updated. For Base64 encoding you can use the following API:

iharder.sourceforge.net/base64/

hope this helps...

DarumAa at 2007-7-12 15:46:52 > top of Java-index,Security,Cryptography...