Respected Sir,
I am very sorry to write ambigous post. Actually we are students who are keen to learn Cryptography using Java. We are trying to implement 3 crytographic algorithms ie Diffie-hellman to share the keys and then we are using DES to encrypt the secret message using the shared key and then we are incorporating RSA for encrypting the secret key of the DES and then send it to the user from the server. Sir, our project may look very silly to you. But we need guidance from you since we arent getting any proper guidance here. And so,we thought of using inbuilt functions for these algorithms.
We hope you that you will understand our problem and this post too.
Thanking You
> Respected Sir,
This makes me feel very old!
> I am very sorry to write ambigous post. Actually we
> are students who are keen to learn Cryptography using
> Java. We are trying to implement 3 crytographic
> algorithms ie Diffie-hellman to share the keys and
> then we are using DES to encrypt the secret message
> using the shared key and then we are incorporating
> RSA for encrypting the secret key of the DES and then
> send it to the user from the server. Sir, our project
> may look very silly to you.
Not silly at all.
> But we need guidance from
> you since we arent getting any proper guidance here.
> And so,we thought of using inbuilt functions for
> these algorithms.
Good idea. Saves you having to write them.
> We hope you that you will understand our problem and
> this post too.
> Thanking You
I'm not sure why you think you need to use Diffie-Hellman (DH)) to share secret keys since you say you are encrypting the secret key with RSA. You would either use DH to share a key and then use it to encrypt with both sides knowing the key OR you would create a random session key, encrypt it with the recipients RSA public key and send the encrypted session key to the recipient.
sir....
we are nw able to use our own key for encryption but on the client side
its not decrypting. its not getting the correct byte value. we don kno hw to correct it. so plz help.
thank you sir.
waitin for ur reply.
server side:
SecretKey key = new SecretKeySpec(b, "DES");
System.out.println("KEY for encryption:"+key);
byte[] initVector = new byte[] { 0x10, 0x10, 0x01, 0x04, 0x01, 0x01, 0x01, 0x02 };
AlgorithmParameterSpec algParamSpec = new IvParameterSpec(initVector);
Cipher m_encrypter = Cipher.getInstance("DES/CBC/PKCS5Padding");
Cipher m_decrypter = Cipher.getInstance("DES/CBC/PKCS5Padding");
m_encrypter.init(Cipher.ENCRYPT_MODE, key,algParamSpec);
m_decrypter.init(Cipher.DECRYPT_MODE, key,algParamSpec);
System.out.println("\n Enter data to be encrypted : ");
BufferedReader brr=new BufferedReader(new InputStreamReader(System.in));
String strr=brr.readLine();
byte[] clearText = strr.getBytes();
byte[] encryptedText = m_encrypter.doFinal(clearText);
System.out.println("Data for encryption:");
System.out.println(new String(clearText));
System.out.println("Encrypted data:");
System.out.println(new String(encryptedText));
//SENDING
PrintWriter p=new PrintWriter(s.getOutputStream(),true);
p.println(encryptedText);
client side:
//KEY GENERATION
SecretKey key = new SecretKeySpec(b, "DES");
System.out.println("KEY for decryption:"+key);
// for CBC; must be 8 bytes
byte[] initVector = new byte[] { 0x10, 0x10, 0x01, 0x04, 0x01, 0x01, 0x01, 0x02 };
AlgorithmParameterSpec algParamSpec = new IvParameterSpec(initVector);
Cipher m_decrypter = Cipher.getInstance("DES/CBC/PKCS5Padding");
m_decrypter.init(Cipher.DECRYPT_MODE, key,algParamSpec);
//RECEIVING
BufferedReader b2=new BufferedReader(new InputStreamReader(s.getInputStream()));
String key1=b2.readLine();
System.out.println("encrypted text received is:"+key1);
byte[] encryptedText = key1.getBytes("utf-8");
System.out.println("in bytes:"+encryptedText);
//System.out.println(new String(encryptedText));
System.out.println("test1");
byte[] decryptedText = m_decrypter.doFinal(encryptedText);
System.out.println("test1");
System.out.println("\n Decrypted data : ");
System.out.println(new String(decryptedText));
s.close();
It seem that today everyone thinks that toString() on an array of bytes gives one a String containing a representation of the content of the array. It does not so the line
p.println(encryptedText);
writes not the byte contents but a pseudo reference to the array.
Also, you seem to be trying to read a line of encrypted data. A line is terminated by \r, \n or \r\n (depending on theplatform) which can all be part of an encrypted message so they can't be used for lines.
Write encrypted bytes not characters. Use DataOutputStream and DataInputStream then you can write as an integer the number of bytes you are going to send and then the bytes.
In the receiving end, read the number of bytes then the bytes themselves.