这是我自己设计的加密算法,后面我附带上源码。已经提交给公司申请专利。
我将它公开求攻击,是想通过更多的人的思考,去发现它的弱点或不足。
我将之取名为CIC加密算法的原因,里边采用了一个联合体作为变换块,将在char与int之间转换,最后再转换为char。呵呵,随便取的名字。
里边核心的加密运算,我沿用了aes加密算法的。人家本来已经做的相当好了,我没设计出更好的,就沿用了。经过我自己在同一台电脑上测试,与从openssl里提取出来的AES加密算法对比效率,在加密效率上要高于AES的20%左右。安全性上,我自认为要比AES要高一些,至于各位认不认同,可以测试,可以拍砖。
代码本身就不多,如果真心要看,我想会一行行去看的,哪里对算法本身不理解,可以问哈,至于说看不懂代码……那建议还是好好回头看C基础吧。
各位看官,说了那么多,该上菜了:
cic.h:
#ifndef CIC
#define CIC
#define word8 unsigned char
#define word32 unsigned int
#define CIC_H 4
#define CIC_L 8
int cic_decrypt(word8 *msg,word8 *key);
int cic_encrypt(word8 *msg,word8 *key);
#endif
cic.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "cic.h"
#define word8 unsigned char
#define word32 unsigned int
#define CIC_H 4
#define CIC_L 8
int block_len;
union keybox
{
word8 key_ch[CIC_H*CIC_L];
word32 key_int[CIC_H];
}keybox;
union msgbox
{
word8 msg_ch[CIC_H*CIC_L];
word8 msg_ch2[CIC_H][CIC_L];
word32 msg_int[CIC_H];
}msgbox;
static word8 P[256]={
254,168,41,91,26,141,16,28,
171,150,255,119,107,39,22,19,
47,83,110,186,106,78,184,107,
243,37,32,170,8,172,120,27,
146,184,146,79,170,143,237,222,
93,157,148,182,172,105,68,178,
215,199,205,111,220,78,186,91,
58,12,52,14,250,121,36,222,
240,170,152,173,63,199,104,6,
27,191,25,105,211,58,1,226,
14,208,172,224,84,120,127,69,
209,67,155,186,86,74,5,69,
161,228,3,29,222,29,201,189,
110,100,84,55,230,132,151,203,
91,214,213,184,221,180,78,82,
65,48,29,85,36,6,97,121,
44,208,155,70,145,121,87,42,
220,244,15,201,239,46,76,148,
70,65,14,157,124,234,110,169,
17,61,130,135,106,151,129,161,
25,213,39,209,95,195,91,118,
239,215,16,198,245,32,105,102,
87,121,32,56,59,2,40,116,
201,209,145,250,50,228,173,230,
241,90,113,101,81,227,29,200,
47,118,32,215,1,66,53,104,
23,230,211,48,33,228,196,216,
242,83,19,83,131,213,44,112,
59,201,63,171,109,194,229,73,
35,55,6,164,26,124,250,195,
13,240,238,45,54,121,138,255,
19,45,209,61,102,82,72,102
};
static word8 P1[256]={
6,233,12,173,97,114,228,13,
199,235,185,13,129,200,62,105,
239,60,236,130,121,90,154,166,
201,122,77,145,55,222,81,52,
133,149,137,181,205,160,3,182,
183,84,207,218,239,255,18,133,
215,206,86,228,121,238,36,46,
191,242,213,41,230,117,165,239,
48,0,9,242,217,46,67,62,
77,13,51,254,152,200,112,66,
30,131,151,255,140,135,227,87,
14,159,254,27,195,193,23,143,
110,241,53,109,110,99,203,44,
240,224,140,129,99,106,128,39,
44,35,88,220,155,107,255,172,
30,73,113,113,181,12,208,155,
166,50,181,45,114,136,196,8,
7,147,131,108,55,47,108,188,
105,118,64,129,143,227,159,178,
88,183,214,51,166,156,248,155,
66,137,48,58,207,229,85,91,
252,239,191,198,254,93,89,137,
62,67,248,248,78,55,235,243,
34,179,212,103,125,187,182,23,
169,191,78,157,107,194,167,171,
54,188,233,151,159,61,113,23,
17,84,39,73,194,175,12,246,
229,3,19,23,34,175,50,152,
67,156,184,93,48,103,225,131,
29,194,168,232,2,23,220,237,
75,111,118,122,209,147,42,67,
10,112,60,74,124,192,149,165
};
static word8 S[256] = {
99, 124, 119, 123, 242, 107, 111, 197,
48, 1, 103, 43, 254, 215, 171, 118,
202, 130, 201, 125, 250, 89, 71, 240,
173, 212, 162, 175, 156, 164, 114, 192,
183, 253, 147, 38, 54, 63, 247, 204,
52, 165, 229, 241, 113, 216, 49, 21,
4, 199, 35, 195, 24, 150, 5, 154,
7, 18, 128, 226, 235, 39, 178, 117,
9, 131, 44, 26, 27, 110, 90, 160,
82, 59, 214, 179, 41, 227, 47, 132,
83, 209, 0, 237, 32, 252, 177, 91,
106, 203, 190, 57, 74, 76, 88, 207,
208, 239, 170, 251, 67, 77, 51, 133,
69, 249, 2, 127, 80, 60, 159, 168,
81, 163, 64, 143, 146, 157, 56, 245,
188, 182, 218, 33, 16, 255, 243, 210,
205, 12, 19, 236, 95, 151, 68, 23,
196, 167, 126, 61, 100, 93, 25, 115,
96, 129, 79, 220, 34, 42, 144, 136,
70, 238, 184, 20, 222, 94, 11, 219,
224, 50, 58, 10, 73, 6, 36, 92,
194, 211, 172, 98, 145, 149, 228, 121,
231, 200, 55, 109, 141, 213, 78, 169,
108, 86, 244, 234, 101, 122, 174, 8,
186, 120, 37, 46, 28, 166, 180, 198,
232, 221, 116, 31, 75, 189, 139, 138,
112, 62, 181, 102, 72, 3, 246, 14,
97, 53, 87, 185, 134, 193, 29, 158,
225, 248, 152, 17, 105, 217, 142, 148,
155, 30, 135, 233, 206, 85, 40, 223,
140, 161, 137, 13, 191, 230, 66, 104,
65, 153, 45, 15, 176, 84, 187, 22
};
static word8 Si[256] = {
82, 9, 106, 213, 48, 54, 165, 56,
191, 64, 163, 158, 129, 243, 215, 251,
124, 227, 57, 130, 155, 47, 255, 135,
52, 142, 67, 68, 196, 222, 233, 203,
84, 123, 148, 50, 166, 194, 35, 61,
238, 76, 149, 11, 66, 250, 195, 78,
8, 46, 161, 102, 40, 217, 36, 178,
118, 91, 162, 73, 109, 139, 209, 37,
114, 248, 246, 100, 134, 104, 152, 22,
212, 164, 92, 204, 93, 101, 182, 146,
108, 112, 72, 80, 253, 237, 185, 218,
94, 21, 70, 87, 167, 141, 157, 132,
144, 216, 171, 0, 140, 188, 211, 10,
247, 228, 88, 5, 184, 179, 69, 6,
208, 44, 30, 143, 202, 63, 15, 2,
193, 175, 189, 3, 1, 19, 138, 107,
58, 145, 17, 65, 79, 103, 220, 234,
151, 242, 207, 206, 240, 180, 230, 115,
150, 172, 116, 34, 231, 173, 53, 133,
226, 249, 55, 232, 28, 117, 223, 110,
71, 241, 26, 113, 29, 41, 197, 137,
111, 183, 98, 14, 170, 24, 190, 27,
252, 86, 62, 75, 198, 210, 121, 32,
154, 219, 192, 254, 120, 205, 90, 244,
31, 221, 168, 51, 136, 7, 199, 49,
177, 18, 16, 89, 39, 128, 236, 95,
96, 81, 127, 169, 25, 181, 74, 13,
45, 229, 122, 159, 147, 201, 156, 239,
160, 224, 59, 77, 174, 42, 245, 176,
200, 235, 187, 60, 131, 83, 153, 97,
23, 43, 4, 126, 186, 119, 214, 38,
225, 105, 20, 99, 85, 33, 12, 125
};
word8 mul(word8 *ch)
{
*ch=(P[*ch]+P1[*ch])%255;
return *ch;
}
word32 Round_num()
{
word8 i;
word32 tmp=0;
for(i=0;i<block_len;i++)
tmp+=(keybox.key_ch[i]&(1<<i))>>i;
return tmp;
}
void swp_ch(word8 *msg)
{
int i;
word8 tmp;
int half_len=block_len>>1;
for(i=0;i<half_len;i++)
{
tmp=msg[i];
msg[i]=msg[(msg[i])%(block_len)];
msg[(msg[i])%(block_len)]=tmp;
}
}
void Round_key(word8 *ch)
{
int i;
word8 tmp;
for(i=0;i<block_len;i++)
{
tmp=keybox.key_ch[i];
keybox.key_ch[i]^=mul(&tmp);
}
tmp=Round_num();
for(i=0;i<CIC_L;i++)
keybox.key_int[i]=(keybox.key_int[i]<<tmp)|(keybox.key_int[i]>>(32-tmp));//循环位移
swp_ch(keybox.key_ch);
}
int add_num_end(word8 *msg)
{
int i;
int num=0;
for(i=0;i<block_len;i++)
num+=(msg[i]&1);
return num;
}
int add_num_str(word8 *msg)
{
int i;
int num=0;
for(i=0;i<block_len;i++)
num+=((msg[i]>>7)&1);
return num;
}
void en_round_ch(word8 *msg)
{
int i,j;
word8 tmp;
/***********************start:以1字节为单位向左循环位移run_num位*****************************/
int run_num=add_num_end(msgbox.msg_ch);
for(j=0;j<run_num;j++)
{
tmp=msgbox.msg_ch[0];
for(i=0;i<block_len-1;i++)
{
msgbox.msg_ch[i]=msgbox.msg_ch[i+1];
}
msgbox.msg_ch[block_len-1]=tmp;
}
/***********************start:以1字节为单位向左循环位移run_num位*****************************/
run_num=add_num_str(keybox.key_ch);
for(i=0;i<CIC_L;i++)
msgbox.msg_int[i]=(msgbox.msg_int[i]<<run_num)|(msgbox.msg_int[i]>>(32-run_num));//循环位移
}
void de_round_ch(word8 *msg)
{
int i,j;
int run_num;
word8 tmp;
run_num=add_num_str(keybox.key_ch);
// printf("run_num:%d\n",run_num);
for(i=0;i<CIC_L;i++)
{
msgbox.msg_int[i]=(msgbox.msg_int[i]>>run_num)|(msgbox.msg_int[i]<<(32-run_num));//循环位移
}
/***********************start:以1字节为单位向右循环位移run_num位*****************************/
run_num=add_num_end(msgbox.msg_ch);
// printf("key:%s %d\n",msgbox.msg_ch,run_num);
for(j=0;j<run_num;j++)
{
tmp=msgbox.msg_ch[block_len-1];
for(i=block_len-1;i>=0;i--)
{
msgbox.msg_ch[i]=msgbox.msg_ch[i-1];
}
msgbox.msg_ch[0]=tmp;
}
/**********************end:以1字节为单位向右循环位移run_num位******************************/
}
void replace_ch(word8 array[256])
{
int i;
for(i=0;i<block_len;i++)
msgbox.msg_ch[i]=array[msgbox.msg_ch[i]];
}
void cry_or()
{
int i,j;
i=0;
j=block_len-1;
for(;i<block_len&&j>=0;)
{
msgbox.msg_ch[i]^=keybox.key_ch[j];
i+=2;
j-=2;
}
i=1;
for(;i<block_len;)
{
msgbox.msg_ch[i]^=keybox.key_ch[i];
i+=2;
}
}
void line_swp()//share
{
int i;
word8 tmp;
for(i=0;i<CIC_L;i++)
{
tmp=msgbox.msg_ch2[0][i];
msgbox.msg_ch2[0][i]=msgbox.msg_ch2[2][i];
msgbox.msg_ch2[2][i]=tmp;
tmp=msgbox.msg_ch2[1][i];
msgbox.msg_ch2[1][i]=msgbox.msg_ch2[3][i];
msgbox.msg_ch2[3][i]=tmp;
}
}
void line_or_en()
{
int i,j;
for(j=0;j<CIC_H-1;j++)
for(i=0;i<CIC_L;i++)
{
msgbox.msg_ch2[j][i]^=msgbox.msg_ch2[j+1][i];
}
for(i=0;i<CIC_L;i++)
{
msgbox.msg_ch2[0][i]^=P[i];
msgbox.msg_ch2[1][i]^=msgbox.msg_ch2[0][i];
msgbox.msg_ch2[2][i]^=msgbox.msg_ch2[1][i];
msgbox.msg_ch2[3][i]^=msgbox.msg_ch2[2][i];
msgbox.msg_ch2[0][i]^=P1[i];
}
for(i=1;i<CIC_L*CIC_H;i++)
msgbox.msg_ch[i]^=msgbox.msg_ch[i-1];
}
void line_or_de()
{
int i,j;
for(i=CIC_L*CIC_H-1;i>0;i--)
msgbox.msg_ch[i]^=msgbox.msg_ch[i-1];
for(i=0;i<CIC_L;i++)
{
msgbox.msg_ch2[0][i]^=P1[i];
msgbox.msg_ch2[3][i]^=msgbox.msg_ch2[2][i];
msgbox.msg_ch2[2][i]^=msgbox.msg_ch2[1][i];
msgbox.msg_ch2[1][i]^=msgbox.msg_ch2[0][i];
msgbox.msg_ch2[0][i]^=P[i];
}
for(j=CIC_H-2;j>=0;j--)
for(i=CIC_L-1;i>=0;i--)
{
msgbox.msg_ch2[j][i]^=msgbox.msg_ch2[j+1][i];
}
}
void father_or_en()
{
int i,j;
for(j=0;j<CIC_H;j++)
for(i=1;i<CIC_L;i++)
{
msgbox.msg_ch2[j][i]^=msgbox.msg_ch2[j][i-1];
}
for(i=0;i<CIC_L;i++)
{
msgbox.msg_ch2[0][i]^=keybox.key_ch[i];
msgbox.msg_ch2[0][i]^=P[keybox.key_ch[i]];
}
}
void father_or_de()
{
int i,j;
for(i=0;i<CIC_L;i++)
{
msgbox.msg_ch2[0][i]^=P[keybox.key_ch[i]];
msgbox.msg_ch2[0][i]^=keybox.key_ch[i];
}
for(j=CIC_H-1;j>=0;j--)
for(i=CIC_L-1;i>0;i--)
{
msgbox.msg_ch2[j][i]^=msgbox.msg_ch2[j][i-1];
}
}
void str_cpy(word8 *strget,word8 *source,word32 num)
{
word32 i;
for(i=0;i<num;i++)
strget[i]=source[i];
}
int cic_encrypt(word8 *msg,word8 *key)
{
#ifndef EN_BLOCK_LEN
block_len=CIC_H*CIC_L;
#define EN_BLOCK_LEN
#endif
/*
str_cpy(keybox.key_ch,key,block_len);
#ifndef EN_R_KEY
Round_key(keybox.key_ch);
#define EN_R_KEY
#endif
*/
str_cpy(msgbox.msg_ch,msg,block_len);
en_round_ch(msgbox.msg_ch);
replace_ch(S);
cry_or();
line_swp();
line_or_en();
father_or_en();
str_cpy(msg,msgbox.msg_ch,block_len);
return 1;
}
int cic_decrypt(word8 *msg,word8 *key)
{
#ifndef EN_BLOCK_LEN
block_len=CIC_H*CIC_L;
#define EN_BLOCK_LEN
#endif
/*
str_cpy(keybox.key_ch,key,block_len);
#ifndef DE_R_KEY
Round_key(keybox.key_ch);
#define DE_R_KEY
#endif
*/
str_cpy(msgbox.msg_ch,msg,block_len);
father_or_de();
line_or_de();
line_swp();
cry_or();
replace_ch(Si);
de_round_ch(msgbox.msg_ch);
str_cpy(msg,msgbox.msg_ch,block_len);
return 1;
}
cic.c[code=C/C++]
void main()
{
int i,start_time,end_time;
word8 ch_in[32];
word8 ch_key[32];
block_len=CIC_H*CIC_L;
strcpy(ch_in,"asdfghjklhzxcvbna8954321bb789012");
strcpy(ch_key,"12345678911234567890123456789asd");
printf("test_rts is:");
for(i=0;i<32;i++)
printf("%c",ch_in[i]);
printf("\n");
start_time=time(0);
str_cpy(keybox.key_ch,ch_key,32);
Round_key(keybox.key_ch);
printf("ch_key is:");
for(i=0;i<32;i++)
printf("%c",keybox.key_ch[i]);
printf("\n");
for(i=0;i<3276800;i++){
cic_encrypt(ch_in,ch_key);
cic_decrypt(ch_in,ch_key);
}
end_time=time(0);
printf("decrypt test:");
for(i=0;i<32;i++)
printf("%c",ch_in[i]);
printf("\n");
printf("crypt fast:%dMB/s\n",(100+100)/(end_time-start_time));
}
协议补充:该贴及源码,只可作为学习,测试之用。未经本人允许,不得转载,及用于商业用途,我保留追究法律责任的权利!
虽然我自己写的,自我感觉良好。但是还是感觉有些不足吧,说不上来,就是希望能通过这样的途径去完善它。
谢谢各位了
[培训]内核驱动高级班,冲击BAT一流互联网大厂工作,每周日13:00-18:00直播授课