openssl RC4 加解密
原创
©著作权归作者所有:来自51CTO博客作者jacklicto的原创作品,请联系作者获取转载授权,否则将追究法律责任
轻量级:TEA、RC系列(RC4),Blowfish (不常换密钥),相对安全级别不高的情况可以使用,速度快;
注意:加解密,请使用不同的key buf;RC4()调用前后,s_table已经不同,如果解密用同一个,会失败;
加密:
//加密
RC4_KEY s_table;
RC4_set_key(&s_table, strlen(key), (unsigned char *)key); //初始化
RC4(&s_table, dataLen, (unsigned char *)data, buffer1); //加密
解密:
//解密
RC4_KEY s_table;
RC4_set_key(&s_table, strlen(key), (unsigned char *)key); //初始化
RC4(&s_table, dataLen, buffer1, (unsigned char *)buffer2); //解密
测试函数:
static void rc4Test(char *key, unsigned char *data, int dataLen) {
unsigned char * buffer1 = new unsigned char[dataLen ];
unsigned char * buffer2 = new unsigned char[dataLen ];
printf("\nopenSSL:\n");
long long start1 = getCurrentMillisecondCount();
//加密
RC4_KEY s_table;
RC4_set_key(&s_table, strlen(key), (unsigned char *)key); //初始化
RC4(&s_table, dataLen, (unsigned char *)data, buffer1); //加密
long long enTime = getCurrentMillisecondCount();
printf("enTime = %lld \n", enTime - start1);
//解密
RC4_set_key(&s_table, strlen(key), (unsigned char *)key); //初始化
RC4(&s_table, dataLen, buffer1, (unsigned char *)buffer2); //解密
long long deTime = getCurrentMillisecondCount() - enTime;
printf("deTime = %lld \n", deTime);
delete[]buffer1;
delete[]buffer2;
}
如果只有RC4,完全没必要用openssl实现
以下是 C 语言实现的 RC4
RC4加密算法
RC4算法的特点是算法简单,运行速度快,而且密钥长度是可变的,可变范围为1-256字节(8-2048比特),在如今技术支持的前提下,当密钥长度为128比特时,用暴力法搜索密钥已经不太可行,所以可以预见RC4的密钥范围任然可以在今后相当长的时间里抵御暴力搜索密钥的攻击。实际上,如今也没有找到对于128bit密钥长度的RC4加密算法的有效攻击方法。
RC4算法的原理很简单,包括初始化算法(KSA)和伪随机子密码生成算法(PRGA)两大部分。假设S-box的长度为256,密钥长度为Len。先来看看算法的初始化部分(用C代码表示):
其中,参数1是一个256长度的char型数组,定义为: unsigned char sBox[256];
参数2是密钥,其内容可以随便定义:char key[256];
参数3是密钥的长度,Len = strlen(key);
#include<stdio.h>
#include<string.h>
typedef unsigned longULONG;
/*初始化函数*/
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<Len; k++)
{
i = (i + 1) % 256;
j = (j + s[i]) % 256;
tmp = s[i];
s[i] = s[j];//交换s[x]和s[y]
s[j] = tmp;
t = (s[i] + s[j]) % 256;
Data[k] ^= s[t];
}
}
int main()
{
unsigned char s[256] = { 0 }, s2[256] = { 0 };//S-box
char key[256] = { "justfortest" };
char pData[512] = "这是一个用来加密的数据Data";
unsigned long len = strlen(pData);
int i;
printf("pData=%s\n", pData);
printf("key=%s,length=%d\n\n", key, strlen(key));
rc4_init(s, (unsigned char*)key, strlen(key));//已经完成了初始化
printf("完成对S[i]的初始化,如下:\n\n");
for (i = 0; i<256; i++)
{
printf("%02X", s[i]);
if (i && (i + 1) % 16 == 0)putchar('\n');
}
printf("\n\n");
for (i = 0; i<256; i++)//用s2[i]暂时保留经过初始化的s[i],很重要的!!!
{
s2[i] = s[i];
}
printf("已经初始化,现在加密:\n\n");
rc4_crypt(s, (unsigned char*)pData, len);//加密
printf("pData=%s\n\n", pData);
printf("已经加密,现在解密:\n\n");
//rc4_init(s,(unsignedchar*)key,strlen(key));//初始化密钥
rc4_crypt(s2, (unsigned char*)pData, len);//解密
printf("pData=%s\n\n", pData);
return 0;
}