rc4.h 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /*
  2. ** Command & Conquer Renegade(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #ifndef RC4_H
  19. #define RC4_H
  20. //
  21. // RC4.h - Implementation of RC4 encryption
  22. //
  23. // RC4 is a stream cypher. This means that it basically produces a stream of
  24. // random bytes that you XOR with your data. Each key is somewhat like a
  25. // one time pad.
  26. //
  27. // Just as you should never re-use a one time pad, you should never re-use a key.
  28. //
  29. // If you can't re-exchange a secret key before every message you could keep a
  30. // partial secret key and then include the other part of the key in plaintext.
  31. // The key would be the concatenation of the two parts of the key.
  32. //
  33. class RC4Class
  34. {
  35. public:
  36. RC4Class();
  37. //
  38. // Key length can be 0..256 bytes
  39. // Key preparation takes about 0.015 Ms on a 1Ghz PC
  40. //
  41. void Prepare_Key(const unsigned char *key_data_ptr, int key_data_len);
  42. //
  43. // In-place encryption. Call Prepare_Key first!
  44. // Only a few clock cycles per byte (9 or so...)
  45. //
  46. void RC4(unsigned char *buffer_ptr, int buffer_len);
  47. private:
  48. struct RC4Key
  49. {
  50. unsigned char State[256];
  51. unsigned char X;
  52. unsigned char Y;
  53. };
  54. RC4Key Key;
  55. };
  56. #endif