RC4.cpp 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #include <iostream>
  2. using namespace std;
  3. int main() {
  4. int key_size,plain_text_size,N=8,l=8,k=0;
  5. cout<<"Enter the size of key\n";
  6. cin>>key_size;
  7. cout<<"Enter the size of plain text\n";
  8. cin>>plain_text_size;
  9. int i,j=0,key[key_size],plain_text[plain_text_size],S[N],Z[key_size],cipher_text[plain_text_size];
  10. cout<<"Enter the key\n";
  11. for(i=0;i<key_size;i++)
  12. cin>>key[i];
  13. cout<<"Enter the plain text\n";
  14. for(i=0;i<plain_text_size;i++)
  15. cin>>plain_text[i];
  16. //KSA
  17. for(i=0;i<N;i++)
  18. S[i]=i;
  19. for(i=0;i<N;i++)
  20. {
  21. j=(j+S[i]+(key[i%key_size]))%l;
  22. S[i]=S[i]+S[j];
  23. S[j]=S[i]-S[j];
  24. S[i]=S[i]-S[j];
  25. }
  26. i=0;j=0;
  27. //PRGA
  28. for(k=0;i<key_size;k++)
  29. {
  30. i++;
  31. j=(j+S[i])%l;
  32. int temp;
  33. temp=S[i];
  34. S[i]=S[j];
  35. S[j]=temp;
  36. Z[k]=S[(S[i]+S[j])%l];
  37. cipher_text[k]=plain_text[k] ^ Z[k];
  38. }
  39. cout<<"Cipher Text is ";
  40. for(i=0;i<plain_text_size;i++)
  41. cout<<cipher_text[i];
  42. cout<<endl;
  43. return 0;
  44. }
  45. /*
  46. OUTPUT:-
  47. Enter the size of key
  48. 4
  49. Enter the size of plain text
  50. 4
  51. Enter the key
  52. 1 2 3 6
  53. Enter the plain text
  54. 1 2 2 2
  55. Cipher Text is 4323
  56. */