├── .gitignore ├── Makefile ├── README ├── rc4.c ├── rc4.h └── test.c /.gitignore: -------------------------------------------------------------------------------- 1 | *~ 2 | *.c~ 3 | *.h~ 4 | .*~ 5 | *.o 6 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | OBJS=rc4.o test.o 2 | CC=gcc 3 | CFLAGS=-O -g 4 | 5 | test:$(OBJS) 6 | $(CC) $(OBJS) -o test 7 | 8 | rc_4.o:rc4.c rc4.h 9 | $(CC) $(CFLAGS) -c rc4.c -o rc4.o 10 | 11 | test.o:test.c rc4.h 12 | $(CC) $(CFLAGS) -c test.c -o test.o 13 | 14 | clean: 15 | rm -rf *.o test -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | This is the codes for RC4, one of the software stream cipher. 2 | Since RC4 generates a pseudorandom stream of bits, it can be used for encryption by combining it with the plaintext using bit-wise exclusive-or; decryption is perform the same way. 3 | -------------------------------------------------------------------------------- /rc4.c: -------------------------------------------------------------------------------- 1 | #include "rc4.h" 2 | 3 | /*S is initialized to the identity permutation, mixes in bytes of the key.*/ 4 | void RC4_key(RC4_KEY *rc4_key, unsigned char *key, int *keylength){ 5 | int i, j, temp; 6 | 7 | /*Initialize S*/ 8 | for (i = 0; i < 256; i++) 9 | rc4_key -> S[i] = i; 10 | 11 | j = 0; 12 | for (i = 0; i < 256; i++){ 13 | j = (j + rc4_key -> S[i] + *(key + i % *keylength)) % 256; 14 | /*Swap rc4_key -> S[i] and rc4_key -> S[j]*/ 15 | temp = rc4_key -> S[i]; 16 | rc4_key -> S[i] = rc4_key -> S[j]; 17 | rc4_key -> S[j] = temp; 18 | } 19 | } 20 | 21 | /*Generate the key stream which length is the same as plaintext's and encrypt the plaintext and output the ciphertext.*/ 22 | void RC4(RC4_KEY *rc4_key, unsigned char *plaintext, int *plaintext_length, unsigned char *ciphertext){ 23 | int i = 0, j = 0, k = 0, n, temp; 24 | 25 | for (k = 0; k < *plaintext_length; k++){ 26 | i = (i + 1) % 256; 27 | j = (j + rc4_key -> S[i]) % 256; 28 | /*Swap rc4_key -> S[i] and rc4_key -> S[j]*/ 29 | temp = rc4_key -> S[i]; 30 | rc4_key -> S[i] = rc4_key -> S[j]; 31 | rc4_key -> S[j] = temp; 32 | 33 | n = rc4_key -> S[(rc4_key -> S[i] + rc4_key -> S[j]) % 256]; 34 | 35 | /*Encryption*/ 36 | *(ciphertext + k) = *(plaintext + k) ^ n; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /rc4.h: -------------------------------------------------------------------------------- 1 | #ifndef RC4_H 2 | #define RC4_H 3 | 4 | #define KEY_LENGTH 6 5 | #define TEXT_LENGTH 10 6 | 7 | typedef struct RC4_KEY_S{ 8 | unsigned char S[256]; 9 | }RC4_KEY; 10 | 11 | /*S is initialized to the identity permutation, mixes in bytes of the key.*/ 12 | void RC4_key(RC4_KEY *rc4_key, unsigned char *key, int *keylength); 13 | 14 | /*Encrypt the plaintext and output the ciphertext.*/ 15 | void RC4(RC4_KEY *rc4_key, unsigned char *plaintext, int *plaintext_length, unsigned char *ciphertext); 16 | 17 | #endif 18 | -------------------------------------------------------------------------------- /test.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "rc4.h" 3 | 4 | int main(){ 5 | int i, x; 6 | int *p; 7 | unsigned char plaintext[TEXT_LENGTH]; 8 | unsigned char key[KEY_LENGTH] = "cipher"; 9 | unsigned char ciphertext[TEXT_LENGTH]; 10 | 11 | RC4_KEY rc4_key; 12 | 13 | printf("------------------------------\n----------RC4 CIPHER----------\n------------------------------\n"); 14 | printf("Please enter the plaintext(%d characters): ", TEXT_LENGTH); 15 | for (i = 0; i < TEXT_LENGTH; i++) 16 | scanf("%c", &plaintext[i]); 17 | 18 | /*Encryption*/ 19 | x = KEY_LENGTH; 20 | p = &x; 21 | RC4_key(&rc4_key, &key[0], p); 22 | x = TEXT_LENGTH; 23 | RC4(&rc4_key, &plaintext[0], p, &ciphertext[0]); 24 | 25 | printf("The plaintext is: "); 26 | for (i = 0; i < TEXT_LENGTH; i++) 27 | printf("%x ", plaintext[i]); 28 | printf("\nThe key is: "); 29 | for (i = 0; i < KEY_LENGTH; i++) 30 | printf("%x ", key[i]); 31 | printf("\nThe ciphertext is: "); 32 | for (i = 0; i