Skip to content

Instantly share code, notes, and snippets.

@vathpela
Last active August 29, 2015 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vathpela/b064d806af4c62dcc087 to your computer and use it in GitHub Desktop.
Save vathpela/b064d806af4c62dcc087 to your computer and use it in GitHub Desktop.
rc4
rc4 : rc4.c
gcc -funsigned-char -o $@ $^
/*
* build with: gcc -funsigned-char -o test test.c
*/
#include <stdio.h>
#include <string.h>
#define rc4(d,l,t,m) \
{char s[256]="";int i=-1,j=0,x;for(;++i<256;)s[i]=i;for(i=-1;++i<256;){x=s[i];s[i]=s[j=(j+s[i]+d[i%l])%256];s[j]=x;}i=0;j=0;while(m--){i++;i&=255;x=s[i];s[i]=s[j=(j+x)%256];s[j]=x;*t++^=s[(s[i]+s[j])%256];}}
int main(void)
{
char key[] = "abcdefghijklmnop";
char plaintext[]="now is the time for all good men";
char *t=plaintext;
int m=strlen(plaintext);
int n=m;
printf("plaintext: \"%s\"\n", plaintext);
rc4(key, strlen(key), t, m);
printf("plaintext: \"%s\"\n", plaintext);
t=plaintext;
rc4(key, strlen(key), t, n);
printf("plaintext: \"%s\"\n", plaintext);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment