I need to use a stream cipher in my application. I know using the same key for many messages is not a good practice for stream ciphers (RC4). But I also dont want to make a new key all the time so I came up with the following.
I create securelly a secret key. Then for every message I send, the key I use in the encryption will be the sha1 hash of the previous key.
So for 1st message the key will be key = sha1(key)
for the 2nd message the key will be key = sha1(sha1(key)), etc..

key := makeSecureKey()
     key := sha1hash(key);
     message := getNewMessage();
     encrypted_message := RC4.Encrypt(message, key);

Is encryption used correctly here?
Finally if instead of using the 160bit result of the sha1 algorithm, I use only the first 128 of the 160bits as a key is there a problem?


1 Answer 1


This is bad practice for so many reasons.

  1. RC4 has been broken every which way since Sunday.
  2. Your key generation algorithm is home grown and may not be safe. Perhaps someone on crypto.stackexchange.com is willing to analyze it for you. I'd leave it behind and use a published KDF.
  3. There are known attacks against RC4 that involve subsequent derivations of a starting key. I do not know if there is one for this.
  4. The SHA hashing function wasn't designed to be a good PRNG, nor a good key generator, these aren't its purpose. There may be artifacts within the resulting hash that compromise how any cipher uses it as a key, let alone RC4.
  5. Modern block ciphers with CBC mode are faster than your network stream can handle data, so if this is traveling over a wire, you don't need a stream cipher.

This Q&A has an excellent answer for why you should use block ciphers over stream ciphers. Also, I prefer AES+CBC for Privacy (encryption) and HMAC for Integrity protection over algorithms like EAX or GCM which perform both in one step. Not sure if I could defend this position (security, mathematically), nevertheless the one algorithm to address both bothers me.

  • I'm not sure where you're reading in that Q&A that you should use CBC as the preferred block cipher mode... Nov 26, 2015 at 22:08
  • @puzzlepalace, fair enough, I skimmed that Answer and saw him test (for performance) AES+CBC. I'm not convinced combining authn+enc in one step (EAX or GCM) is really secure against attacks. I prefer separate AES+CBC for Privacy and HMAC for Integrity. Changing my answer to reflect this. Nov 26, 2015 at 23:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .