RC4 basic encrypt message error? [on hold]

I don’t know if this is normal or not, but it seems to me that the rendering (result) of the RC4 encryption that I implemented in C provides me with something else and I’m not convinced where I’m wrong.

#define REF_RC4 0xFF
#define EXP_RC4 0x100

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct s_context{
    unsigned int I;
    unsigned int J;
    unsigned int S[EXP_RC4];
}CONTEXT;

void f_init_rc4( CONTEXT *p, unsigned char Key[], int size ){

    p->I = 0;
    p->J = 0;
    p->S[0] = '\0';

    unsigned int s = 0;
    for( p->I = 0; REF_RC4 > p->I; p->I++ )
        p->S[p->I] = p->I;

    for( p->I = 0; REF_RC4 > p->I; p->I++ ){
        p->J = ( p->J + p->S[p->I] + Key[p->I % size]) & REF_RC4;
        s = p->S[p->I];
        p->S[p->I] = p->S[p->J];
        p->S[p->J] = s;
    }
    p->I = 0;
    p->J = 0;
}

void f_cipher_RC4( CONTEXT *p,unsigned char *pMsg){

    unsigned int i = 0;
    unsigned int s = 0;
    const unsigned long x = (unsigned long)strlen((char*)pMsg);
    for( i = 0; x > i; i++ ){
        p->I = ( p->I + 1 ) & EXP_RC4;
        p->J = ( p->J + p->S[p->I] ) & EXP_RC4;
        s = p->S[p->I];
        p->S[p->I] = p->S[p->J];
        p->S[p->J] = s;
        pMsg[i] = pMsg[i] ^ p->S[(p->S[p->I]+p->S[p->J]) & REF_RC4 ];
    }
}


int main( void ){

    extern int errno;
    CONTEXT *p = NULL;
    unsigned char Key[] = "Wiki\0";
    unsigned char Message[] = "pedia\0";

    errno = 0;
    if( NULL == (p = malloc(1 *sizeof(struct s_context*) ) ) ){
        (void)fprintf(stderr, "Error(%d)\t:%s\n÷t:%s\n", errno,
                      "Error malloc context", strerror(errno) );
        return EXIT_FAILURE;
    }

    (void)memset(p, 0, sizeof(*p));
    f_init_rc4(p, Key, 4 );
    p->I = 0;
    p->J = 0;
    f_cipher_RC4(p, Message);
    (void)fprintf( stdout, "CIPHER\t:%02X\n", (unsigned int)Message );
    free( (NULL==p) ? NULL : p );
    p = NULL;
    return EXIT_SUCCESS;
}

Result:

CIPHER  :5FBFF603
Program ended with exit code: 0

Expected results:

CIPHER :1021BF0420

Continue reading RC4 basic encrypt message error? [on hold]

Posted in RC4