| | |
Encryption in Java
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
•
•
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.

I love the way you encourage ppl at Dani-Web.
And we ve got a couple of workarounds I see. Nice.
•
•
Join Date: Mar 2009
Posts: 123
Reputation:
Solved Threads: 0
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.
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.
•
•
Join Date: Apr 2008
Posts: 1,030
Reputation:
Solved Threads: 152
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.
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.
•
•
Join Date: Mar 2009
Posts: 123
Reputation:
Solved Threads: 0
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.
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.
•
•
Join Date: Dec 2008
Posts: 53
Reputation:
Solved Threads: 6
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:
(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:
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).
(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)
Random r = new SecureRandom(); byte[] key = new byte[8]; r.nextBytes(key); 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)
KeyGenerator kg = KeyGenerator.getInstance("DES"); SecretKey k = kg.generateKey(); SecretKeyFactory skf = SecretKeyFactory.getInstance("DES"); DESKeySpec spec = (DESKeySpec) skf.getKeySpec(k, DESKeySpec.class); 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
![]() |
Similar Threads
- encryption (Java)
- java is supposed to be secure? (Java)
- decrypting (Java)
- Simple problem with encryption (Java)
- Encryption -- Custom algorithm (Java)
- IE6SP2 update on ME - Java doesn't work (Windows 95 / 98 / Me)
Other Threads in the Java Forum
- Previous Thread: Storing objects in an array using inheritance and polymorphism
- Next Thread: another ActionListener question
Views: 1203 | Replies: 17
| Thread Tools | Search this Thread |
Tag cloud for Java
911 addball addressbook android api append apple applet application arguments array arrays automation binary bluetooth button chat class classes client code component css csv database draw eclipse ee error event exception file fractal game givemetehcodez graphics gui helpwithhomework html ide image input integer j2me java javaarraylist javaprojects jmf jni jpanel julia jvm key linux list loan loop map method methods mobile netbeans newbie number object oracle output packets phone print problem program programming project recursion reporting robot scanner screen se server service set size sms socket software sort sql stream string swing test threads time transfer tree ubuntu windows wrong






