3DES.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200
  1. #include "StdAfx.h"
  2. #include "DES.h"
  3. /************************************************************************
  4. * DES/3DES原理:
  5. * 1-1、变换密钥
  6. * 1-1-1、取得64位的密钥,每个第8位作为奇偶校验位。程序上无需任何操作
  7. *
  8. * 1-2、变换密钥
  9. * 1-2-1、舍弃64位密钥中的奇偶校验位,根据下表(PC-1)进行密钥变换(换位)得到56位的密钥,在变换中,奇偶校验位被舍弃
  10. *
  11. * Permuted Choice 1 (PC-1),本表表示的数组下标从1开始,因此转换时需要:PC1_Table[i]-1
  12. * 57, 49, 41, 33, 25, 17, 9,
  13. * 1, 58, 50, 42, 34, 26, 18,
  14. * 10, 2, 59, 51, 43, 35, 27,
  15. * 19, 11, 3, 60, 52, 44, 36,
  16. * 63, 55, 47, 39, 31, 23, 15,
  17. * 7, 62, 54, 46, 38, 30, 22,
  18. * 14, 6, 61, 53, 45, 37, 29,
  19. * 21, 13, 5, 28, 20, 12, 4
  20. *
  21. * 1-2-2、将变换后的密钥分为两个部分,开始的28位称为C[0],最后的28位称为D[0]
  22. * 1-2-3、生成16个子密钥, I = 1。<这里假设I为生成密钥的轮数,它的基数是从1开始,即I取值为 1 – 16 >
  23. * 1-2-3-1、同时将C[I]、D[I]左移1位或2位,根据I值决定左移的位数。见下表:
  24. * I: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
  25. * 左移位数: 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1
  26. * <每轮移位输出的C[I] D[I]作为下一轮移位时输入的C[I] D[I]
  27. * 这里有个关键点,进行解密时这里是向右移,而且在I = 1时无需移位。即:
  28. * 右移位数: 0, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1
  29. * 解密时也只有这里也有区别,其它的地方和加密是一样的,DES算法的很多换位表是互反的,即可能你用
  30. * 表1换位完后,用表2再换一遍就又和原始值一样了>
  31. * 1-2-3-2、将C[I]D[I]作为一个整体按下表(PC-2)变换,得到48位的K[I]
  32. *
  33. * Permuted Choice 2 (PC-2),本表表示的数组下标从1开始,因此转换时需要:PC2_Table[i]-1
  34. * 14, 17, 11, 24, 1, 5,
  35. * 3, 28, 15, 6, 21, 10,
  36. * 23, 19, 12, 4, 26, 8,
  37. * 16, 7, 27, 20, 13, 2,
  38. * 41, 52, 31, 37, 47, 55,
  39. * 30, 40, 51, 45, 33, 48,
  40. * 44, 49, 39, 56, 34, 53,
  41. * 46, 42, 50, 36, 29, 32
  42. *
  43. * 1-2-3-3、从1-2-3-1处循环执行,直到K[16]被计算完成
  44. *
  45. * 2、处理64位的数据
  46. * 2-1、取得64位的数据,如果数据长度不足64位,应该将其扩展为64位(例如补零)
  47. * <补什么要看具体数据,只能补数据取值范围外的字符,如果数据取值范围是0x00 – 0XFF而且还不是64位整倍数,那就要另想办法,比如在数据前面加上长度什么的>
  48. * 2-2、将64位数据按下表变换(IP)
  49. *
  50. * Initial Permutation (IP),本表表示的数组下标从1开始,因此转换时需要:IP_Table[i]-1
  51. * 58, 50, 42, 34, 26, 18, 10, 2,
  52. * 60, 52, 44, 36, 28, 20, 12, 4,
  53. * 62, 54, 46, 38, 30, 22, 14, 6,
  54. * 64, 56, 48, 40, 32, 24, 16, 8,
  55. * 57, 49, 41, 33, 25, 17, 9, 1,
  56. * 59, 51, 43, 35, 27, 19, 11, 3,
  57. * 61, 53, 45, 37, 29, 21, 13, 5,
  58. * 63, 55, 47, 39, 31, 23, 15, 7
  59. *
  60. * 2-3、将变换后的数据分为两部分,开始的32位称为L[0],最后的32位称为R[0]
  61. * 2-4、用16个子密钥加密数据,初始I=1
  62. * 2-4-1、将32位的R[I-1]按下表(E)扩展为48位的E[I-1]
  63. * Expansion (E),本表表示的数组下标从1开始,因此转换时需要:E_Table[i]-1
  64. * 32, 1, 2, 3, 4, 5,
  65. * 4, 5, 6, 7, 8, 9,
  66. * 8, 9, 10, 11, 12, 13,
  67. * 12, 13, 14, 15, 16, 17,
  68. * 16, 17, 18, 19, 20, 21,
  69. * 20, 21, 22, 23, 24, 25,
  70. * 24, 25, 26, 27, 28, 29,
  71. * 28, 29, 30, 31, 32, 1
  72. *
  73. * 2-4-2、异或E[I-1]和K[I],即E[I-1] XOR K[I] <上面生成的16组子密钥就是在这里用到的>
  74. * 2-4-3、将异或后的结果分为8个6位长的部分,第1位到第6位称为B[1],第7位到第12位称为B[2],依此类推,第43位到第48位称为B[8]
  75. * 2-4-4、按S表变换所有的B[J],初始J=1。所有在S表的值都被当作4位长度处理
  76. * 2-4-4-1、将B[J]的第1位和第6位组合为一个2位长度的变量M,M作为在S[J]中的行号
  77. * 2-4-4-2、将B[J]的第2位到第5位组合,作为一个4位长度的变量N,N作为在S[J]中的列号
  78. * 2-4-4-3、用S[J][M][N]来取代B[J]
  79. *
  80. * Substitution Box
  81. * S[1]
  82. * 14 4 13 1 2 15 11 8 3 10 6 12 5 9 0 7
  83. * 0 15 7 4 14 2 13 1 10 6 12 11 9 5 3 8
  84. * 4 1 14 8 13 6 2 11 15 12 9 7 3 10 5 0
  85. * 15 12 8 2 4 9 1 7 5 11 3 14 10 0 6 13
  86. *
  87. * S[2]
  88. * 15 1 8 14 6 11 3 4 9 7 2 13 12 0 5 10
  89. * 3 13 4 7 15 2 8 14 12 0 1 10 6 9 11 5
  90. * 0 14 7 11 10 4 13 1 5 8 12 6 9 3 2 15
  91. * 13 8 10 1 3 15 4 2 11 6 7 12 0 5 14 9
  92. *
  93. * S[3]
  94. * 10 0 9 14 6 3 15 5 1 13 12 7 11 4 2 8
  95. * 13 7 0 9 3 4 6 10 2 8 5 14 12 11 15 1
  96. * 13 6 4 9 8 15 3 0 11 1 2 12 5 10 14 7
  97. * 1 10 13 0 6 9 8 7 4 15 14 3 11 5 2 12
  98. *
  99. * S[4]
  100. * 7 13 14 3 0 6 9 10 1 2 8 5 11 12 4 15
  101. * 13 8 11 5 6 15 0 3 4 7 2 12 1 10 14 9
  102. * 10 6 9 0 12 11 7 13 15 1 3 14 5 2 8 4
  103. * 3 15 0 6 10 1 13 8 9 4 5 11 12 7 2 14
  104. *
  105. * S[5]
  106. * 2 12 4 1 7 10 11 6 8 5 3 15 13 0 14 9
  107. * 14 11 2 12 4 7 13 1 5 0 15 10 3 9 8 6
  108. * 4 2 1 11 10 13 7 8 15 9 12 5 6 3 0 14
  109. * 11 8 12 7 1 14 2 13 6 15 0 9 10 4 5 3
  110. *
  111. * S[6]
  112. * 12 1 10 15 9 2 6 8 0 13 3 4 14 7 5 11
  113. * 10 15 4 2 7 12 9 5 6 1 13 14 0 11 3 8
  114. * 9 14 15 5 2 8 12 3 7 0 4 10 1 13 11 6
  115. * 4 3 2 12 9 5 15 10 11 14 1 7 6 0 8 13
  116. *
  117. * S[7]
  118. * 4 11 2 14 15 0 8 13 3 12 9 7 5 10 6 1
  119. * 13 0 11 7 4 9 1 10 14 3 5 12 2 15 8 6
  120. * 1 4 11 13 12 3 7 14 10 15 6 8 0 5 9 2
  121. * 6 11 13 8 1 4 10 7 9 5 0 15 14 2 3 12
  122. *
  123. * S[8]
  124. * 13 2 8 4 6 15 11 1 10 9 3 14 5 0 12 7
  125. * 1 15 13 8 10 3 7 4 12 5 6 11 0 14 9 2
  126. * 7 11 4 1 9 12 14 2 0 6 10 13 15 3 5 8
  127. * 2 1 14 7 4 10 8 13 15 12 9 0 3 5 6 11
  128. *
  129. * 2-4-4-4、从2-4-4-1处循环执行,直到B[8]被替代完成
  130. * 2-4-4-5、将B[1]到B[8]组合,按下表(P)变换,得到P <B[1]到B[8]组成一个32位的数据>
  131. *
  132. * Permutation P
  133. * 16, 7, 20, 21,
  134. * 29, 12, 28, 17,
  135. * 1, 15, 23, 26,
  136. * 5, 18, 31, 10,
  137. * 2, 8, 24, 14,
  138. * 32, 27, 3, 9,
  139. * 19, 13, 30, 6,
  140. * 22, 11, 4, 25
  141. *
  142. * 2-4-6、异或P和L[I-1]结果放在R[I],即R[I] = P XOR L[I - 1]
  143. * 2-4-7、L[I] = R[I - 1]
  144. * 2-4-8、从2-4-1处开始循环执行,直到K[16]被变换完成
  145. * 2-4-5、组合变换后的R[16]L[16](注意:R作为开始的32位),按下表(IP-1)变换得到最后的结果
  146. * <这里要注意了,是对R[16]L[16],而不是L[16]R[16] 即,需要进行左32位和右32位调换一下位置, 这里下标里的16是指轮数,不是位数>
  147. *
  148. * Final Permutation (IP**-1)
  149. * 40, 8, 48, 16, 56, 24, 64, 32,
  150. * 39, 7, 47, 15, 55, 23, 63, 31,
  151. * 38, 6, 46, 14, 54, 22, 62, 30,
  152. * 37, 5, 45, 13, 53, 21, 61, 29,
  153. * 36, 4, 44, 12, 52, 20, 60, 28,
  154. * 35, 3, 43, 11, 51, 19, 59, 27,
  155. * 34, 2, 42, 10, 50, 18, 58, 26,
  156. * 33, 1, 41, 9, 49, 17, 57, 25
  157. *
  158. * 3DES计算方法如下:
  159. * 3DES(加密) = DES(key1, 加密) DES(key2, 解密) DES(key3, 加密)
  160. * 3DES(解密) = DES(key3, 解密) DES(key2, 加密) DES(key1, 解密)
  161. * 每个KEY为64位,总共可以有192位的KEY, 但一般都只使用128位的key
  162. * 如果只用128位密钥,则key3 = key1
  163. *
  164. *************************************************************************/
  165. #include "3des.h"
  166. #include <stdlib.h>
  167. #include <stdio.h>
  168. #include <memory.h>
  169. #include <string.h>
  170. #include <ctype.h>
  171. const char IP_Table[64] =
  172. {
  173. 58, 50, 42, 34, 26, 18, 10, 2,
  174. 60, 52, 44, 36, 28, 20, 12, 4,
  175. 62, 54, 46, 38, 30, 22, 14, 6,
  176. 64, 56, 48, 40, 32, 24, 16, 8,
  177. 57, 49, 41, 33, 25, 17, 9, 1,
  178. 59, 51, 43, 35, 27, 19, 11, 3,
  179. 61, 53, 45, 37, 29, 21, 13, 5,
  180. 63, 55, 47, 39, 31, 23, 15, 7
  181. };
  182. const char IPR_Table[64] =
  183. {
  184. 40, 8, 48, 16, 56, 24, 64, 32,
  185. 39, 7, 47, 15, 55, 23, 63, 31,
  186. 38, 6, 46, 14, 54, 22, 62, 30,
  187. 37, 5, 45, 13, 53, 21, 61, 29,
  188. 36, 4, 44, 12, 52, 20, 60, 28,
  189. 35, 3, 43, 11, 51, 19, 59, 27,
  190. 34, 2, 42, 10, 50, 18, 58, 26,
  191. 33, 1, 41, 9, 49, 17, 57, 25
  192. };
  193. const char E_Table[48] =
  194. {
  195. 32, 1, 2, 3, 4, 5,
  196. 4, 5, 6, 7, 8, 9,
  197. 8, 9, 10, 11, 12, 13,
  198. 12, 13, 14, 15, 16, 17,
  199. 16, 17, 18, 19, 20, 21,
  200. 20, 21, 22, 23, 24, 25,
  201. 24, 25, 26, 27, 28, 29,
  202. 28, 29, 30, 31, 32, 1
  203. };
  204. const char P_Table[32] =
  205. {
  206. 16, 7, 20, 21,
  207. 29, 12, 28, 17,
  208. 1, 15, 23, 26,
  209. 5, 18, 31, 10,
  210. 2, 8, 24, 14,
  211. 32, 27, 3, 9,
  212. 19, 13, 30, 6,
  213. 22, 11, 4, 25
  214. };
  215. const char PC1_Table[56] =
  216. {
  217. 57, 49, 41, 33, 25, 17, 9,
  218. 1, 58, 50, 42, 34, 26, 18,
  219. 10, 2, 59, 51, 43, 35, 27,
  220. 19, 11, 3, 60, 52, 44, 36,
  221. 63, 55, 47, 39, 31, 23, 15,
  222. 7, 62, 54, 46, 38, 30, 22,
  223. 14, 6, 61, 53, 45, 37, 29,
  224. 21, 13, 5, 28, 20, 12, 4
  225. };
  226. const char PC2_Table[48] =
  227. {
  228. 14, 17, 11, 24, 1, 5,
  229. 3, 28, 15, 6, 21, 10,
  230. 23, 19, 12, 4, 26, 8,
  231. 16, 7, 27, 20, 13, 2,
  232. 41, 52, 31, 37, 47, 55,
  233. 30, 40, 51, 45, 33, 48,
  234. 44, 49, 39, 56, 34, 53,
  235. 46, 42, 50, 36, 29, 32
  236. };
  237. const char LOOP_Table[16] =
  238. {
  239. 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1
  240. };
  241. const char S_Box[8][4][16] =
  242. {
  243. {
  244. {14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7},
  245. { 0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8},
  246. { 4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0},
  247. {15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13}
  248. },
  249. {
  250. {15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10},
  251. { 3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5},
  252. { 0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15},
  253. {13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9}
  254. },
  255. {
  256. {10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8},
  257. {13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1},
  258. {13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7},
  259. { 1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12}
  260. },
  261. {
  262. { 7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15},
  263. {13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9},
  264. {10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4},
  265. { 3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14}
  266. },
  267. {
  268. { 2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9},
  269. {14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6},
  270. { 4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14},
  271. {11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3}
  272. },
  273. {
  274. {12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11},
  275. {10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8},
  276. { 9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6},
  277. { 4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13}
  278. },
  279. {
  280. { 4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1},
  281. {13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6},
  282. { 1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2},
  283. { 6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12}
  284. },
  285. {
  286. {13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7},
  287. { 1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2},
  288. { 7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8},
  289. { 2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11}
  290. }
  291. };
  292. char* ch64="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  293. static void ByteToBit(char *Out, const char *In, int bits);
  294. static void BitToByte(char *Out, const char *In, int bits);
  295. static void RotateL(char *In, int len, int loop);
  296. static void Xor(char *InA, const char *InB, int len);
  297. static void Transform(char *Out, const char *In, const char *Table, int len);
  298. static void S_func(char Out[32], const char In[48]);
  299. static void F_func(char In[32], const char Ki[48]);
  300. static void SetSubKey(PSubKey pSubKey, const char Key[8]);
  301. static void DES(char Out[8], const char In[8], const PSubKey pSubKey, int Type);
  302. /*******************************************************************
  303. 函 数 名 称: ByteToBit
  304. 功 能 描 述: 把BYTE转化为Bit流
  305. 参 数 说 明: Out: 输出的Bit流[in][out]
  306. In: 输入的BYTE流[in]
  307. bits: Bit流的长度[in]
  308. 返回值 说明: void
  309. *******************************************************************/
  310. static void ByteToBit(char *Out, const char *In, int bits)
  311. {
  312. int i;
  313. for (i=0; i<bits; ++i)
  314. Out[i] = (In[i>>3]>>(7 - i&7)) & 1;
  315. }
  316. /*******************************************************************
  317. 函 数 名 称: BitToByte
  318. 功 能 描 述: 把Bit转化为Byte流
  319. 参 数 说 明: Out: 输出的BYTE流[in][out]
  320. In: 输入的Bit流[in]
  321. bits: Bit流的长度[in]
  322. 返回值 说明: void
  323. 作 者: huangjf
  324. 更 新 日 期: 2009.6.17
  325. *******************************************************************/
  326. static void BitToByte(char *Out, const char *In, int bits)
  327. {
  328. int i;
  329. memset(Out, 0, bits>>3);
  330. for (i=0; i<bits; ++i) Out[i>>3] |= In[i]<<(7 - i&7);
  331. }
  332. /*******************************************************************
  333. 函 数 名 称: RotateL
  334. 功 能 描 述: 把BIT流按位向左迭代
  335. 参 数 说 明: In: 输入的Bit流[in][out]
  336. len: Bit流的长度[in]
  337. loop: 向左迭代的长度
  338. 返回值 说明: void
  339. 作 者: huangjf
  340. 更 新 日 期: 2009.6.17
  341. *******************************************************************/
  342. static void RotateL(char *In, int len, int loop)
  343. {
  344. char szTmp[256] = {0};
  345. if (len >= 256) return;
  346. if (loop==0 || loop>=256) return;
  347. memset(szTmp, 0x00, sizeof(szTmp));
  348. memcpy(szTmp, In, loop);
  349. memmove(In, In+loop, len-loop);
  350. memcpy(In+len-loop, szTmp, loop);
  351. }
  352. /*******************************************************************
  353. 函 数 名 称: Xor
  354. 功 能 描 述: 把两个Bit流进行异或
  355. 参 数 说 明: InA: 输入的Bit流[in][out]
  356. InB: 输入的Bit流[in]
  357. loop: Bit流的长度
  358. 返回值 说明: void
  359. 作 者: huangjf
  360. 更 新 日 期: 2009.6.17
  361. *******************************************************************/
  362. static void Xor(char *InA, const char *InB, int len)
  363. {
  364. int i;
  365. for (i=0; i<len; ++i) InA[i] ^= InB[i];
  366. }
  367. /*******************************************************************
  368. 函 数 名 称: Transform
  369. 功 能 描 述: 把两个Bit流按表进行位转化
  370. 参 数 说 明: Out: 输出的Bit流[out]
  371. In: 输入的Bit流[in]
  372. Table: 转化需要的表指针
  373. len: 转化表的长度
  374. 返回值 说明: void
  375. 作 者: huangjf
  376. 更 新 日 期: 2009.6.17
  377. *******************************************************************/
  378. static void Transform(char *Out, const char *In, const char *Table, int len)
  379. {
  380. char szTmp[256] = {0};
  381. int i;
  382. if (!Out || !In || !Table) return;
  383. if (len >= 256) return;
  384. memset(szTmp, 0x00, sizeof(szTmp));
  385. for (i=0; i<len; ++i) szTmp[i] = In[Table[i]-1];
  386. memcpy(Out, szTmp, len);
  387. }
  388. /*******************************************************************
  389. 函 数 名 称: S_func
  390. 功 能 描 述: 实现数据加密S BOX模块
  391. 参 数 说 明: Out: 输出的32Bit[out]
  392. In: 输入的48Bit[in]
  393. 返回值 说明: void
  394. 作 者: huangjf
  395. 更 新 日 期: 2009.6.17
  396. *******************************************************************/
  397. static void S_func(char Out[32], const char In[48])
  398. {
  399. int i,j,k,l;
  400. for (i=0,j=0,k=0; i<8; ++i,In+=6,Out+=4)
  401. {
  402. j = (In[0]<<1) + In[5];
  403. k = (In[1]<<3) + (In[2]<<2) + (In[3]<<1) + In[4];
  404. for ( l=0; l<4; ++l)
  405. Out[l] = (S_Box[i][j][k]>>(3 - l)) & 1;
  406. }
  407. }
  408. /*******************************************************************
  409. 函 数 名 称: F_func
  410. 功 能 描 述: 实现数据加密到输出P
  411. 参 数 说 明: Out: 输出的32Bit[out]
  412. In: 输入的48Bit[in]
  413. 返回值 说明: void
  414. 作 者: huangjf
  415. 更 新 日 期: 2009.6.17
  416. *******************************************************************/
  417. static void F_func(char In[32], const char Ki[48])
  418. {
  419. char MR[48] = {0};
  420. memset(MR, 0x00, sizeof(MR));
  421. Transform(MR, In, E_Table, 48);
  422. Xor(MR, Ki, 48);
  423. S_func(In, MR);
  424. Transform(In, In, P_Table, 32);
  425. }
  426. /*******************************************************************
  427. 函 数 名 称: SetSubKey
  428. 功 能 描 述: 变换(换位)8字节密钥,生成16个子密钥
  429. 参 数 说 明: pSubKey: 转换生成的16个子密钥[out]
  430. Key: 输入的8字节64Bit密钥[in]
  431. 返回值 说明: void
  432. 作 者: huangjf
  433. 更 新 日 期: 2009.6.17
  434. *******************************************************************/
  435. static void SetSubKey(PSubKey pSubKey, const char Key[8])
  436. {
  437. int i;
  438. char K[64] = {0}, *KL=&K[0], *KR=&K[28];
  439. ByteToBit(K, Key, 64);
  440. Transform(K, K, PC1_Table, 56);
  441. for ( i=0; i<16; ++i)
  442. {
  443. RotateL(KL, 28, LOOP_Table[i]);
  444. RotateL(KR, 28, LOOP_Table[i]);
  445. Transform((*pSubKey)[i], K, PC2_Table, 48);
  446. }
  447. }
  448. /*******************************************************************
  449. 函 数 名 称: DES
  450. 功 能 描 述: 处理8字节64位的数据
  451. 参 数 说 明: Out: 输出的8字节[out]
  452. In: 输入的8字节待加密[in]
  453. pSubKey: 转换后的16个48位子密钥
  454. Type: 类型:加密ENCRYPT,解密DECRYPT
  455. 返回值 说明: void
  456. 作 者: huangjf
  457. 更 新 日 期: 2009.6.17
  458. *******************************************************************/
  459. static void DES(char Out[8], const char In[8], const PSubKey pSubKey, int Type)
  460. {
  461. int i;
  462. char M[64] = {0}, *ML=&M[0], *MR=&M[32], szTmp[32] = {0};
  463. ByteToBit(M, In, 64);
  464. Transform(M, M, IP_Table, 64);
  465. if (Type == ENCRYPT)
  466. {
  467. for (i=0; i<16; ++i)
  468. {
  469. memcpy(szTmp, MR, 32);
  470. F_func(MR, (*pSubKey)[i]);
  471. Xor(MR, ML, 32);
  472. memcpy(ML, szTmp, 32);
  473. }
  474. }
  475. else
  476. {
  477. for (i=15; i>=0; --i)
  478. {
  479. memcpy(szTmp, MR, 32);
  480. F_func(MR, (*pSubKey)[i]);
  481. Xor(MR, ML, 32);
  482. memcpy(ML, szTmp, 32);
  483. }
  484. }
  485. RotateL(M, 64, 32);
  486. Transform(M, M, IPR_Table, 64);
  487. BitToByte(Out, M, 64);
  488. }
  489. /************************************************************************************************************************************************/
  490. /************************************************************************************************************************************************/
  491. /*******************************************************************
  492. 函 数 名 称: Run1Des
  493. 功 能 描 述: 执行单DES算法对文本加解密
  494. 参 数 说 明: bType :类型:加密ENCRYPT,解密DECRYPT
  495. bMode :模式:ECB,CBC
  496. In :待加密串指针
  497. in_len :待加密串的长度,同时Out的缓冲区大小应大于或者等于in_len
  498. Key :密钥(可为8位,16位,24位)支持3密钥
  499. key_len :密钥长度,多出24位部分将被自动裁减
  500. Out :待输出串指针
  501. out_len :输出缓存大小
  502. cvecstr :8字节随即字符串
  503. 作 者: huangjf
  504. 更 新 日 期: 2009.6.17
  505. 返回值 说明: int :是否加密成功,1:成功,0:失败
  506. *******************************************************************/
  507. int Run1Des(int bType, int bMode, const char *In, unsigned int in_len, const char *Key, unsigned int key_len, char* Out, unsigned int out_len, const char cvecstr[8])
  508. {
  509. int i,j,k;
  510. char m_SubKey[16][48] = {0};
  511. /*参数不合法*/
  512. if (!In || !Key || !Out) return 0;
  513. /*密钥长度,单DES只支持8字节(64位的密钥,每个第8位作为奇偶校验位),多于8字节的自动裁剪*/
  514. if (key_len & 0x00000007) return 0;
  515. /*被加密数据长度,必需为8字节的倍数,如果非8的倍数,调用RunPad()函数补位*/
  516. if (in_len & 0x00000007) return 0;
  517. /*输出缓存大小判断*/
  518. if (out_len < in_len) return 0;
  519. /*生成16个子密钥*/
  520. memset(m_SubKey, 0x00, sizeof(m_SubKey));
  521. SetSubKey(&m_SubKey, Key);
  522. if (bMode == ECB)
  523. {
  524. /*每8字节加密*/
  525. for (i=0,j=in_len>>3; i<j; ++i,Out+=8,In+=8)
  526. {
  527. DES(Out, In, &m_SubKey, bType);
  528. }
  529. }
  530. else if (bMode == CBC)
  531. {
  532. if (cvecstr == NULL) return 0;
  533. char cvec[8] = {0};
  534. char cvin[8] = {0};
  535. memcpy(cvec, cvecstr, 8);
  536. for (i=0,j=in_len>>3; i<j; ++i,Out+=8,In+=8)
  537. {
  538. if (bType == ENCRYPT)
  539. {
  540. for ( k=0; k<8; ++k)
  541. {
  542. cvin[k] = In[k] ^ cvec[k];
  543. }
  544. }
  545. else
  546. {
  547. memcpy(cvin, In, 8);
  548. }
  549. DES(Out, cvin, &m_SubKey, bType);
  550. if (bType == ENCRYPT)
  551. {
  552. memcpy(cvec, Out, 8);
  553. }
  554. else
  555. {
  556. for (k=0; k<8; ++k)
  557. {
  558. Out[k] = Out[k] ^ cvec[k];
  559. }
  560. memcpy(cvec, cvin, 8);
  561. }
  562. }
  563. }
  564. else
  565. {
  566. return 0;
  567. }
  568. return 1;
  569. }
  570. /*******************************************************************
  571. 函 数 名 称: Run3Des
  572. 功 能 描 述: 执行3DES算法对文本加解密
  573. 参 数 说 明: bType :类型:加密ENCRYPT,解密DECRYPT
  574. bMode :模式:ECB,CBC
  575. In :待加密串指针
  576. in_len :待加密串的长度,同时Out的缓冲区大小应大于或者等于in_len
  577. Key :密钥(可为8位,16位,24位)支持3密钥
  578. key_len :密钥长度,多出24位部分将被自动裁减
  579. Out :待输出串指针
  580. out_len :输出缓存大小
  581. cvecstr :8字节随即字符串
  582. 返回值 说明: int :是否加密成功,1:成功,0:失败
  583. 作 者: huangjf
  584. 更 新 日 期: 2009.6.17
  585. 3DES(加密) = DES(key1, 加密) DES(key2, 解密) DES(key3, 加密)
  586. 3DES(解密) = DES(key3, 解密) DES(key2, 加密) DES(key1, 解密)
  587. 每个KEY为64位,总共可以有192位的KEY, 但一般都只使用128位的key
  588. 如果只用128位密钥,则key3 = key1
  589. *******************************************************************/
  590. int Run3Des(int bType, int bMode, const char *In, unsigned int in_len, const char *Key, unsigned int key_len, char* Out, unsigned int out_len, const char cvecstr[8])
  591. {
  592. int i,j,k;
  593. char m_SubKey[3][16][48] = {0};
  594. unsigned char nKey;
  595. /*参数不合法*/
  596. if (!In || !Key || !Out) return 0;
  597. /*被加密数据长度,必需为8字节的倍数,如果非8的倍数,调用RunPad()函数补位*/
  598. if (in_len & 0x00000007) return 0;
  599. /*密钥长度,3DES只支持8、16、24字节(192位的密钥,实际使用128位,每个第8位作为奇偶校验位),多于24字节的自动裁剪*/
  600. if (key_len & 0x00000007) return 0;
  601. /*输出缓存大小判断*/
  602. if (out_len < in_len) return 0;
  603. /*生成16个子密钥*/
  604. nKey = (key_len>>3)>3 ? 3 : (key_len>>3);
  605. memset(m_SubKey, 0x00, sizeof(m_SubKey));
  606. for ( i=0; i<nKey; i++)
  607. {
  608. SetSubKey(&m_SubKey[i], &Key[i<<3]);
  609. }
  610. if (bMode == ECB)
  611. {
  612. if (nKey == 1)
  613. {
  614. /*每8字节加密*/
  615. for (i=0,j=in_len>>3; i<j; ++i,Out+=8,In+=8)
  616. {
  617. DES(Out, In, &m_SubKey[0], bType);
  618. }
  619. }
  620. else if (nKey == 2)
  621. {
  622. for (i=0,j=in_len>>3; i<j; ++i,Out+=8,In+=8)
  623. {
  624. DES(Out, In, &m_SubKey[0], bType);
  625. DES(Out, Out, &m_SubKey[1], bType==ENCRYPT?DECRYPT:ENCRYPT);
  626. DES(Out, Out, &m_SubKey[0], bType);
  627. }
  628. }
  629. else if (nKey == 3)
  630. {
  631. for (i=0,j=in_len>>3; i<j; ++i,Out+=8,In+=8)
  632. {
  633. DES(Out, In, &m_SubKey[bType?2:0], bType);
  634. DES(Out, Out, &m_SubKey[1], bType==ENCRYPT?DECRYPT:ENCRYPT);
  635. DES(Out, Out, &m_SubKey[bType?0:2], bType);
  636. }
  637. }
  638. else
  639. {
  640. /*密钥长度不对*/
  641. return 0;
  642. }
  643. }
  644. else if (bMode == CBC)
  645. {
  646. if (cvecstr == NULL) return 0;
  647. char cvec[8] = {0};
  648. char cvin[8] = {0};
  649. memcpy(cvec, cvecstr, 8);
  650. if (nKey == 1)
  651. {
  652. for (i=0,j=in_len>>3; i<j; ++i,Out+=8,In+=8)
  653. {
  654. if (bType == ENCRYPT)
  655. {
  656. for (k=0; k<8; ++k)
  657. {
  658. cvin[k] = In[k] ^ cvec[k];
  659. }
  660. }
  661. else
  662. {
  663. memcpy(cvin, In, 8);
  664. }
  665. DES(Out, cvin, &m_SubKey[0], bType);
  666. if (bType == ENCRYPT)
  667. {
  668. memcpy(cvec, Out, 8);
  669. }
  670. else
  671. {
  672. for (k=0; k<8; ++k)
  673. {
  674. Out[k] = Out[k] ^ cvec[k];
  675. }
  676. memcpy(cvec, cvin, 8);
  677. }
  678. }
  679. }
  680. else if (nKey == 2)
  681. {
  682. for (i=0,j=in_len>>3; i<j; ++i,Out+=8,In+=8)
  683. {
  684. if (bType == ENCRYPT)
  685. {
  686. for ( k=0; k<8; ++k)
  687. {
  688. cvin[k] = In[k] ^ cvec[k];
  689. }
  690. }
  691. else
  692. {
  693. memcpy(cvin, In, 8);
  694. }
  695. DES(Out, cvin, &m_SubKey[0], bType);
  696. DES(Out, Out, &m_SubKey[1], bType==ENCRYPT?DECRYPT:ENCRYPT);
  697. DES(Out, Out, &m_SubKey[0], bType);
  698. if (bType == ENCRYPT)
  699. {
  700. memcpy(cvec, Out, 8);
  701. }
  702. else
  703. {
  704. for (k=0; k<8; ++k)
  705. {
  706. Out[k] = Out[k] ^ cvec[k];
  707. }
  708. memcpy(cvec, cvin, 8);
  709. }
  710. }
  711. }
  712. else if (nKey == 3)
  713. {
  714. for (i=0,j=in_len>>3; i<j; ++i,Out+=8,In+=8)
  715. {
  716. if (bType == ENCRYPT)
  717. {
  718. for (k=0; k<8; ++k)
  719. {
  720. cvin[k] = In[k] ^ cvec[k];
  721. }
  722. }
  723. else
  724. {
  725. memcpy(cvin, In, 8);
  726. }
  727. DES(Out, cvin, &m_SubKey[bType?2:0], bType);
  728. DES(Out, Out, &m_SubKey[1], bType==ENCRYPT?DECRYPT:ENCRYPT);
  729. DES(Out, Out, &m_SubKey[bType?0:2], bType);
  730. if (bType == ENCRYPT)
  731. {
  732. memcpy(cvec, Out, 8);
  733. }
  734. else
  735. {
  736. for (k=0; k<8; ++k)
  737. {
  738. Out[k] = Out[k] ^ cvec[k];
  739. }
  740. memcpy(cvec, cvin, 8);
  741. }
  742. }
  743. }
  744. else
  745. {
  746. /*密钥长度不对*/
  747. return 0;
  748. }
  749. }
  750. else
  751. {
  752. return 0;
  753. }
  754. return 1;
  755. }
  756. /*******************************************************************
  757. 函 数 名 称: RunPad
  758. 功 能 描 述: 根据协议对加密前的数据进行填充
  759. 参 数 说 明: bType :类型:PAD类型
  760. In :数据串指针
  761. Out :填充输出串指针
  762. in_len :数据的长度
  763. padlen :(in,out)输出buffer的长度,填充后的长度
  764. 返回值 说明: char :是否填充成功
  765. 作 者: huangjf
  766. 修 改 历 史:
  767. 更 新 日 期: 2009.6.17
  768. *******************************************************************/
  769. int RunPad(int nType,const char* In,unsigned in_len,char* Out,int* padlen)
  770. {
  771. int res = (in_len & 0x00000007);
  772. /*
  773. if(*padlen< ((int)in_len+8-res))
  774. {
  775. return 0;
  776. }
  777. else
  778. {
  779. *padlen = ((int)in_len+8-res);
  780. memcpy(Out,In,in_len);
  781. }
  782. */
  783. *padlen = ((int)in_len+8-res);
  784. memcpy(Out,In,in_len);
  785. if (nType == PAD_ISO_1)
  786. {
  787. memset(Out+in_len,0x00,8-res);
  788. }
  789. else if (nType == PAD_ISO_2)
  790. {
  791. memset(Out+in_len,0x80,1);
  792. memset(Out+in_len,0x00,7-res);
  793. }
  794. else if (nType == PAD_PKCS_7)
  795. {
  796. memset(Out+in_len,8-res,8-res);
  797. }
  798. else
  799. {
  800. return 0;
  801. }
  802. return 1;
  803. }
  804. /*解密后,将填充的字符去掉*/
  805. void RunRsm(char *Text)
  806. {
  807. int len,tmpint;
  808. len=strlen(Text);
  809. tmpint=*(Text+len-1);
  810. *(Text+len-tmpint)=0x00;
  811. }
  812. /*将48位的明文密钥转换成24的字符串密钥*/
  813. int CovertKey(char *iKey, char *oKey)
  814. {
  815. char in[64],out[64];
  816. int inlen,i,j;
  817. unsigned char p,q,t,m,n;
  818. memset(in,0,sizeof(in));
  819. memset(out,0,sizeof(out));
  820. inlen=strlen(iKey);
  821. /*检查长度合法性*/
  822. if (inlen!=48)
  823. return 0;
  824. strcpy(in,iKey);
  825. /*检查字符合法性*/
  826. for (i=0; i<inlen; i++)
  827. {
  828. if (!isxdigit(in[i]))
  829. return 0;
  830. }
  831. for (i=0,j=0; i<inlen; i+=2,j++)
  832. {
  833. p=toupper(in[i]);
  834. q=toupper(in[i+1]);
  835. if (isdigit(p))
  836. m=p-48;
  837. else
  838. m=p-55;
  839. if (isdigit(q))
  840. n=q-48;
  841. else
  842. n=q-55;
  843. p=(char)((m<<4)&0xf0);
  844. q=n&0x0f;
  845. t=p|q;
  846. out[j]=t;
  847. }
  848. memcpy(oKey,out,j+1);
  849. return 1;
  850. }
  851. char *Base64Encode(char *src,int srclen)
  852. {
  853. int n,buflen,i,j;
  854. int pading=0;
  855. char *buf;
  856. static char *dst;
  857. buf=src;
  858. buflen=n=srclen;
  859. if (n%3!=0) /* pad with 0x00 by using a temp buffer */
  860. {
  861. pading=1;
  862. buflen=n+3-n%3;
  863. buf=(char*)malloc(buflen+1);
  864. memset(buf,0,buflen+1);
  865. memcpy(buf,src,n);
  866. for (i=0; i<3-n%3; i++)
  867. buf[n+i]=0x00;
  868. }
  869. dst=(char*)malloc(buflen*4/3+1);
  870. memset(dst,0,buflen*4/3+1);
  871. for (i=0,j=0; i<buflen; i+=3,j+=4)
  872. {
  873. dst[j]=(buf[i]&0xFC)>>2;
  874. dst[j+1]=((buf[i]&0x03)<<4) + ((buf[i+1]&0xF0)>>4);
  875. dst[j+2]=((buf[i+1]&0x0F)<<2) + ((buf[i+2]&0xC0)>>6);
  876. dst[j+3]=buf[i+2]&0x3F;
  877. }
  878. for (i=0; i<buflen*4/3; i++) /* map 6 bit value to base64 ASCII character */
  879. {
  880. dst[i]=ch64[dst[i]];
  881. }
  882. for (i=0; i<3-n%3; i++)/*补'='*/
  883. dst[j-i-1]='=';
  884. if (pading)
  885. free(buf);
  886. return dst;
  887. }
  888. char *Base64Decode(char *src)
  889. {
  890. int m,n,i,j,len;
  891. char *p;
  892. static char *dst;
  893. char strbuf[256];
  894. if (src == 0 || src[0] == 0)
  895. return NULL;
  896. len = strlen(src);
  897. if (len % 4)
  898. return NULL;
  899. for (i = 0; i < len-2; i++)
  900. if (src[i] == '=')
  901. return NULL;
  902. memset(strbuf,0,sizeof(strbuf));
  903. strcpy(strbuf,src);
  904. n=strlen(src);
  905. for (i=0; i<n; i++) /* map base64 ASCII character to 6 bit value */
  906. {
  907. p=strchr(ch64,src[i]);
  908. if (!p)
  909. break;
  910. src[i]=p-ch64;
  911. }
  912. dst=(char*)malloc(n*3/4+1);
  913. memset(dst,0,n*3/4+1);
  914. for (i=0,j=0; i<n; i+=4,j+=3)
  915. {
  916. dst[j]=(src[i]<<2) + ((src[i+1]&0x30)>>4);
  917. dst[j+1]=((src[i+1]&0x0F)<<4) + ((src[i+2]&0x3C)>>2);
  918. dst[j+2]=((src[i+2]&0x03)<<6) + src[i+3];
  919. }
  920. m=strcspn(strbuf,"=");
  921. for (i=0; i<n-m; i++)
  922. dst[j-i-1]=0x00;
  923. return dst;
  924. }
  925. void MyDesInit(int* cryptmode,int* padmode,char* cvec)
  926. {
  927. char buf[10];
  928. memset(buf,0,sizeof(buf));
  929. *cryptmode=CBC;
  930. *padmode=PAD_PKCS_7;
  931. buf[0]=0x01;
  932. buf[1]=0x02;
  933. buf[2]=0x03;
  934. buf[3]=0x04;
  935. buf[4]=0x05;
  936. buf[5]=0x06;
  937. buf[6]=0x07;
  938. buf[7]=0x08;
  939. memcpy(cvec,buf,8);
  940. }
  941. /*******************************************************************
  942. 函 数 名 称: Crypt3Des
  943. 功 能 描 述: 实现3DES的加解密
  944. 参 数 说 明: type :类型:加密ENCRYPT,解密DECRYPT
  945. in :待加密串指针或者待解密的密码字符串指针
  946. Out :加密后的密码或者解密后的明文
  947. Key :密钥(48位的ASCII字符串)
  948. 返回值 说明:
  949. 0:成功
  950. -1:非法的密钥长度
  951. -2:密钥含有非16进制字符
  952. -3:明文填充失败
  953. -4:加解密失败
  954. -5:非法的操作类型
  955. *******************************************************************/
  956. int Crypt3Des(int type,char* in,char* key,char* out)
  957. {
  958. int cryptmode,padmode,inlen,outlength,keylen,i;
  959. char cvec[10],instr[256],keystr[256],outstr[256],tmpstr[256];
  960. char* p,*q;
  961. memset(cvec,0,sizeof(cvec));
  962. memset(instr,0,sizeof(instr));
  963. memset(outstr,0,sizeof(outstr));
  964. memset(keystr,0,sizeof(keystr));
  965. memset(tmpstr,0,sizeof(tmpstr));
  966. MyDesInit(&cryptmode,&padmode,cvec);/*设置环境变量*/
  967. inlen=strlen(in);
  968. if (in == 0 || in[0] == 0)
  969. return -7;
  970. inlen = strlen(in);
  971. if (inlen % 8)
  972. return -8;
  973. if (key == 0) return -9;
  974. keylen=strlen(key);
  975. if (keylen!=48)
  976. return -1;
  977. for (i = 0; i < keylen; i++)
  978. {
  979. if ((key[i] >= '0' && key[i] <= '9') || (key[i] >= 'a' && key[i] <= 'f') || (key[i] >= 'A' && key[i] <= 'F'))
  980. continue;
  981. else
  982. return -10;
  983. }
  984. if (inlen>256)
  985. return 0;
  986. if (!CovertKey(key,keystr))
  987. return -2;
  988. keylen=strlen(keystr);
  989. if (type==ENCRYPT)
  990. {
  991. if (!RunPad(padmode,in,strlen(in),instr,&inlen))
  992. return -3;
  993. if (!Run3Des(0,cryptmode,instr,inlen,keystr,(unsigned char)keylen,outstr,sizeof(outstr),cvec))
  994. return -4;
  995. outlength=strlen(outstr);
  996. p=Base64Encode(outstr,outlength);
  997. if (p==NULL)
  998. return -6;
  999. strcpy(out,p);
  1000. free(p);
  1001. return 1;
  1002. }
  1003. else if (type==DECRYPT)
  1004. {
  1005. strcpy(tmpstr,in);
  1006. q=Base64Decode(tmpstr);
  1007. if (q==NULL)
  1008. return -6;
  1009. strcpy(instr,q);
  1010. inlen=strlen(q);
  1011. free(q);
  1012. if (!Run3Des(1,cryptmode,instr,inlen,keystr,(unsigned char)keylen,outstr,sizeof(outstr),cvec))
  1013. return -4;
  1014. RunRsm(outstr);
  1015. strcpy(out,outstr);
  1016. }
  1017. else
  1018. return -5;
  1019. return 1;
  1020. }
  1021. unsigned char GetByte(char *s)
  1022. {
  1023. int v1;
  1024. int v2;
  1025. if (s[0] >= '0' && s[0] <= '9')
  1026. v1 = s[0] - '0';
  1027. else if (s[0] >= 'a' && s[0] <= 'f')
  1028. v1 = s[0] - 'a' + 10;
  1029. else
  1030. v1 = s[0] - 'A' + 10;
  1031. if (s[1] >= '0' && s[1] <= '9')
  1032. v2 = s[1] - '0';
  1033. else if (s[1] >= 'a' && s[1] <= 'f')
  1034. v2 = s[1] - 'a' + 10;
  1035. else
  1036. v2 = s[1] - 'A' + 10;
  1037. return (v1*16+v2);
  1038. }
  1039. /************************************************************************
  1040. //3DES
  1041. char FVin[16] = {0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x38};
  1042. char FVout[16] = {0};
  1043. char FCvec[8] = {0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37};
  1044. unsigned char * p;
  1045. size_t iLen;
  1046. //ECB加密
  1047. if (Run3Des(ENCRYPT, ECB, FVin, 16, Km[0], KEY_LEN_16, FVout, sizeof(FVout), NULL) == 1) {
  1048. RunRsm(FVout);
  1049. }
  1050. //DES
  1051. if (Run1Des(ENCRYPT, ECB, FVin, 16, Km[0], KEY_LEN_8, FVout, sizeof(FVout), NULL) == 1) {
  1052. RunRsm(FVout);
  1053. }
  1054. ************************************************************************/