(已解决)遇到一个看不懂的函数,感觉自己傻乎乎。。。
本帖最后由 与龙共舞 于 2018-8-15 11:38 编辑以前小公司嵌入式终端和平台收发数据都是明文,现在我司也加密了。网络数据加密,蓝牙数据加密,昨天看到刷卡RFID的数据也是加密了。
函数:
void DecryStr(unsigned char* str,unsigned char* key,unsigned char* presult)//解密函数8个字节
{
unsigned char subkey;
makeKey(key, subkey);
desData(1,str,presult,subkey);
conversePermutation(presult);
}对一个str的解密,第一步是做钥匙(能看懂)第二步是解密(能看懂)主要是利用C=A^B加密 然后C^B就回到A吧。所以A是明文,B是秘钥,C是密文(我自己理解的ASE算法)
第三步就看不懂了,这个是干啥呀,拿出来问问:
void conversePermutation(unsigned char* inData)
{
unsigned char newData={0,0,0,0,0,0,0,0};
int i;
for(i=0;i<64;i++)
{
if(( inData>>3] & (1<<(7-(BitCP&7))) )!=0 )
newData=newData|(1<<(7-(i&7)));
}
for(i=0;i<8;i++)
{
inData=newData;
}
}看上去就是对一个string倒腾。然后用到了一个常量的数组。
const unsigned char BitCP =
{ 36, 7, 40, 60, 43, 19, 47, 23,
34, 4, 39, 8, 42, 26, 46, 29,
33, 1, 38, 16, 57, 18, 51, 28,
32, 0, 37, 12, 56, 10, 45, 21,
53, 3, 6, 61, 44, 54, 5, 35,
15, 48, 55, 58, 20, 22, 31, 25,
9, 41, 13, 14, 59, 30, 2, 63,
11, 27, 17, 49, 62, 24, 52, 50 };这个数组我看了是0--63的乱序排列。
问题1:这个函数啥意思--------------------具体做啥暂时不知道 是解密的吧 还有多学点english
问题2:string经过这个函数以后就变化了,用什么办法可以还原呢----------有的 看配套的函数
++++++++++++++++++++20180815解答+++++++++++++
昨天看微信文章DES 和3DES 看到图片列在一起初始转置表--终止转职表 我有突然想起我的这个问题,
解铃还需系的人呀 工程里面果然我也找到了它的配偶,下面是我自己的加密解密,还是不知道叫什么名字。。。。DES吧
#include<stdio.h>
#include<string.h>
typedef unsigned charuint8_t;
typedef unsigned short uint16_t;
const unsigned char BitIP =
{ 25, 17, 54, 33, 9, 38, 34, 1,
11, 48, 29, 56, 27, 50, 51, 40,
19, 58, 21, 5, 44, 31, 45, 7,
61, 47, 13, 57, 23, 15, 53, 46,
24, 16, 8, 39, 0, 26, 18, 10,
2, 49, 12, 4, 36, 30, 14, 6,
41, 59, 63, 22, 62, 32, 37, 42,
28, 20, 43, 52, 3, 35, 60, 55 };
const unsigned char BitCP =
{ 36, 7, 40, 60, 43, 19, 47, 23,
34, 4, 39, 8, 42, 26, 46, 29,
33, 1, 38, 16, 57, 18, 51, 28,
32, 0, 37, 12, 56, 10, 45, 21,
53, 3, 6, 61, 44, 54, 5, 35,
15, 48, 55, 58, 20, 22, 31, 25,
9, 41, 13, 14, 59, 30, 2, 63,
11, 27, 17, 49, 62, 24, 52, 50 };
void initPermutation(unsigned char* inData)
{
unsigned char newData={0,0,0,0,0,0,0,0};
int i;
for(i=0;i<64;i++)
{
if((inData>>3]&(1<<(7-(BitIP&7))))!=0)
newData=newData|(1<<(7-(i&7)));
}
for(i=0;i<8;i++)
{
inData=newData;
}
}
void conversePermutation(unsigned char* inData)
{
unsigned char newData={0,0,0,0,0,0,0,0};
int i;
for(i=0;i<64;i++)
{
if(( inData>>3] & (1<<(7-(BitCP&7))) )!=0 )
newData=newData|(1<<(7-(i&7)));
}
for(i=0;i<8;i++)
{
inData=newData;
}
}
unsigned char str={0xF6,0x56,0x21,0x33,0x9E,0x25,0x29,0xD3};
int main(void)
{
int i;
for(i=0;i<8;i++) printf("%02X",str); printf("\n");
initPermutation(str);
for(i=0;i<8;i++) printf("%02X",str); printf("\n");
conversePermutation(str);
for(i=0;i<8;i++) printf("%02X",str); printf("\n");
return 0;
}
F65621339E2529D3
0D9C16780E8F6F1D
F65621339E2529D3
本帖最后由 stm1024 于 2018-8-1 10:34 编辑
这个函数简单来说就是把字符串给你打乱了,做简单的加密,既然加密算法你都知道了,直接还原肯定不是难事。
简单的分析认为,这个InData应该是一个8字节的数据,经过处理后,就变成了另外的一个8字节数据了。
通常有两种方法:
一种是暴力枚举,这个加密算法比较友好,原字节和新字节都是一一对应的,所以找到0x00-0xff与之对应的每一个新字符,然后反向查找即可,不过建立这个的前提是,原字节和新字节应该是一一对应的关系,不允许存在类似 0x12和0x33都对应0x78这种问题。
另一种方法就是深入的算法攻击,相当于是求解这个加密函数的逆函数,同样需要存在一一映射关系。先拿第一种方法做测试:
#include <stdio.h>
#include <stdlib.h>
const unsigned char BitCP =
{ 36, 7, 40, 60, 43, 19, 47, 23,
34, 4, 39, 8, 42, 26, 46, 29,
33, 1, 38, 16, 57, 18, 51, 28,
32, 0, 37, 12, 56, 10, 45, 21,
53, 3, 6, 61, 44, 54, 5, 35,
15, 48, 55, 58, 20, 22, 31, 25,
9, 41, 13, 14, 59, 30, 2, 63,
11, 27, 17, 49, 62, 24, 52, 50 };
void conversePermutation(unsigned char* inData)
{
unsigned char newData={0,0,0,0,0,0,0,0};
int i;
for(i=0;i<64;i++)
{
if((inData>>3]&(1<<(7-(BitCP&7))))!=0)
newData=newData|(1<<(7-(i&7)));
}
for(i=0;i<8;i++)
{
inData=newData;
}
}
int main()
{
unsigned char text;
int i,j;
for(i=0;i<32;i++)
{
for(j=0;j<8;j++)
text=8*i+j;
conversePermutation(text);
for(j=0;j<8;j++)
printf("%02x\t%02x\n",8*i+j,text);
}
return 0;
}
这段代码的目的是计算0x00-0xff之间对应的所有新字符,结果如下:
00 02
01 00
02 00
03 22
04 94
05 86
06 05
07 08
08 92
09 40
0a 01
0b 32
0c 9c
0d 8e
0e 05
0f 0a
10 0e
11 00
12 02
13 22
14 d5
15 86
16 0d
17 c8
18 9e
19 40
1a 03
1b 32
1c dd
1d 8e
1e 0d
1f ca
20 02
21 8c
22 04
23 26
24 94
25 96
26 07
27 09
28 92
29 cc
2a 05
2b 36
2c 9c
2d 9e
2e 07
2f 0b
30 0e
31 8c
32 06
33 26
34 d5
35 96
36 0f
37 c9
38 9e
39 cc
3a 07
3b 36
3c dd
3d 9e
3e 0f
3f cb
40 02
41 00
42 c8
43 22
44 94
45 87
46 c5
47 38
48 92
49 40
4a c9
4b 32
4c 9c
4d 8f
4e c5
4f 3a
50 0e
51 00
52 ca
53 22
54 d5
55 87
56 cd
57 f8
58 9e
59 40
5a cb
5b 32
5c dd
5d 8f
5e cd
5f fa
60 02
61 8c
62 cc
63 26
64 94
65 97
66 c7
67 39
68 92
69 cc
6a cd
6b 36
6c 9c
6d 9f
6e c7
6f 3b
70 0e
71 8c
72 ce
73 26
74 d5
75 97
76 cf
77 f9
78 9e
79 cc
7a cf
7b 36
7c dd
7d 9f
7e cf
7f fb
80 22
81 10
82 10
83 ea
84 94
85 c6
86 05
87 0c
88 b2
89 50
8a 11
8b fa
8c 9c
8d ce
8e 05
8f 0e
90 2e
91 10
92 12
93 ea
94 d5
95 c6
96 0d
97 cc
98 be
99 50
9a 13
9b fa
9c dd
9d ce
9e 0d
9f ce
a0 22
a1 9c
a2 14
a3 ee
a4 94
a5 d6
a6 07
a7 0d
a8 b2
a9 dc
aa 15
ab fe
ac 9c
ad de
ae 07
af 0f
b0 2e
b1 9c
b2 16
b3 ee
b4 d5
b5 d6
b6 0f
b7 cd
b8 be
b9 dc
ba 17
bb fe
bc dd
bd de
be 0f
bf cf
c0 22
c1 10
c2 d8
c3 ea
c4 94
c5 c7
c6 c5
c7 3c
c8 b2
c9 50
ca d9
cb fa
cc 9c
cd cf
ce c5
cf 3e
d0 2e
d1 10
d2 da
d3 ea
d4 d5
d5 c7
d6 cd
d7 fc
d8 be
d9 50
da db
db fa
dc dd
dd cf
de cd
df fe
e0 22
e1 9c
e2 dc
e3 ee
e4 94
e5 d7
e6 c7
e7 3d
e8 b2
e9 dc
ea dd
eb fe
ec 9c
ed df
ee c7
ef 3f
f0 2e
f1 9c
f2 de
f3 ee
f4 d5
f5 d7
f6 cf
f7 fd
f8 be
f9 dc
fa df
fb fe
fc dd
fd df
fe cf
ff ff
不知道这么分析是否正确
本帖最后由 与龙共舞 于 2018-8-1 11:21 编辑
stm1024 发表于 2018-8-1 09:40
这个函数简单来说就是把字符串给你打乱了,做简单的加密,既然加密算法你都知道了,直接还原肯定不是难事。 ...
这个循环挺高级的 0--0xff的双循环
所以现在是成功做了一个对应关系表吗 0--0xff和右边对应起来 但是多对一的情况出现了 desData这个是啥?DES加密? 类似的可以使用Python解密小技巧么
页:
[1]