Quote Originally Posted by wysota View Post
If the message was long enough, that wouldn't have helped much. Knowing the pattern of masking bits or shifting one could still be able to find collisions, i.e. if you shift, you'll eventually get the original key again and it can happer faster when you use weak keys. For example if you rotated the key one bit to the right in each iteration, a weak key could be 1111 0000 1111 0000 - it repeats every half of its length (0101 0101 0101 0101 is even weaker).
When I mentioned predefined extension of the key to make it virtually as long as the message I had no precise idea but I'm now thinking about this concept : "the message is the key". My first approach is to keep the XOR block encryption but to avoid repeating the key by using the message as a key. The first block is encrypted with the key and then used as key to encrypt the next block and so on... This is not of course unbreakable but the first block can't be decrypted in any other way than brute-force search which makes the algorithm much stronger than the first one I proposed.

Qt Code:
  1. void encrypt(QByteArray& data, QByteArray& pass)
  2. {
  3. int i = 0, e = data.size(), x = 0, m = pass.count();
  4. char c, *d = data.data(), *p = pass.data(), *b = new char [m];
  5.  
  6. while ( i < e )
  7. {
  8. b[x] = d[i];
  9. d[i] ^= p[x];
  10.  
  11. if ( !(x = (++i % m)) )
  12. qSwap(p, b);
  13. }
  14. }
  15.  
  16. void decrypt(QByteArray& data, QByteArray& pass)
  17. {
  18. int i = 0, e = data.size(), x = 0, m = pass.count();
  19. char c, *d = data.data(), *p = pass.data(), *b = new char [m];
  20.  
  21. while ( i < e )
  22. {
  23. d[i] ^= p[x];
  24. b[x] = d[i];
  25.  
  26. if ( !(x = (++i % m)) )
  27. qSwap(p, b);
  28. }
  29. }
To copy to clipboard, switch view to plain text mode 

Further enhancement of this code could be :
  • Compressing the data on-the-fly to reduce redundancy without increasing complexity (a simple RLE would be enough I think)
  • Using tricks to make encryption a little more random. For instance XOR'ing with 0xff (one's complement) on some special occasions (data byte is even, or odd, or multiple of 3 or whatever...)