RC4 encryption algorithm and its implementation

Source: Internet
Author: User

The RC4 encryption Algorithm (HTTP://EN.WIKIPEDIA.ORG/WIKI/RC4) is the top figure in the famous RSA trio Ron Rivest was designed in 1987 with a variable key-length stream cipher algorithm cluster. It is called a cluster, because its core part of the S-box length can be arbitrary, but generally 256 bytes. The speed of the algorithm can reach about 10 times times of DES encryption, and it has a high level of nonlinearity. RC4 was originally used to protect trade secrets. But in September 1994, its algorithms were posted on the Internet, and there was no more trade secrets. RC4 is also called ARC4 (alleged rc4--so-called RC4), because RSA has never formally published this algorithm.

The RC4 principle is very simple, including the initialization algorithm (Ksa/setkey) and pseudo-random codon generation algorithm (prga/transform) two parts. The implementation code is as follows (VS2008):

[CPP]View Plaincopy
  1. Rc4.h
  2. template<class t> inline void
  3. Swap (t& I, t& j)
  4. {
  5. T tmp = i;
  6. i = j;
  7. j = tmp;
  8. }
  9. Class RC4
  10. {
  11. Public
  12. void Setkey (const char* key, int keylen);
  13. void Transform (char* output, const char* input, int len);
  14. Private
  15. unsigned char key_[256];
  16. };
  17. /////////////////////////////////////////////
  18. rc4.cc
  19. #include "rc4.h"
  20. void Rc4::setkey (const char* key, int keylen)
  21. {
  22. For (int i = 0; i <; i++)
  23. {
  24. Key_[i] = i;
  25. }
  26. int j = 0;
  27. For (int i = 0; i <; i++)
  28. {
  29. j = (j + key_[i] + Key[i%keylen])% 256;
  30. Swap (Key_[i], key_[j]);
  31. }
  32. }
  33. void Rc4::transform (char* output, const char* input, int len)
  34. {
  35. int i = 0, j = 0;
  36. For (int k = 0; k < Len; k++)
  37. {
  38. i = (i + 1)% 256;
  39. j = (j + key_[i])% 256;
  40. Swap (Key_[i], key_[j]);
  41. unsigned char subkey = key_[(Key_[i] + key_[j])% 256];
  42. Output[k] = subkey ^ Input[k];
  43. }
  44. }
  45. /////////////////////////////////////////
  46. main.cc
  47. #include <stdio.h>
  48. #include <string.h>
  49. #include <stdlib.h>
  50. #include "rc4.h"
  51. int main ()
  52. {
  53. char input[256] = "Times teaches all things to him who lives forever but I had not the luxury of eternity.";
  54. char output[256] = "";
  55. printf ("before encrypt:%s/n", input);
  56. const int keylen = 16;
  57. char Key[keylen] = "";
  58. For (int i = 0; i < Keylen; i++)
  59. {
  60. Key[i] = rand ()% 256;
  61. }
  62. RC4 Rc4encrypt, Rc4decrypt;
  63. Rc4encrypt. Setkey (key, Keylen);
  64. Rc4decrypt. Setkey (key, Keylen);
  65. Rc4encrypt. Transform (output, input, strlen (input));
  66. printf ("After encrypt:%s/n", output);
  67. Rc4decrypt. Transform (output, output, strlen (input));
  68. printf ("After decrypt:%s/n", output);
  69. return 0;
  70. }

RC4 encryption algorithm and its implementation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.