Last active
August 29, 2015 14:08
-
-
Save vathpela/b064d806af4c62dcc087 to your computer and use it in GitHub Desktop.
rc4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
rc4 : rc4.c | |
gcc -funsigned-char -o $@ $^ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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