Setelah sebelumnya andik19 berbagi algoritma enkripsi RC4, kali andik19 akan membagikan source code RC4 . Source code ini adalah implementasi dari algoritma RC4 .
Source code:
#include <cstdlib>
#include <iostream>
#include <winsock.h>
#include <windows.h>
#include <string>
using namespace std;
void rc4(unsigned char * input, unsigned char * key,unsigned char * &output){
unsigned char * doc;
int i,j=0,t,n,s[256], s2[256],temp,K;
for (i=0;i<256;i++){
s[i]=i;//mengisi indek
s2[i]= key[(i % strlen((char *)key))]; //mengisi index s2 dengan key sampai penuh
}
//KSA
for (i=0;i<256;i++){
j = (j + s[i] + s2[i]) % 256;
temp=s[i];
s[i]=s[j];
s[j]=temp;
}
doc = new unsigned char [ (int)strlen((char *)input)] ;
//PGRA
for (i=j=n=0;n<(int)strlen((char *)input);n++){
i = (i + 1) % 256;
j = (j + s[i]) % 256;
temp=s[i];
s[i]=s[j];
s[j]=temp;//swap
t = (s[i] + s[j]) % 256;
K = s[t];//keyStream
doc[n]=input[n]^K;//XOR //ENCRIPT //DECRIPT
}
doc[n]='\0';
output=doc;
}
int main(int argc, char *argv[])
{
unsigned char * plaintext;
unsigned char * key;
unsigned char * encrypted;
unsigned char * decrypted;
plaintext=(unsigned char *)"Andik19 .blogspot.com";
key=(unsigned char *)"abc";//key harus sama
rc4(plaintext,key,encrypted);
cout << "Data asli " << plaintext << endl;
cout << "Data encrypted " << encrypted << endl;
rc4(encrypted,key,decrypted); //encrypted sbg input
cout << "Data decripted " << decrypted << endl;
system("PAUSE");
}
Terimakasih , semoga membantu.