You are on page 1of 2

RC4 Program(short code)

#include <iostream>
#include <string>
using namespace std;

int main() {
int s[256];
int T[256];
int i, j;
string plaintext ;
string key1 ;
cout << "enter plaintext= ";
getline(cin, plaintext);
cout << endl;
cout << "enter key= ";
getline(cin, key1);
cout << endl;
// Initilize the S
for (i = 0; i < 256; i++) {
s[i] = i;
T[i] = (int)key1[(i % key1.length())];
}
j = 0;
//int tmp = 0;
for (i = 0; i< 256; i++) { Output
j = (j + s[i] + T[i]) % 256; enter plaintext cryp
swap(s[i], s[j]);
} enter key rc41

cyphertext is= n ╛ ╡
int cipher[10000];
int keystream;
i = 0, j = 0;
cout << "cyphertext is= ";
for (int r = 0; r<plaintext.length(); r++)
{
i = (i + 1) % 256;
j = (j + s[i]) % 256;
swap(s[i], s[j]);
keystream = s[(s[i] + s[j]) % 256];
cipher[r] = ((int) plaintext[r] ^ keystream);
cout << " " << (char) cipher[r];
}
cout << endl;
system("pause");
return 0;
}

You might also like