rc4加密的C源代码

rc4.h

#ifndef _RC4_H
#define _RC4_H

struct rc4_state
{
int x, y, m[256];
};

void rc4_setup( struct rc4_state *s, unsigned char *key, int length );
void rc4_crypt( struct rc4_state *s, unsigned char *data, int length );

#endif /* rc4.h */

rc4.c

/*
* An implementation of the ARC4 algorithm
*
* Copyright (C) 2001-2003 Christophe Devine
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/

#include "rc4.h"

void rc4_setup( struct rc4_state *s, unsigned char *key, int length )
{
int i, j, k, *m, a;

s->x = 0;
s->y = 0;
m = s->m;

for( i = 0; i < 256; i++ )
{
m[i] = i;
}

j = k = 0;

for( i = 0; i < 256; i++ )
{
a = m[i];
j = (unsigned char) ( j + a + key[k] );
m[i] = m[j]; m[j] = a;
if( ++k >= length ) k = 0;
}
}

void rc4_crypt( struct rc4_state *s, unsigned char *data, int length )
{
int i, x, y, *m, a, b;

x = s->x;
y = s->y;
m = s->m;

for( i = 0; i < length; i++ )
{
x = (unsigned char) ( x + 1 ); a = m[x];
y = (unsigned char) ( y + a );
m[x] = b = m[y];
m[y] = a;
data[i] ^= m[(unsigned char) ( a + b )];
}

s->x = x;
s->y = y;
}

#ifdef TEST

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

/*
* ARC4 tests vectors from OpenSSL (crypto/rc4/rc4test.c)
*/

static unsigned char keys[7][30]={
{8,0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef},
{8,0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef},
{8,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
{4,0xef,0x01,0x23,0x45},
{8,0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef},
{4,0xef,0x01,0x23,0x45},
};

static unsigned char data_len[7]={8,8,8,20,28,10};
static unsigned char data[7][30]={
{0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,0xff},
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xff},
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xff},
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0xff},
{0x12,0x34,0x56,0x78,0x9A,0xBC,0xDE,0xF0,
0x12,0x34,0x56,0x78,0x9A,0xBC,0xDE,0xF0,
0x12,0x34,0x56,0x78,0x9A,0xBC,0xDE,0xF0,
0x12,0x34,0x56,0x78,0xff},
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xff},
{0},
};

static unsigned char output[7][30]={
{0x75,0xb7,0x87,0x80,0x99,0xe0,0xc5,0x96,0x00},
{0x74,0x94,0xc2,0xe7,0x10,0x4b,0x08,0x79,0x00},
{0xde,0x18,0x89,0x41,0xa3,0x37,0x5d,0x3a,0x00},
{0xd6,0xa1,0x41,0xa7,0xec,0x3c,0x38,0xdf,
0xbd,0x61,0x5a,0x11,0x62,0xe1,0xc7,0xba,
0x36,0xb6,0x78,0x58,0x00},
{0x66,0xa0,0x94,0x9f,0x8a,0xf7,0xd6,0x89,
0x1f,0x7f,0x83,0x2b,0xa8,0x33,0xc0,0x0c,
0x89,0x2e,0xbe,0x30,0x14,0x3c,0xe2,0x87,
0x40,0x01,0x1e,0xcf,0x00},
{0xd6,0xa1,0x41,0xa7,0xec,0x3c,0x38,0xdf,0xbd,0x61,0x00},
{0},
};

int main( void )
{
int i;
struct rc4_state s;
unsigned char buffer[30];

printf( "\n RC4 Validation Tests:\n\n" );

for( i = 0; i < 6; i++ )
{
printf( " Test %d ", i + 1 );

memcpy( buffer, data[i], data_len[i] );

rc4_setup( &s, &keys[i][1], keys[i][0] );
rc4_crypt( &s, buffer, data_len[i] );

if( memcmp( buffer, output[i], data_len[i] ) )
{
printf( "failed!\n" );
return( 1 );
}

printf( "passed.\n" );
}

printf( "\n" );

return( 0 );
}

#endif

===============================================================================

刚才浪费了好长的时间研究RC4加密算法。
关于RC4加密算法,可以参考百度百科:RC4算法

参考RC4经典加密算法代码(VB版),写了个VBS版。
下面附有代码,我觉得应该没有错误。
但测试后却发现:
1, key不能使用中文
2, 不同编码的中文加密以后的值也是不同的
3, 加密以后太多的不可见字符,根本不适合web使用。
4, 也许变通一下会是一个不错的加密算法。比如我下面的直接输出asc码。

Function RC4(inp,key)
' //RC4加密算法的VBS版
' //by fisker

Dim S(255), K(255), i
Dim j, temp, Y, t, x
Dim Outp

For i = 0 To 255
S(i) = i
Next

j = 1
For i = 0 To 255
If j > Len(key) Then j = 1
K(i) = Asc(mid(key, j, 1))
j = j + 1
Next

j = 0
For i = 0 To 255
j = (j + S(i) + K(i)) Mod 256
temp = S(i)
S(i) = S(j)
S(j) = temp
Next

i = 0
j = 0
For x = 1 To Len(inp)
i = (i + 1) Mod 256
j = (j + S(i)) Mod 256
temp = S(i)
S(i) = S(j)
S(j) = temp
t = (S(i) + (S(j) Mod 256)) Mod 256
Y = S(t)

'Outp = Outp & (Asc(mid(inp, x, 1)) Xor Y)
Outp = Outp & Chr(Asc(mid(inp, x, 1)) Xor Y) 'RC4算法
next

RC4 = Outp

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值