文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>rc4加解密算法-c实现

rc4加解密算法-c实现

时间:2010-05-01  来源:marksman201

偶尔翻出以前关于加解密的一些东西,放上来和大家分享。
关于rc4加密算法的原理:

RC4 Encryption Principle

The RC4 encryption algorithm is stream cipher, which can use variable length keys. The algorithm was developed in 1987 by Ron Rivest, for RSA Data Security, and was a propriety algorithm until 1994.

In the algorithm the keystream is completely independent of the plaintext used. An 8 * 8 S-Box (S0 S255), each of the entries is a permutation of the numbers 0 to 255, and the permutation is a function of the variable length key. There are two counters i, and j, both initialised to 0 used in the algorithm.

The S-Box is easily generated, using the following:

Fill S1 to S255 linearly (i.e. S0 = 0; S1 = 1 ... S255 = 255)

Another 256 byte array is then filled with the key, the key is repeated as necessary to fill the entire array.

The index j is then set to 0

for (i = 0 to i = 255)
     j = (j + Si + ki) MOD 256
    Swap Si and Sj
fi

The following is used to generate a random byte:

i = (i + 1) MOD 256
j = (j + Si) MOD 256
Swap Si and Sj
t = (Si + Sj) MOD 256
K = St

K is the XORed with the plaintext to produce the ciphertext, or the ciphertext to produce the plaintext.

RSA claims that the algorithm is immune to differential and linear cryptanalysis. The algorithm can also be changed from the 8-bit used above to 16-bit by using a 16 * 16 S-Box, and a 16-bit word. The initial set-up would need to be repeated 65,536 times to keep with the design, but it should be faster.

附件中还有一篇对RC4原理详细说明的文档:

《Evaluation of the RC4 Algorithm for Data Encryption》http://www.tmrfindia.org/ijcsa/V3I24.pdf

文件: V3I24.pdf
大小: 149KB
下载: 下载

 


关于rc4加密算法的代码实现,原理搞清楚了,加密算法实现就很简单:

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

void swap(unsigned char *s1,unsigned char *s2)
{
    char temp;
    temp=*s1;
    *s1=*s2;
    *s2=temp;
}

void re_S(unsigned char *S)
{
    unsigned int i;
    for(i=0;i<256;i++)
        S[i]=i;
}

void re_T(unsigned char *T,char *key)
{
    int i;
    int keylen;
    keylen=strlen(key);
    for(i=0;i<256;i++)
        T[i]=key[i%keylen];
}

void re_Sbox(unsigned char *S,unsigned char *T)
{
    int i;
    int j=0;
    for(i=0;i<256;i++)
    {
        j=(j+S[i]+T[i])%256;
        swap(&S[i],&S[j]);
    }
}

void re_RC4(unsigned char *S,char *key)
{
    char T[256]={0};
    re_S(S);
    re_T(T,key);
    re_Sbox(S,T);
}

void RC4(char* src, int slen, char*key)
{
    unsigned char S[256]={0};
    int i,j,t;
    int m;

    re_RC4(S,key);
    i=j=0;
    for(m=0;m<slen;m++)
    {
        i = (i + 1) % 256;
        j = (j + S[i]) % 256;
        swap(&S[i],&S[j]);
        t = (S[i] + (S[j] % 256)) % 256;
        src[m]=src[m]^S[t];
    }
}

int main(void)
{
    char buf[256]={0};
    int len=0;
    char str[]="Welcome to My RC4 Test! \n"
        "HIT(Harbin Institution of Technology)\n"
        "Email: [email protected]";

    len=strlen(str);
    strcpy(buf,str);

    printf("string:%s\n",buf);
    RC4(buf,len,"abcdefghijklmn");
    //printf("string:%s\n",buf);


    printf("-----------------------\n");

    RC4(buf,len,"abcdefghijklmn");
    printf("string:%s\n",buf);

    return 0;
}


$ gcc -o rc4 rc4.c
$ ./rc4
string:Welcome to My RC4 Test!
HIT(Harbin Institution of Technology)
Email: [email protected]
------------------------
string:Welcome to My RC4 Test!
HIT(Harbin Institution of Technology)
Email: [email protected]


在rc系列的加密算法中,rc4已经基本可以满足一般的加密需求,如果觉得rc4还不太安全的话,可以考虑rc5



相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载