1

I am working on a client-server application in Java which is going to use private-key encryption.

Currently I have a class with a static object of the Cipher class which I initialise like this: myCipher.init(Cipher.DECRYPT_MODE, secretKey, ivParameterSpec);.

Now my question is: I have two-way communication, should I have a separate Cipher object for each way (one for encrypting and one for decrypting) and use the same initialisation vector? Or should I just use the same object and call Cipher.init() to change the modes depending on whether I am encrypting or decrypting?

It makes sense to have separate objects in my head, but I just wanted to be sure. I tried googling but most examples only show encryption one way.

Currently I send the initialisation vector to the server unencrypted, is this correct, or is there a security flaw?

Or am I approaching it completely the wrong way?

Thanks.

1
  • Its a learning exercise for me, not a production one. It was more interesting to learn how to do things (and how it works) manually that to simply use SSL where everything is done by java. Commented May 17, 2011 at 10:26

1 Answer 1

2

Your question is two fold:

  1. You should use one object. This saves memory. But if memory is spendable, go ahead with two. At least that'll make your code more readable.

  2. IV needs not encrypted. IV is a random value, sync'd by both the sender and receiver. The most important thing about IV is it must not be repeated, or re-used, with the same key!

Sign up to request clarification or add additional context in comments.

5 Comments

@Answer 1: I'm just trying to gauge the Memory/CPU trade-off here. I would like the decryption/encryption to be a quick as possible so if using separate objects does that without too much of an increase in memory...how much memory are we talking here? @Answer 2: So does the iv need to be recreated per message between client and server or per session? Currently I have one iv per session for all messages sent between the client and server whilst they are connected. If they disconnect and reconnect, a new IV is generated.
1. using separate objects do not increase performance, unless they are executed in different threads/processes. the overhead of a new object is usually tens to a few hundreds bytes. Small, but still. 2. it's best to use different IVs for different messages. Once again, these IVs need not be protected. So, you could use a random IV initially, then increase it by one after a message. The IV should be long enough (64 bits or more) to prevent rollover.
So if I'm thinking correctly. An initialisation vector is only useful if the attacker does not know what is it?...but its sent in plaintext with the encrypted message so if the attacker knows that the IV is the first 8 bytes of the message, they know the initialisation vector defeating the point of it....correct?
Do you want to pull this comment to another question? No, the purpose if the IV is not to be secret. The only secret is your key. The IV just needs to be random to prevent repeated cipherblock (same plaintext + same key + different iv --> different cipherblock).
After hours of research and trawling through examples I now understand. Thanks for your invaluable help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.