+(NSString*)HloveyRC4:(NSString*)aInput key:(NSString*)aKey{ NSMutableArray *iS = [[NSMutableArray alloc] initWithCapacity:256]; NSMutableArray *iK = [[NSMutableArray alloc] initWithCapacity:256]; for (int i= 0; i<256; i++) { [iS addObject:[NSNumber numberWithInt:i]]; } int j=1; for (short i=0; i<256; i++) { UniChar c = [aKey characterAtIndex:i%aKey.length]; [iK addObject:[NSNumber numberWithChar:c]]; } j=0; for (int i=0; i<255; i++) { int is = [[iS objectAtIndex:i] intValue]; UniChar ik = (UniChar)[[iK objectAtIndex:i] charValue]; j = (j + is + ik)%256; NSNumber *temp = [iS objectAtIndex:i]; [iS replaceObjectAtIndex:i withObject:[iS objectAtIndex:j]]; [iS replaceObjectAtIndex:j withObject:temp]; } int i=0; j=0; NSString *result = aInput; for (short x=0; x<[aInput length]; x++) { i = (i+1)%256; int is = [[iS objectAtIndex:i] intValue]; j = (j+is)%256; int is_i = [[iS objectAtIndex:i] intValue]; int is_j = [[iS objectAtIndex:j] intValue]; int t = (is_i+is_j) % 256; int iY = [[iS objectAtIndex:t] intValue]; UniChar ch = (UniChar)[aInput characterAtIndex:x]; UniChar ch_y = ch^iY; result = [result stringByReplacingCharactersInRange:NSMakeRange(x, 1) withString:[NSString stringWithCharacters:&ch_y length:1]]; } [iS release]; [iK release]; return result;}
RC4算法的原理很简单,包括初始化算法(KSA)和伪随机子密码生成算法(PRGA)两大部分。假设S-box的长度为256,密钥长度为Len。先来看看算法的初始化部分(用C代码表示): 其中,参数1是一个256长度的char型数组,定义为: unsigned char sBox[256]; 参数2是密钥,其内容可以随便定义:char key[256]; 参数3是密钥的长度,Len = strlen(key); void rc4_init(unsigned char *s, unsigned char *key, unsigned long Len) { int i =0, j = 0, k[256] = { 0}; unsigned char tmp = 0; for(i=0;i<256;i++) { s[i]=i; k[i]=key[i%Len]; } for (i=0; i<256; i++) { j=(j+s[i]+k[i])%256; tmp = s[i]; s[i] = s[j]; //交换s[i]和s[j] s[j] = tmp; } } 在初始化的过程中,密钥的主要功能是将S-box搅乱,i确保S-box的每个元素都得到处理,j保证S-box的搅乱是随机的。而不同的S-box在经过伪随机子密码生成算法的处理后可以得到不同的子密钥序列,将S-box和明文进行xor运算,得到密文,解密过程也完全相同。 再来看看算法的加密部分(用C代码表示): 其中,参数1是上边rc4_init函数中,被搅乱的S-box; 参数2是需要加密的数据data; 参数3是data的长度. void rc4_crypt(unsigned char *s, unsigned char *Data, unsigned long Len) { int x = 0, y = 0, t = 0, i = 0; unsigned char tmp; for(i=0;i#include typedef unsigned long ULONG; void rc4_init(unsigned char *s, unsigned char *key, unsigned long Len) //初始化函数 { int i =0, j = 0; char k[256] = { 0}; unsigned char tmp = 0; for(i=0;i<256;i++) { s[i]=i; k[i]=key[i%Len]; } for (i=0; i<256; i++) { j=(j+s[i]+k[i])%256; tmp = s[i]; s[i] = s[j]; //交换s[i]和s[j] s[j] = tmp; } } void rc4_crypt(unsigned char *s, unsigned char *Data, unsigned long Len) //加解密 { int i = 0, j = 0, t = 0; unsigned long k = 0; unsigned char tmp; for(k=0;k