rc4和xxtea都为对称算法,不熟悉这两个算法的自行搜索,本文对两者的运行时间进行对比。
my_xxtea.cpp:
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/time.h>
using namespace std;
long get_mesl() {
struct timeval tv;
gettimeofday(&tv,NULL);
return tv.tv_sec*1000 + tv.tv_usec/1000;
}
long get_trick() {
struct timeval tv;
gettimeofday(&tv,NULL);
return tv.tv_sec*1000000 + tv.tv_usec;
}
#define MX (z>>5^y<<2) + (y>>3^z<<4)^(sum^y) + (k[p&3^e]^z);
/*
函数long btea(long* v, long n, long* k)
v 表示需要加解密的源数据,加解密的结果依然存在v变量里。
n 传入值大于1表示加密,小于-1表示解密
k 密钥
*/
long btea(int* v, int n, const int* k) {
unsigned long z=v[n-1], y=v[0], sum=0, e, DELTA=0x9e3779b9;
long p, q ;
if (n > 1) { /* Coding Part */
q = 6 + 52/n;
while (q-- > 0) {
sum += DELTA;
e = (sum >> 2) & 3;
for (p=0; p<n-1; p++) y = v[p+1], z = v[p] += MX;
y = v[0];
z = v[n-1] += MX;
}
return 0 ;
} else if (n < -1) { /* Decoding Part */
n = -n;
q = 6 + 52/n;
sum = q*DELTA ;
while (sum != 0) {
e = (sum >> 2) & 3;
for (p=n-1; p>0; p--) z = v[p-1], y = v[p] -= MX;
z = v[n-1];
y = v[0] -= MX;
sum -= DELTA;
}
return 0;
}
return 1;
}
int main()
{
int key[4] = {0x12345678, 0x734a67fc, 0x3367a642, 0x78432562};
const char *str = "ergwrewthwth234534626rgehgweh365786847843et2y5y356y3ergwrewthwth234534626rgehgweh365786847843et2y5y356y3erqg的如果日本人石头人体内惹她还认识他忽然惹她还认识他还散热太好sdfggggggggggggggggggggggggggggreeeeeeeeeeeeepiojaorjgearjgerjrejothgrweretnhgrwethnrtnkhrt儿歌网敢惹他还给我让他和肉体和热汤喝肉汤二哥如果dfgesfgbrtdfbrdbrstbsrthrthehwth5w6htttttttttjtryjtyurrrrrjetq34tw5hywtherthretherthethrgfgeh6hywe5hrtethertghertbfgbfgb56h5jk46kququw6uwjkm,3qy546ynhdfgn76rsAR5而他也我不56云南underu那边v也不能不能ubiquity4他vtb号 会被vying 亲提前哦并且并且哦不去日本国旗我如果如果vwhy一【2i【ionizer4【有【4确认提前进入过全额计入股权】】】】34 比你2有年又吧4她她分拆迁费太4太太45v与你妈咪不5635uu36uunu去逃避与你那你你 亲卡也5于5435而他给微软我比较有人我人机一盘ergwrewthwth234534626rgehgweh365786847843et2y5y356y3erqg的如果日本人石头人体内惹她还认识他忽然惹她还认识他还散热太好sdfggggggggggggggggggggggggggggreeeeeeeeeeeeepiojaorjgearjgerjrejothgrweretnhgrwethnrtnkhrt儿歌网敢惹他还给我让他和肉体和热汤喝肉汤二哥如果dfgesfgbrtdfbrdbrstbsrthrthehwth5w6htttttttttjtryjtyurrrrrjetq34tw5hywtherthretherthethrgfgeh6hywe5hrtethertghertbfgbfgb56h5jk46kququw6uwjkm,3qy546ynhdfgn76rsAR5而他也我不56云南underu那边v也不能不能ubiquity4他vtb号 会被vying 亲提前哦并且并且哦不去日本国旗我如果如果vwhy一【2i【ionizer4【有【4确认提前进入过全额计入股权】】】】34 比你2有年又吧4她她分拆迁费太4太太45v与你妈咪不5635uu36uunu去逃避与你那你你亲卡也5于5435而他给微软我比较有人我人机一盘ergwrewthwth234534626rgehgweh365786847843et2y5y356y3ergwrewthwth234534626rgehgweh365786847843et2y5y356y3ergwrewthwth234534626rgehgweh365786847843et2y5y356y3erqg的如果日本人石头人体内惹她还认识他忽然惹她还认识他还散热太好sdfggggggggggggggggggggggggggggreeeeeeeeeeeeepiojaorjgearjgerjrejothgrweretnhgrwethnrtnkhrt儿歌网敢惹他还给我让他和肉体和热汤喝肉汤二哥如果dfgesfgbrtdfbrdbrstbsrthrthehwth5w6htttttttttjtryjtyurrrrrjetq34tw5hywtherthretherthethrgfgeh6hywe5hrtethertghertbfgbfgb56h5jk46kququw6uwjkm,3qy546ynhdfgn76rsAR5而他也我不56云南underu那边v也不能不能ubiquity4他vtb号 会被vying 亲提前哦并且并且哦不去日本国旗我如果如果vwhy一【2i【ionizer4【有【4确认提前进入过全额计入股权】】】】34 比你2有年又吧4她她分拆迁费太4太太45v与你妈咪不5635uu36uunu去逃避与你那你你 亲卡也5于5435而他给微软我比较有人我人机一盘ergwrewthwth234534626rgehgweh365786847843et2y5y356y3erqg的如果日本人石头人体内惹她还认识他忽然惹她还认识他还散热太好sdfggggggggggggggggggggggggggggreeeeeeeeeeeeepiojaorjgearjgerjrejothgrweretnhgrwethnrtnkhrt儿歌网敢惹他还给我让他和肉体和热汤喝肉汤二哥如果dfgesfgbrtdfbrdbrstbsrthrthehwth5w6htttttttttjtryjtyurrrrrjetq34tw5hywtherthretherthethrgfgeh6hywe5hrtethertghertbfgbfgb56h5jk46kququw6uwjkm,3qy546ynhdfgn76rsAR5而他也我不56云南underu那边v也不能不能ubiquity4他vtb号 会被vying 亲提前哦并且并且哦不去日本国旗我如果如果vwhy一【2i【ionizer4【有【4确认提前进入过全额计入股权】】】】34 比你2有年又吧4她她分拆迁费太4太太45v与你妈咪不5635uu36uunu去逃避与你那你你 亲卡也5于5435而他给微软我比较有人我人机一盘ergwrewthwth234534626rgehgweh365786847843et2y5y356y3";
int len = strlen(str);
cout << len <<endl;
char encode_str[20*1024];
memset(encode_str, '\0', sizeof(encode_str));
strncpy(encode_str, str, len);
printf("-------------------------encry-----------------------------\n");
long start = get_trick();
btea((int*)encode_str, len, key);
long spend = get_trick() - start;
printf("spend:%ld us\n", spend);
//printf("len:%d\n", len);
//cout <<"encode:"<< encode_str << endl;
//cout << strlen(encode_str) << endl;
printf("-------------------------decry-------------------------------\n");
start = get_trick();
btea((int*)encode_str, -len, key);
spend = get_trick() - start;
printf("spend:%ld us\n", spend);
cout << "decode:"<<encode_str << endl;
cout << strlen(encode_str) << endl;
printf("-------------------------long cry-------------------------------\n");
const int LOOP = 10000; // 一万次
start = get_trick();
int i;
for(i=0; i<LOOP; ++i) {
btea((int*)encode_str, len, key);;//加密
btea((int*)encode_str, -len, key);//解密
}
spend = get_trick() - start;
printf("spend:%ld us\n", spend);
return 0;
}
my_rc4.c
//程序开始
#include<stdio.h>
#include<string.h>
#include <sys/time.h>
typedef unsigned longULONG;
long get_mesl() {
struct timeval tv;
gettimeofday(&tv,NULL);
return tv.tv_sec*1000 + tv.tv_usec/1000;
}
long get_trick() {
struct timeval tv;
gettimeofday(&tv,NULL);
return tv.tv_sec*1000000 + tv.tv_usec;
}
/*初始化函数*/
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*s2, int slen, unsigned char*Data, unsigned long Len)
{
memcpy(s2, s, slen);
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];
}
memcpy(s, s2, slen);
}
int main()
{
unsigned char s[256] = { 0 }, s2[256] = { 0 };//S-box
int slen = sizeof(s);
char orig_key[256] = { "justfortest" };
char key[256] = {" c830df6e9ba06988d27a14fc679e24c9"}; //md5处理生成散列值
//char pData[1024] = {"ergwrewthwth234534626rgehgweh365786847843et2y5y356y3ergwrewthwth234534626rgehgweh365786847843et2y5y356y3erqg的如果日本人石头人体内惹她还认识他忽然惹她还认识他还散热太好sdfggggggggggggggggggggggggggggreeeeeeeeeeeeepiojaorjgearjgerjrejothgrweretnhgrwethnrtnkhrt儿歌网敢惹他还给我让他和肉体和热汤喝肉汤二哥如果dfgesfgbrtdfbrdbrstbsrthrthehwth5w6htttttttttjtryjtyurrrrrjetq34tw5hywtherthretherthethrgfgeh6hywe5hrtethertghertbfgbfgb56h5jk46kququw6uwjkm,3qy546ynhdfgn76rsAR5而他也我不56云南underu那边v也不能不能ubiquity4他vtb号 会被vying 亲提前哦并且并且哦不去日本国旗我如果如果vwhy一【2i【ionizer4【有【4确认提前进入过全额计入股权】】】】34 比你2有年又吧4她她分拆迁费太4太太45v与你妈咪不5635uu36uunu去逃避与你那你你 亲卡也5于5435而他给微软我比较有人我人机一盘ergwrewthwth234534626rgehgweh365786847843et2y5y356y3"};
char pData[4 * 1024] = {"ergwrewthwth234534626rgehgweh365786847843et2y5y356y3ergwrewthwth234534626rgehgweh365786847843et2y5y356y3erqg的如果日本人石头人体内惹她还认识他忽然惹她还认识他还散热太好sdfggggggggggggggggggggggggggggreeeeeeeeeeeeepiojaorjgearjgerjrejothgrweretnhgrwethnrtnkhrt儿歌网敢惹他还给我让他和肉体和热汤喝肉汤二哥如果dfgesfgbrtdfbrdbrstbsrthrthehwth5w6htttttttttjtryjtyurrrrrjetq34tw5hywtherthretherthethrgfgeh6hywe5hrtethertghertbfgbfgb56h5jk46kququw6uwjkm,3qy546ynhdfgn76rsAR5而他也我不56云南underu那边v也不能不能ubiquity4他vtb号 会被vying 亲提前哦并且并且哦不去日本国旗我如果如果vwhy一【2i【ionizer4【有【4确认提前进入过全额计入股权】】】】34 比你2有年又吧4她她分拆迁费太4太太45v与你妈咪不5635uu36uunu去逃避与你那你你 亲卡也5于5435而他给微软我比较有人我人机一盘ergwrewthwth234534626rgehgweh365786847843et2y5y356y3erqg的如果日本人石头人体内惹她还认识他忽然惹她还认识他还散热太好sdfggggggggggggggggggggggggggggreeeeeeeeeeeeepiojaorjgearjgerjrejothgrweretnhgrwethnrtnkhrt儿歌网敢惹他还给我让他和肉体和热汤喝肉汤二哥如果dfgesfgbrtdfbrdbrstbsrthrthehwth5w6htttttttttjtryjtyurrrrrjetq34tw5hywtherthretherthethrgfgeh6hywe5hrtethertghertbfgbfgb56h5jk46kququw6uwjkm,3qy546ynhdfgn76rsAR5而他也我不56云南underu那边v也不能不能ubiquity4他vtb号 会被vying 亲提前哦并且并且哦不去日本国旗我如果如果vwhy一【2i【ionizer4【有【4确认提前进入过全额计入股权】】】】34 比你2有年又吧4她她分拆迁费太4太太45v与你妈咪不5635uu36uunu去逃避与你那你你亲卡也5于5435而他给微软我比较有人我人机一盘ergwrewthwth234534626rgehgweh365786847843et2y5y356y3ergwrewthwth234534626rgehgweh365786847843et2y5y356y3ergwrewthwth234534626rgehgweh365786847843et2y5y356y3erqg的如果日本人石头人体内惹她还认识他忽然惹她还认识他还散热太好sdfggggggggggggggggggggggggggggreeeeeeeeeeeeepiojaorjgearjgerjrejothgrweretnhgrwethnrtnkhrt儿歌网敢惹他还给我让他和肉体和热汤喝肉汤二哥如果dfgesfgbrtdfbrdbrstbsrthrthehwth5w6htttttttttjtryjtyurrrrrjetq34tw5hywtherthretherthethrgfgeh6hywe5hrtethertghertbfgbfgb56h5jk46kququw6uwjkm,3qy546ynhdfgn76rsAR5而他也我不56云南underu那边v也不能不能ubiquity4他vtb号 会被vying 亲提前哦并且并且哦不去日本国旗我如果如果vwhy一【2i【ionizer4【有【4确认提前进入过全额计入股权】】】】34 比你2有年又吧4她她分拆迁费太4太太45v与你妈咪不5635uu36uunu去逃避与你那你你 亲卡也5于5435而他给微软我比较有人我人机一盘ergwrewthwth234534626rgehgweh365786847843et2y5y356y3erqg的如果日本人石头人体内惹她还认识他忽然惹她还认识他还散热太好sdfggggggggggggggggggggggggggggreeeeeeeeeeeeepiojaorjgearjgerjrejothgrweretnhgrwethnrtnkhrt儿歌网敢惹他还给我让他和肉体和热汤喝肉汤二哥如果dfgesfgbrtdfbrdbrstbsrthrthehwth5w6htttttttttjtryjtyurrrrrjetq34tw5hywtherthretherthethrgfgeh6hywe5hrtethertghertbfgbfgb56h5jk46kququw6uwjkm,3qy546ynhdfgn76rsAR5而他也我不56云南underu那边v也不能不能ubiquity4他vtb号 会被vying 亲提前哦并且并且哦不去日本国旗我如果如果vwhy一【2i【ionizer4【有【4确认提前进入过全额计入股权】】】】34 比你2有年又吧4她她分拆迁费太4太太45v与你妈咪不5635uu36uunu去逃避与你那你你 亲卡也5于5435而他给微软我比较有人我人机一盘ergwrewthwth234534626rgehgweh365786847843et2y5y356y3"};
unsigned long len = strlen(pData);
int i;
// printf("pData=%s\n", pData);
printf("key=%s,length=%d\n", key, strlen(key));
printf("len:%d\n", len);
/*
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");
*/
printf("-------------------------encry-----------------------------\n");
long start = get_trick();
rc4_crypt(s, s2, slen, (unsigned char*)pData, len);//加密
long spend = get_trick() - start;
printf("spend:%ld us\n", spend);
//printf("pData:%s\n", pData);
/*
int elen = strlen(pData);
printf("%d\n", elen);
unsigned char* tData = (unsigned char*) pData;
for (i=0; i<elen; ++i) {
printf("%02X ", tData[i]);
}
printf("\n");
*/
printf("-------------------------decry-------------------------------\n");
start = get_trick();
rc4_crypt(s, s2, slen, (unsigned char*)pData, len);//解密
spend = get_trick() - start;
printf("spend:%ld us\n", spend);
printf("pData:%s\n", pData);
printf("%d\n", strlen(pData));
printf("-------------------------long cry-------------------------------\n");
const int LOOP = 10000; // 一万次
start = get_trick();
for(i=0; i<LOOP; ++i) {
rc4_crypt(s, s2, slen, (unsigned char*)pData, len);//加密
rc4_crypt(s, s2, slen, (unsigned char*)pData, len);//解密
}
spend = get_trick() - start;
printf("spend:%ld us\n", spend);
return 0;
}
Makefile文件:
XXTEA=my_xxtea.cpp
RC4=my_rc4.c
CFLAG=-O0 -ggdb -static
build:
g++ $(CFLAG) $(XXTEA) -o xxtea
gcc $(CFLAG) $(RC4) -o rc4
clean:
rm xxtea rc4