linux内核arc4算法,rc4加解密算法-c实现

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

关于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 S1to S255linearly (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 Siand 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》

pdf.gif

文件:

V3I24.pdf

大小:

149KB

下载:

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

#include

#include

#include

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

{

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值