Chinaunix首页 | 论坛 | 博客
  • 博客访问: 259459
  • 博文数量: 74
  • 博客积分: 1470
  • 博客等级: 上尉
  • 技术积分: 793
  • 用 户 组: 普通用户
  • 注册时间: 2008-11-25 21:01
文章分类

全部博文(74)

文章存档

2011年(1)

2010年(32)

2009年(32)

2008年(9)

我的朋友

分类: C/C++

2010-05-01 13:33:17

偶尔翻出以前关于加解密的一些东西,放上来和大家分享。
关于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》

文件: 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: marksman.xu@gmail.com";

    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: marksman.xu@gmail.com
------------------------
string:Welcome to My RC4 Test!
HIT(Harbin Institution of Technology)
Email: marksman.xu@gmail.com


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



阅读(2312) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2010-08-04 21:21:13

感谢分享~