RC4 加密算法 C 语言实现
代码文件名
RC4.cpp Encrypt.h (代码详见后文) 备注:将以上两个文件放在相同的路径(建议不要放在中文路径
下)编译执行!编译环境
Microsoft Visual C++ 6.0
C-Free 5.0
代码解释
RC4 加密算法是大名鼎鼎的RSA 三人组中的头号人物Ron Rivest 在1987 年设计的密钥长度可变的流加密算法簇。之所以称其为簇,是由于其核心部分的S-box 长度可为任意,但一般为256字节。该算法的速度可以达到DES 加密的10倍左右。
RC4 算法的原理很简单,包括初始化算法和伪随机子密码生成算法两大部分。假设S-box 长度和密钥长度均为为n。先来看看算法的初始化部分(用类C伪代码表示):
for (i=0; i s=i; j=0; for (i=0; i { j=(j+s+k)%256; swap(s, s[j]); } 在初始化的过程中,密钥的主要功能是将S-box搅乱,i确保S-box的每个元素都得到 处理,j 保证S-box 的搅乱是随机的。而不同的S-box 在经过伪随机子密码生成算法的处理后可以得到不同的子密钥序列,并且,该序列是随机的: i=j=0; while ( 明文未结束) { ++i%=n; j=(j+s)%n; swap(s, s[j]); sub_k=s((s+s[j])% n); } 得到的子密码sub_k用以和明文进行xor运算,得到密文,解密过程也完全相同。 RC4加密算法在C++中的实现: RC4函数(加密/解密):其实RC4只有加密,将密文再加密一次,就是解密了。 GetKey函数:随机字符串产生器。 ByteToHex函数:把字节码转为十六进制码,一个字节两个十六进制。十六进制字符串非常适合在HTTP中传输。 HexToByte函数:把十六进制字符串,转为字节码。。 Encrypt函数:把字符串经RC4加密后,再把密文转为十六进制字符串返回,可直接用于传输。 Decrypt函数:直接密码十六进制字符串密文,再解密,返回字符串明文。 源代码 以下为Encrypt.h文件代码 #ifndef _ENCRYPT_RC4_ #defi ne _ENCRYPT_RC4_ #in clude #defi ne BOX_LEN 256 int GetKey(c onst un sig ned char* pass, int pass_le n, un sig ned char *out); int RC4(c onst un sig ned char* data, int data_le n, const un sig ned char* key, int key_le n, un sig ned char* out, i nt* out_le n); static void swap_byte( un sig ned char* a, un sig ned char* b); char* En crypt(co nst char* szSource, const char* szPassWord); // 加密,返回加密结果 char* Decrypt(co nst char* szSource, con st char* szPassWord); // 解密,返回解密结果 char* ByteToHex(c onst un sig ned char* vByte, const int vLe n); // 把字节码pbBuffer转为十六进制字符串,方便传输 unsigned char* HexToByte(const char* szHex); // 把十六进制字符串转为字节码pbBuffer,解码 #e ndif // #ifndef _ENCRYPT_RC4_ 以下为RC4.cpp文件代码 //////////////////////////////////////////////////////////// // // 版权信息 // // //////////////////////////////////////////////////////////// #in elude #in elude #i nclude #in elude "En crypt.h". const int max n=256+10; char* En crypt(co nst char* szSource, const char* szPassWord) // 加密,返回加密结果 { if(szSource == NULL || szPassWord == NULL) retur n NULL; un sig ned char* ret = new un sig ned char[strle n( szSource)]; int ret_le n = 0; if(RC4((unsigned char*)szSource, strle n( szSource), (un sig ned char*)szPassWord, strle n( szPassWord), ret, & ret_le n) == NULL) return NULL; char* ret2 = ByteToHex(ret, ret_le n); delete[] ret; return ret2; } char* Decrypt(co nst char* szSource, con st char* szPassWord) // 解密,返回解密结果 { if(szSource == NULL || (strle n(szSource)%2 != 0) || szPassWord == NULL) return NULL; un sig ned char* src = HexToByte(szSource); un sig ned char* ret = new un sig ned char[strle n( szSource) / 2 + 1]; int ret_le n = 0; memset(ret, strle n( szSource) / 2 + 1,0); if(RC4(src, strle n( szSource) / 2, (un sig ned char*)szPassWord, strle n(szPassWord), ret, & ret_le n) ==NULL) return NULL; ret[ret_len] = '\0'; return (char*)ret; } int RC4(const unsigned char* data, int data_len, const unsigned char* key, int key_len, unsigned char* out, int* out_len) { if (data == NULL || key == NULL || out == NULL) return NULL; unsigned char* mBox = new unsigned char[BOX_LEN]; if(GetKey(key, key_len, mBox) == NULL) return NULL; int i=0; int x=0; int y=0; for(int k = 0; k < data_len; k++) { x = (x + 1) % BOX_LEN; y = (mBox[x] + y) % BOX_LEN; swap_byte(&mBox[x], &mBox[y]); out[k] = data[k] A mBox[(mBox [ x] + mBox[y]) % BOX_LEN]; } *out_len = data_len; delete[] mBox; return -1; } int GetKey(const unsigned char* pass, int pass_len, unsigned char* out) { if(pass == NULL || out == NULL) return NULL; int i; for(i = 0; i < BOX_LEN; i++) out[i] = i; int j = 0; for(i = 0; i < BOX_LEN; i++) { j = (pass[i % pass_len] + out[i] + j) % BOX_LEN; swap_byte(&out[i], &out[j]); } return -1; } static void swap_byte(unsigned char* a, unsigned char* b) { unsigned char swapByte; swapByte = *a; *a = *b; *b = swapByte; } // 把字节码转为十六进制码,一个字节两个十六进制,内部为字符串分配空间 char* ByteToHex(const unsigned char* vByte, const int vLen) { if(!vByte) return NULL; char* tmp = new char[vLen * 2 + 1]; // 一个字节两个十六进制码,最后要多一个int '\0' tmp2; for (int i=0;i { tmp2 = (int)(vByte[i])/16; tmp[i*2] = (char)(tmp2+((tmp2>9)?'A'-10:'0')); tmp2 = (int)(vByte[i])%16; tmp[i*2+1] = (char)(tmp2+((tmp2>9)?'A'-10:'0')); } tmp[vLen * 2] = '\0'; return tmp; } // 把十六进制字符串,转为字节码,每两个十六进制字符作为一个字节 unsigned char* HexToByte(const char* szHex) { if(!szHex) return NULL; int iLen = strlen(szHex); if (iLen<=0 || 0!=iLen%2) return NULL; unsigned char* pbBuf = new unsigned char[iLen/2]; // 数据缓冲区 int tmp1, tmp2; for (int i=0;i { tmp1 = (int)szHex[i*2] - (((int)szHex[i*2]>='A')?'A'-10:'0'); if(tmp1>=16) return NULL; tmp2 = (int)szHex[i*2+1] - (((int)szHex[i*2+1]>='A')?'A'-10:'0'); if(tmp2>=16) return NULL; pbBuf[i] = (tmp1*16+tmp2); } return pbBuf; } int main (i nt argc,char *argv[]) { int size = 0; char Message[max n]; prin tf("\n=================================="); prin tf("\n RC4 "); prin tf("\n==================================\n"); printf("\n请输入需要加密的消息:”); sca nf("%s". Message); char source[max n]; strcpy(source,Message); char Key[max n]; printf("\n请输入符合要求的密钥:"); scanf("%s", Key); char pass[max n]; strcpy(pass,Key); char *result1 = NULL; char *result2 = NULL; result1 = En crypt(source, pass); printf("\n 加密结果(十六进制):%s\n", result"; result2 = Decrypt(result1, pass); printf("\n 解密结果(字符串式):%s\n\n", result2); delete []result1; delete []result2; return 0; }