943,589 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 2158
  • Java RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Apr 8th, 2009
0

Re: Encryption in Java

Click to Expand / Collapse  Quote originally posted by jbennet ...
As you dont have the key yet, the key would have to be sent unencrypted which means it can be sniffed, defeating the point of the encryption.
Hey! Thanks for reiterating my message
I love the way you encourage ppl at Dani-Web.

And we ve got a couple of workarounds I see. Nice.
Reputation Points: 10
Solved Threads: 3
Newbie Poster
hkansal is offline Offline
21 posts
since Mar 2009
Apr 8th, 2009
0

Re: Encryption in Java

Hello everyone , thanks a lot for your answers ,

Wow .. I've learn so many things, i will try these techniques during the coming days !!

Again, thanks a lot, I will let know if my problem has been solved.
Reputation Points: 6
Solved Threads: 0
Junior Poster
neutralfox is offline Offline
124 posts
since Mar 2009
Apr 12th, 2009
0

Re: Encryption in Java

Hello, the reason why I want to save the key : because I want to seperate the encryption and decryption process.

Let say, that the sender encrypt the message using a specific key and the receiver need this key to decrypt the message at the other end.

You see, I just want to have the key ... but the problem is that I am unable to store the key and use it at the other .. thanks.
Reputation Points: 6
Solved Threads: 0
Junior Poster
neutralfox is offline Offline
124 posts
since Mar 2009
Apr 12th, 2009
0

Re: Encryption in Java

OK, lets go thru this one more time:
At the server generate a public/private key pair, and store them.
Client connects, requests server's public key.
Client generates a random session key, encrypts it using the server's public key, and sends it to the server.
Server uses its private key to decrypt the session key.
Client and server now use the session key to encrypt all following traffic, starting with a logon etc.
Featured Poster
Reputation Points: 1907
Solved Threads: 949
Posting Expert
JamesCherrill is offline Offline
5,756 posts
since Apr 2008
Apr 12th, 2009
0

Re: Encryption in Java

Thanks for the answer James, but the problem is that ... i don't want to use the private/public key. I want to use the example I gave in the first post. I just want to have a way to store the key .. or use the same key to decrypt.

Forget the chat program for the time being. I just want to use DES to encrypt and decrypt file. But the encryption and the decryption should be seperated and for that i must find a way to store the key. Hope you understand my question.

I will surely use the public/private key in the coming weeks but for the time I want to use DES .. its for learning purposes.

Thanks a lot for your answers guys.
Reputation Points: 6
Solved Threads: 0
Junior Poster
neutralfox is offline Offline
124 posts
since Mar 2009
Apr 12th, 2009
0

Re: Encryption in Java

If you just want to store the key, use Preferences, like I suggested n posts ago. I understand what you WANT to do, but I really think it's not realistic - you'll have to send the key to the client unencrypted, which is pointless. Still, good luck.
Featured Poster
Reputation Points: 1907
Solved Threads: 949
Posting Expert
JamesCherrill is offline Offline
5,756 posts
since Apr 2008
Apr 12th, 2009
0

Re: Encryption in Java

Thanks for the answer, yepp you are right .. but I just want to use DES for some other purposes, just want to know how it works, I am currently working on the FTP part .... in the coming weeks I will surely implement the private/public, if i have enough time of course, again, thanks for the answer.
Reputation Points: 6
Solved Threads: 0
Junior Poster
neutralfox is offline Offline
124 posts
since Mar 2009
Apr 12th, 2009
0

Re: Encryption in Java

The key is just a string of random bytes. You can store it any which way you like-- it's just a boring old string of bytes. To actually get the bytes, you have a couple of options:

(1) Just generate the string of random bytes yourself, using SecureRandom, then init your Cipher with a SecretKeySpec wrapper around those bytes:

Java Syntax (Toggle Plain Text)
  1. Random r = new SecureRandom();
  2. byte[] key = new byte[8];
  3. r.nextBytes(key);
  4. c.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key, "DES"));

(2) The "proper" way (but slightly overengineered for this purpose) is to use a KeyGenerator to generate the key, then a SecretKeyFactory to convetr it into a keyspec object from which you can pull out the bytes:

Java Syntax (Toggle Plain Text)
  1. KeyGenerator kg = KeyGenerator.getInstance("DES");
  2. SecretKey k = kg.generateKey();
  3. SecretKeyFactory skf = SecretKeyFactory.getInstance("DES");
  4. DESKeySpec spec = (DESKeySpec) skf.getKeySpec(k, DESKeySpec.class);
  5. byte[] keyBytes = spec.getKey();

You can also cheat slightly in this case. If you call getEncoded() on the SecretKey, you'll actually get the selfsame bytes as via the factory.

Strictly, the KeyGenerator is the "correct" way because it is supposed to know about certain weak keys/constraints on key creation for your particular algorithm. As I recall, DES has 4 weak kes out of the total 2^56, so in practice you really don't need to worry. AES (and, ideally, whatever algorithm you pick) has no known weak keys -- you really can just generate a random string of bytes.

Incidentally, I assume you know that DES is not secure by today's standards (actually, it wasn't very secure by yesterday's standards...) and much slower and less secure than practically any other algorithm in the universe. You may be interested in some stuff I've written about Java cryptography, including the security and performance of different algorithms. I'd also recommend you read the stuff about block modes (the example uses AES, but the information leak problem applies to DES too).
Last edited by neilcoffey; Apr 12th, 2009 at 11:34 pm. Reason: Added clarification
Reputation Points: 120
Solved Threads: 7
Junior Poster in Training
neilcoffey is offline Offline
53 posts
since Dec 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Storing objects in an array using inheritance and polymorphism
Next Thread in Java Forum Timeline: another ActionListener question





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC