des.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924
  1. #include "stdafx.h"
  2. #include "des.h"
  3. #include <string.h>
  4. #include "..\..\common\Base64\Base64.h"
  5. #include "..\..\common\EncodingConversion\EncodingConversion.h"
  6. static const unsigned char deskey[9] = "mygz/ndu";//lyfz.net的偶校验结果;
  7. //static const unsigned char desiv[9] = "WorkbyIT";
  8. // 32-bit integer manipulation macros (big endian)
  9. #ifndef GET_ULONG_BE
  10. #define GET_ULONG_BE(n,b,i) \
  11. { \
  12. (n) = ( (unsigned long) (b)[(i)] << 24 ) \
  13. | ( (unsigned long) (b)[(i) + 1] << 16 ) \
  14. | ( (unsigned long) (b)[(i) + 2] << 8 ) \
  15. | ( (unsigned long) (b)[(i) + 3] ); \
  16. }
  17. #endif
  18. #ifndef PUT_ULONG_BE
  19. #define PUT_ULONG_BE(n,b,i) \
  20. { \
  21. (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
  22. (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
  23. (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
  24. (b)[(i) + 3] = (unsigned char) ( (n) ); \
  25. }
  26. #endif
  27. //Expanded DES S-boxes
  28. static const unsigned long SB1[64] =
  29. {
  30. 0x01010400, 0x00000000, 0x00010000, 0x01010404,
  31. 0x01010004, 0x00010404, 0x00000004, 0x00010000,
  32. 0x00000400, 0x01010400, 0x01010404, 0x00000400,
  33. 0x01000404, 0x01010004, 0x01000000, 0x00000004,
  34. 0x00000404, 0x01000400, 0x01000400, 0x00010400,
  35. 0x00010400, 0x01010000, 0x01010000, 0x01000404,
  36. 0x00010004, 0x01000004, 0x01000004, 0x00010004,
  37. 0x00000000, 0x00000404, 0x00010404, 0x01000000,
  38. 0x00010000, 0x01010404, 0x00000004, 0x01010000,
  39. 0x01010400, 0x01000000, 0x01000000, 0x00000400,
  40. 0x01010004, 0x00010000, 0x00010400, 0x01000004,
  41. 0x00000400, 0x00000004, 0x01000404, 0x00010404,
  42. 0x01010404, 0x00010004, 0x01010000, 0x01000404,
  43. 0x01000004, 0x00000404, 0x00010404, 0x01010400,
  44. 0x00000404, 0x01000400, 0x01000400, 0x00000000,
  45. 0x00010004, 0x00010400, 0x00000000, 0x01010004
  46. };
  47. static const unsigned long SB2[64] =
  48. {
  49. 0x80108020, 0x80008000, 0x00008000, 0x00108020,
  50. 0x00100000, 0x00000020, 0x80100020, 0x80008020,
  51. 0x80000020, 0x80108020, 0x80108000, 0x80000000,
  52. 0x80008000, 0x00100000, 0x00000020, 0x80100020,
  53. 0x00108000, 0x00100020, 0x80008020, 0x00000000,
  54. 0x80000000, 0x00008000, 0x00108020, 0x80100000,
  55. 0x00100020, 0x80000020, 0x00000000, 0x00108000,
  56. 0x00008020, 0x80108000, 0x80100000, 0x00008020,
  57. 0x00000000, 0x00108020, 0x80100020, 0x00100000,
  58. 0x80008020, 0x80100000, 0x80108000, 0x00008000,
  59. 0x80100000, 0x80008000, 0x00000020, 0x80108020,
  60. 0x00108020, 0x00000020, 0x00008000, 0x80000000,
  61. 0x00008020, 0x80108000, 0x00100000, 0x80000020,
  62. 0x00100020, 0x80008020, 0x80000020, 0x00100020,
  63. 0x00108000, 0x00000000, 0x80008000, 0x00008020,
  64. 0x80000000, 0x80100020, 0x80108020, 0x00108000
  65. };
  66. static const unsigned long SB3[64] =
  67. {
  68. 0x00000208, 0x08020200, 0x00000000, 0x08020008,
  69. 0x08000200, 0x00000000, 0x00020208, 0x08000200,
  70. 0x00020008, 0x08000008, 0x08000008, 0x00020000,
  71. 0x08020208, 0x00020008, 0x08020000, 0x00000208,
  72. 0x08000000, 0x00000008, 0x08020200, 0x00000200,
  73. 0x00020200, 0x08020000, 0x08020008, 0x00020208,
  74. 0x08000208, 0x00020200, 0x00020000, 0x08000208,
  75. 0x00000008, 0x08020208, 0x00000200, 0x08000000,
  76. 0x08020200, 0x08000000, 0x00020008, 0x00000208,
  77. 0x00020000, 0x08020200, 0x08000200, 0x00000000,
  78. 0x00000200, 0x00020008, 0x08020208, 0x08000200,
  79. 0x08000008, 0x00000200, 0x00000000, 0x08020008,
  80. 0x08000208, 0x00020000, 0x08000000, 0x08020208,
  81. 0x00000008, 0x00020208, 0x00020200, 0x08000008,
  82. 0x08020000, 0x08000208, 0x00000208, 0x08020000,
  83. 0x00020208, 0x00000008, 0x08020008, 0x00020200
  84. };
  85. static const unsigned long SB4[64] =
  86. {
  87. 0x00802001, 0x00002081, 0x00002081, 0x00000080,
  88. 0x00802080, 0x00800081, 0x00800001, 0x00002001,
  89. 0x00000000, 0x00802000, 0x00802000, 0x00802081,
  90. 0x00000081, 0x00000000, 0x00800080, 0x00800001,
  91. 0x00000001, 0x00002000, 0x00800000, 0x00802001,
  92. 0x00000080, 0x00800000, 0x00002001, 0x00002080,
  93. 0x00800081, 0x00000001, 0x00002080, 0x00800080,
  94. 0x00002000, 0x00802080, 0x00802081, 0x00000081,
  95. 0x00800080, 0x00800001, 0x00802000, 0x00802081,
  96. 0x00000081, 0x00000000, 0x00000000, 0x00802000,
  97. 0x00002080, 0x00800080, 0x00800081, 0x00000001,
  98. 0x00802001, 0x00002081, 0x00002081, 0x00000080,
  99. 0x00802081, 0x00000081, 0x00000001, 0x00002000,
  100. 0x00800001, 0x00002001, 0x00802080, 0x00800081,
  101. 0x00002001, 0x00002080, 0x00800000, 0x00802001,
  102. 0x00000080, 0x00800000, 0x00002000, 0x00802080
  103. };
  104. static const unsigned long SB5[64] =
  105. {
  106. 0x00000100, 0x02080100, 0x02080000, 0x42000100,
  107. 0x00080000, 0x00000100, 0x40000000, 0x02080000,
  108. 0x40080100, 0x00080000, 0x02000100, 0x40080100,
  109. 0x42000100, 0x42080000, 0x00080100, 0x40000000,
  110. 0x02000000, 0x40080000, 0x40080000, 0x00000000,
  111. 0x40000100, 0x42080100, 0x42080100, 0x02000100,
  112. 0x42080000, 0x40000100, 0x00000000, 0x42000000,
  113. 0x02080100, 0x02000000, 0x42000000, 0x00080100,
  114. 0x00080000, 0x42000100, 0x00000100, 0x02000000,
  115. 0x40000000, 0x02080000, 0x42000100, 0x40080100,
  116. 0x02000100, 0x40000000, 0x42080000, 0x02080100,
  117. 0x40080100, 0x00000100, 0x02000000, 0x42080000,
  118. 0x42080100, 0x00080100, 0x42000000, 0x42080100,
  119. 0x02080000, 0x00000000, 0x40080000, 0x42000000,
  120. 0x00080100, 0x02000100, 0x40000100, 0x00080000,
  121. 0x00000000, 0x40080000, 0x02080100, 0x40000100
  122. };
  123. static const unsigned long SB6[64] =
  124. {
  125. 0x20000010, 0x20400000, 0x00004000, 0x20404010,
  126. 0x20400000, 0x00000010, 0x20404010, 0x00400000,
  127. 0x20004000, 0x00404010, 0x00400000, 0x20000010,
  128. 0x00400010, 0x20004000, 0x20000000, 0x00004010,
  129. 0x00000000, 0x00400010, 0x20004010, 0x00004000,
  130. 0x00404000, 0x20004010, 0x00000010, 0x20400010,
  131. 0x20400010, 0x00000000, 0x00404010, 0x20404000,
  132. 0x00004010, 0x00404000, 0x20404000, 0x20000000,
  133. 0x20004000, 0x00000010, 0x20400010, 0x00404000,
  134. 0x20404010, 0x00400000, 0x00004010, 0x20000010,
  135. 0x00400000, 0x20004000, 0x20000000, 0x00004010,
  136. 0x20000010, 0x20404010, 0x00404000, 0x20400000,
  137. 0x00404010, 0x20404000, 0x00000000, 0x20400010,
  138. 0x00000010, 0x00004000, 0x20400000, 0x00404010,
  139. 0x00004000, 0x00400010, 0x20004010, 0x00000000,
  140. 0x20404000, 0x20000000, 0x00400010, 0x20004010
  141. };
  142. static const unsigned long SB7[64] =
  143. {
  144. 0x00200000, 0x04200002, 0x04000802, 0x00000000,
  145. 0x00000800, 0x04000802, 0x00200802, 0x04200800,
  146. 0x04200802, 0x00200000, 0x00000000, 0x04000002,
  147. 0x00000002, 0x04000000, 0x04200002, 0x00000802,
  148. 0x04000800, 0x00200802, 0x00200002, 0x04000800,
  149. 0x04000002, 0x04200000, 0x04200800, 0x00200002,
  150. 0x04200000, 0x00000800, 0x00000802, 0x04200802,
  151. 0x00200800, 0x00000002, 0x04000000, 0x00200800,
  152. 0x04000000, 0x00200800, 0x00200000, 0x04000802,
  153. 0x04000802, 0x04200002, 0x04200002, 0x00000002,
  154. 0x00200002, 0x04000000, 0x04000800, 0x00200000,
  155. 0x04200800, 0x00000802, 0x00200802, 0x04200800,
  156. 0x00000802, 0x04000002, 0x04200802, 0x04200000,
  157. 0x00200800, 0x00000000, 0x00000002, 0x04200802,
  158. 0x00000000, 0x00200802, 0x04200000, 0x00000800,
  159. 0x04000002, 0x04000800, 0x00000800, 0x00200002
  160. };
  161. static const unsigned long SB8[64] =
  162. {
  163. 0x10001040, 0x00001000, 0x00040000, 0x10041040,
  164. 0x10000000, 0x10001040, 0x00000040, 0x10000000,
  165. 0x00040040, 0x10040000, 0x10041040, 0x00041000,
  166. 0x10041000, 0x00041040, 0x00001000, 0x00000040,
  167. 0x10040000, 0x10000040, 0x10001000, 0x00001040,
  168. 0x00041000, 0x00040040, 0x10040040, 0x10041000,
  169. 0x00001040, 0x00000000, 0x00000000, 0x10040040,
  170. 0x10000040, 0x10001000, 0x00041040, 0x00040000,
  171. 0x00041040, 0x00040000, 0x10041000, 0x00001000,
  172. 0x00000040, 0x10040040, 0x00001000, 0x00041040,
  173. 0x10001000, 0x00000040, 0x10000040, 0x10040000,
  174. 0x10040040, 0x10000000, 0x00040000, 0x10001040,
  175. 0x00000000, 0x10041040, 0x00040040, 0x10000040,
  176. 0x10040000, 0x10001000, 0x10001040, 0x00000000,
  177. 0x10041040, 0x00041000, 0x00041000, 0x00001040,
  178. 0x00001040, 0x00040040, 0x10000000, 0x10041000
  179. };
  180. //PC1: left and right halves bit-swap
  181. static const unsigned long LHs[16] =
  182. {
  183. 0x00000000, 0x00000001, 0x00000100, 0x00000101,
  184. 0x00010000, 0x00010001, 0x00010100, 0x00010101,
  185. 0x01000000, 0x01000001, 0x01000100, 0x01000101,
  186. 0x01010000, 0x01010001, 0x01010100, 0x01010101
  187. };
  188. static const unsigned long RHs[16] =
  189. {
  190. 0x00000000, 0x01000000, 0x00010000, 0x01010000,
  191. 0x00000100, 0x01000100, 0x00010100, 0x01010100,
  192. 0x00000001, 0x01000001, 0x00010001, 0x01010001,
  193. 0x00000101, 0x01000101, 0x00010101, 0x01010101,
  194. };
  195. static const unsigned char odd_parity_table[128] = { 1, 2, 4, 7, 8,
  196. 11, 13, 14, 16, 19, 21, 22, 25, 26, 28, 31, 32, 35, 37, 38, 41, 42, 44,
  197. 47, 49, 50, 52, 55, 56, 59, 61, 62, 64, 67, 69, 70, 73, 74, 76, 79, 81,
  198. 82, 84, 87, 88, 91, 93, 94, 97, 98, 100, 103, 104, 107, 109, 110, 112,
  199. 115, 117, 118, 121, 122, 124, 127, 128, 131, 133, 134, 137, 138, 140,
  200. 143, 145, 146, 148, 151, 152, 155, 157, 158, 161, 162, 164, 167, 168,
  201. 171, 173, 174, 176, 179, 181, 182, 185, 186, 188, 191, 193, 194, 196,
  202. 199, 200, 203, 205, 206, 208, 211, 213, 214, 217, 218, 220, 223, 224,
  203. 227, 229, 230, 233, 234, 236, 239, 241, 242, 244, 247, 248, 251, 253,
  204. 254
  205. };
  206. #define WEAK_KEY_COUNT 16
  207. static const unsigned char weak_key_table[WEAK_KEY_COUNT][DES_KEY_SIZE] =
  208. {
  209. { 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01 },
  210. { 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE },
  211. { 0x1F, 0x1F, 0x1F, 0x1F, 0x0E, 0x0E, 0x0E, 0x0E },
  212. { 0xE0, 0xE0, 0xE0, 0xE0, 0xF1, 0xF1, 0xF1, 0xF1 },
  213. { 0x01, 0x1F, 0x01, 0x1F, 0x01, 0x0E, 0x01, 0x0E },
  214. { 0x1F, 0x01, 0x1F, 0x01, 0x0E, 0x01, 0x0E, 0x01 },
  215. { 0x01, 0xE0, 0x01, 0xE0, 0x01, 0xF1, 0x01, 0xF1 },
  216. { 0xE0, 0x01, 0xE0, 0x01, 0xF1, 0x01, 0xF1, 0x01 },
  217. { 0x01, 0xFE, 0x01, 0xFE, 0x01, 0xFE, 0x01, 0xFE },
  218. { 0xFE, 0x01, 0xFE, 0x01, 0xFE, 0x01, 0xFE, 0x01 },
  219. { 0x1F, 0xE0, 0x1F, 0xE0, 0x0E, 0xF1, 0x0E, 0xF1 },
  220. { 0xE0, 0x1F, 0xE0, 0x1F, 0xF1, 0x0E, 0xF1, 0x0E },
  221. { 0x1F, 0xFE, 0x1F, 0xFE, 0x0E, 0xFE, 0x0E, 0xFE },
  222. { 0xFE, 0x1F, 0xFE, 0x1F, 0xFE, 0x0E, 0xFE, 0x0E },
  223. { 0xE0, 0xFE, 0xE0, 0xFE, 0xF1, 0xFE, 0xF1, 0xFE },
  224. { 0xFE, 0xE0, 0xFE, 0xE0, 0xFE, 0xF1, 0xFE, 0xF1 }
  225. };
  226. // Initial Permutation macro
  227. #define DES_IP(X,Y) \
  228. { \
  229. T = ((X >> 4) ^ Y) & 0x0F0F0F0F; Y ^= T; X ^= (T << 4); \
  230. T = ((X >> 16) ^ Y) & 0x0000FFFF; Y ^= T; X ^= (T << 16); \
  231. T = ((Y >> 2) ^ X) & 0x33333333; X ^= T; Y ^= (T << 2); \
  232. T = ((Y >> 8) ^ X) & 0x00FF00FF; X ^= T; Y ^= (T << 8); \
  233. Y = ((Y << 1) | (Y >> 31)) & 0xFFFFFFFF; \
  234. T = (X ^ Y) & 0xAAAAAAAA; Y ^= T; X ^= T; \
  235. X = ((X << 1) | (X >> 31)) & 0xFFFFFFFF; \
  236. }
  237. // Final Permutation macro
  238. #define DES_FP(X,Y) \
  239. { \
  240. X = ((X << 31) | (X >> 1)) & 0xFFFFFFFF; \
  241. T = (X ^ Y) & 0xAAAAAAAA; X ^= T; Y ^= T; \
  242. Y = ((Y << 31) | (Y >> 1)) & 0xFFFFFFFF; \
  243. T = ((Y >> 8) ^ X) & 0x00FF00FF; X ^= T; Y ^= (T << 8); \
  244. T = ((Y >> 2) ^ X) & 0x33333333; X ^= T; Y ^= (T << 2); \
  245. T = ((X >> 16) ^ Y) & 0x0000FFFF; Y ^= T; X ^= (T << 16); \
  246. T = ((X >> 4) ^ Y) & 0x0F0F0F0F; Y ^= T; X ^= (T << 4); \
  247. }
  248. // DES round macro
  249. #define DES_ROUND(X,Y) \
  250. { \
  251. T = *SK++ ^ X; \
  252. Y ^= SB8[ (T) & 0x3F ] ^ \
  253. SB6[ (T >> 8) & 0x3F ] ^ \
  254. SB4[ (T >> 16) & 0x3F ] ^ \
  255. SB2[ (T >> 24) & 0x3F ]; \
  256. \
  257. T = *SK++ ^ ((X << 28) | (X >> 4)); \
  258. Y ^= SB7[ (T) & 0x3F ] ^ \
  259. SB5[ (T >> 8) & 0x3F ] ^ \
  260. SB3[ (T >> 16) & 0x3F ] ^ \
  261. SB1[ (T >> 24) & 0x3F ]; \
  262. }
  263. #define SWAP(a,b) { unsigned long t = a; a = b; b = t; t = 0; }
  264. void des_key_set_parity( unsigned char key[DES_KEY_SIZE] )
  265. {
  266. for(int i = 0; i < DES_KEY_SIZE; i++ )
  267. key[i] = odd_parity_table[key[i] / 2];
  268. }
  269. int des_key_check_key_parity( const unsigned char key[DES_KEY_SIZE] )
  270. {
  271. for(int i = 0; i < DES_KEY_SIZE; i++ )
  272. if( key[i] != odd_parity_table[key[i] / 2] )
  273. return( 1 );
  274. return( 0 );
  275. }
  276. int des_key_check_weak( const unsigned char key[DES_KEY_SIZE] )
  277. {
  278. for(int i = 0; i < WEAK_KEY_COUNT; i++ )
  279. if( memcmp( weak_key_table[i], key, DES_KEY_SIZE) == 0 )
  280. return( 1 );
  281. return( 0 );
  282. }
  283. unsigned __int64 des_enc_len(const unsigned __int64& nlen)
  284. {
  285. unsigned __int64 npaddinglen = nlen;
  286. int npad = nlen&7;//nFileLen & 7 <=> nFileLen%8
  287. if ( npad != 0 )
  288. {
  289. npaddinglen += 8 - npad;
  290. }
  291. return npaddinglen;
  292. }
  293. unsigned __int64 des_dec_len(const unsigned char* input, unsigned __int64 nlen)
  294. {
  295. while( input[--nlen] == 0x00 );
  296. return ++nlen;
  297. }
  298. /************************************************************************/
  299. /* 函数:des_setkey[6/5/2016 IT];
  300. /* 描述:;
  301. /* 参数:;
  302. /* [IN] :;
  303. /* [OUT] :;
  304. /* [IN/OUT] :;
  305. /* 返回:void;
  306. /* 注意:;
  307. /* 示例:;
  308. /*
  309. /* 修改:;
  310. /* 日期:;
  311. /* 内容:;
  312. /************************************************************************/
  313. static void des_setkey( unsigned long SK[32], const unsigned char key[8] )
  314. {
  315. int i;
  316. unsigned long X, Y, T;
  317. GET_ULONG_BE( X, key, 0 );
  318. GET_ULONG_BE( Y, key, 4 );
  319. // Permuted Choice 1
  320. T = ((Y >> 4) ^ X) & 0x0F0F0F0F; X ^= T; Y ^= (T << 4);
  321. T = ((Y ) ^ X) & 0x10101010; X ^= T; Y ^= (T );
  322. X = (LHs[ (X ) & 0xF] << 3) | (LHs[ (X >> 8) & 0xF ] << 2)
  323. | (LHs[ (X >> 16) & 0xF] << 1) | (LHs[ (X >> 24) & 0xF ] )
  324. | (LHs[ (X >> 5) & 0xF] << 7) | (LHs[ (X >> 13) & 0xF ] << 6)
  325. | (LHs[ (X >> 21) & 0xF] << 5) | (LHs[ (X >> 29) & 0xF ] << 4);
  326. Y = (RHs[ (Y >> 1) & 0xF] << 3) | (RHs[ (Y >> 9) & 0xF ] << 2)
  327. | (RHs[ (Y >> 17) & 0xF] << 1) | (RHs[ (Y >> 25) & 0xF ] )
  328. | (RHs[ (Y >> 4) & 0xF] << 7) | (RHs[ (Y >> 12) & 0xF ] << 6)
  329. | (RHs[ (Y >> 20) & 0xF] << 5) | (RHs[ (Y >> 28) & 0xF ] << 4);
  330. X &= 0x0FFFFFFF;
  331. Y &= 0x0FFFFFFF;
  332. // calculate subkeys
  333. for( i = 0; i < 16; i++ )
  334. {
  335. if( i < 2 || i == 8 || i == 15 )
  336. {
  337. X = ((X << 1) | (X >> 27)) & 0x0FFFFFFF;
  338. Y = ((Y << 1) | (Y >> 27)) & 0x0FFFFFFF;
  339. }
  340. else
  341. {
  342. X = ((X << 2) | (X >> 26)) & 0x0FFFFFFF;
  343. Y = ((Y << 2) | (Y >> 26)) & 0x0FFFFFFF;
  344. }
  345. *SK++ = ((X << 4) & 0x24000000) | ((X << 28) & 0x10000000)
  346. | ((X << 14) & 0x08000000) | ((X << 18) & 0x02080000)
  347. | ((X << 6) & 0x01000000) | ((X << 9) & 0x00200000)
  348. | ((X >> 1) & 0x00100000) | ((X << 10) & 0x00040000)
  349. | ((X << 2) & 0x00020000) | ((X >> 10) & 0x00010000)
  350. | ((Y >> 13) & 0x00002000) | ((Y >> 4) & 0x00001000)
  351. | ((Y << 6) & 0x00000800) | ((Y >> 1) & 0x00000400)
  352. | ((Y >> 14) & 0x00000200) | ((Y ) & 0x00000100)
  353. | ((Y >> 5) & 0x00000020) | ((Y >> 10) & 0x00000010)
  354. | ((Y >> 3) & 0x00000008) | ((Y >> 18) & 0x00000004)
  355. | ((Y >> 26) & 0x00000002) | ((Y >> 24) & 0x00000001);
  356. *SK++ = ((X << 15) & 0x20000000) | ((X << 17) & 0x10000000)
  357. | ((X << 10) & 0x08000000) | ((X << 22) & 0x04000000)
  358. | ((X >> 2) & 0x02000000) | ((X << 1) & 0x01000000)
  359. | ((X << 16) & 0x00200000) | ((X << 11) & 0x00100000)
  360. | ((X << 3) & 0x00080000) | ((X >> 6) & 0x00040000)
  361. | ((X << 15) & 0x00020000) | ((X >> 4) & 0x00010000)
  362. | ((Y >> 2) & 0x00002000) | ((Y << 8) & 0x00001000)
  363. | ((Y >> 14) & 0x00000808) | ((Y >> 9) & 0x00000400)
  364. | ((Y ) & 0x00000200) | ((Y << 7) & 0x00000100)
  365. | ((Y >> 7) & 0x00000020) | ((Y >> 3) & 0x00000011)
  366. | ((Y << 2) & 0x00000004) | ((Y >> 21) & 0x00000002);
  367. }
  368. }
  369. /************************************************************************/
  370. /* 函数:des_setkey_enc[6/5/2016 IT];
  371. /* 描述:设置DES加密密钥(56bit,加密);
  372. /* 参数:;
  373. /* [IN] ctx:要被初始化的DES结构体;
  374. /* [IN] key:8字节长度的加解密密钥;
  375. /* 返回:void;
  376. /* 注意:;
  377. /* 示例:;
  378. /*
  379. /* 修改:;
  380. /* 日期:;
  381. /* 内容:;
  382. /************************************************************************/
  383. void des_setkey_enc( IN des_context *ctx, IN const unsigned char key[8] )
  384. {
  385. des_setkey( ctx->sk, key );
  386. }
  387. /************************************************************************/
  388. /* 函数:des_setkey_dec[6/5/2016 IT];
  389. /* 描述:设置DES解密密钥(56bit,解密);;
  390. /* 参数:;
  391. /* [IN] ctx:要被初始化的DES结构体;
  392. /* [IN] key:8字节长度的加解密密钥;
  393. /* 返回:void;
  394. /* 注意:;
  395. /* 示例:;
  396. /*
  397. /* 修改:;
  398. /* 日期:;
  399. /* 内容:;
  400. /************************************************************************/
  401. void des_setkey_dec( IN des_context *ctx, IN const unsigned char key[8] )
  402. {
  403. int i;
  404. des_setkey( ctx->sk, key );
  405. for( i = 0; i < 16; i += 2 )
  406. {
  407. SWAP( ctx->sk[i], ctx->sk[30 - i] );
  408. SWAP( ctx->sk[i + 1], ctx->sk[31 - i] );
  409. }
  410. }
  411. /************************************************************************/
  412. /* 函数:[6/5/2016 IT];
  413. /* 描述:DES-ECB模式下加解密;
  414. /* 参数:;
  415. /* [IN] ctx:已经初始化过的DES结构体;
  416. /* [IN] input:64bit的输入缓存块;
  417. /* [OUT] output:64bit的输出缓存块;
  418. /* 返回:成功返回0;
  419. /* 注意:;
  420. /* 示例:;
  421. /*
  422. /* 修改:;
  423. /* 日期:;
  424. /* 内容:;
  425. /************************************************************************/
  426. int des_crypt_ecb( IN des_context *ctx, IN const unsigned char input[8], OUT unsigned char output[8] )
  427. {
  428. int i;
  429. unsigned long X, Y, T, *SK;
  430. SK = ctx->sk;
  431. GET_ULONG_BE( X, input, 0 );
  432. GET_ULONG_BE( Y, input, 4 );
  433. DES_IP( X, Y );
  434. for( i = 0; i < 8; i++ )
  435. {
  436. DES_ROUND( Y, X );
  437. DES_ROUND( X, Y );
  438. }
  439. DES_FP( Y, X );
  440. PUT_ULONG_BE( Y, output, 0 );
  441. PUT_ULONG_BE( X, output, 4 );
  442. return( 0 );
  443. }
  444. /************************************************************************/
  445. /* 函数:des_crypt_cbc[6/5/2016 IT];
  446. /* 描述:DES-CBC模式加解密;
  447. /* 参数:;
  448. /* [IN] ctx:已经初始化过的DES结构体;
  449. /* [IN] mode:加密或解密模式;
  450. /* [IN] length:input长度;
  451. /* [IN] iv:初始向量(使用后更新);
  452. /* [IN] input:要加密或解密的数据;
  453. /* [OUT] output:加密或解密的数据;
  454. /* 返回:成功返回0;
  455. /* 注意:;
  456. /* 示例:;
  457. /*
  458. /* 修改:;
  459. /* 日期:;
  460. /* 内容:;
  461. /************************************************************************/
  462. int des_crypt_cbc( IN des_context *ctx, IN int mode, IN int length, IN unsigned char iv[8], IN const unsigned char *input, OUT unsigned char *output )
  463. {
  464. int i;
  465. unsigned char temp[8];
  466. if( length % 8 )
  467. return( POLARSSL_ERR_DES_INVALID_INPUT_LENGTH );
  468. if( mode == DES_ENCRYPT )
  469. {
  470. while( length > 0 )
  471. {
  472. for( i = 0; i < 8; i++ )
  473. output[i] = (unsigned char)( input[i] ^ iv[i] );
  474. des_crypt_ecb( ctx, output, output );
  475. memcpy( iv, output, 8 );
  476. input += 8;
  477. output += 8;
  478. length -= 8;
  479. }
  480. }
  481. else /* DES_DECRYPT */
  482. {
  483. while( length > 0 )
  484. {
  485. memcpy( temp, input, 8 );
  486. des_crypt_ecb( ctx, input, output );
  487. for( i = 0; i < 8; i++ )
  488. output[i] = (unsigned char)( output[i] ^ iv[i] );
  489. memcpy( iv, temp, 8 );
  490. input += 8;
  491. output += 8;
  492. length -= 8;
  493. }
  494. }
  495. return( 0 );
  496. }
  497. /************************************************************************/
  498. /* 函数:des_crypt_cbc[6/5/2016 IT];
  499. /* 描述:DES-CBC模式加解密;
  500. /* 参数:;
  501. /* [IN] ctx:已经初始化过的DES结构体;
  502. /* [IN] mode:加密或解密模式;
  503. /* [IN] length:input长度;
  504. /* [IN] iv:初始向量(使用后更新);
  505. /* [IN] input:要加密或解密的数据;
  506. /* [OUT] output:加密或解密的数据;
  507. /* 返回:成功返回0;
  508. /* 注意:;
  509. /* 示例:;
  510. /*
  511. /* 修改:;
  512. /* 日期:;
  513. /* 内容:;
  514. /************************************************************************/
  515. int des_crypt_cbc(IN int mode, IN int length, IN const unsigned char *input, OUT unsigned char *output, IN const int& outMaxlen )
  516. {
  517. if ( length == 0 )
  518. return -1;
  519. int i;
  520. des_context ctx;
  521. if ( mode == DES_ENCRYPT )
  522. des_setkey_enc(&ctx, deskey);
  523. else
  524. des_setkey_dec(&ctx, deskey);
  525. int nInputlen = length;
  526. unsigned char *result = output;
  527. unsigned char iv[9] = "WorkbyIT";
  528. unsigned char temp[8];
  529. int npadlen = length & 7;
  530. if( npadlen != 0 )
  531. {
  532. length += 8 - npadlen;
  533. nInputlen = length;
  534. }
  535. if( mode == DES_ENCRYPT )
  536. {
  537. while( length > 0 )
  538. {
  539. for( i = 0; i < 8; i++ )
  540. {
  541. if ( length == 8 && npadlen != 0)
  542. {
  543. if ( i >= npadlen )
  544. output[i] = (unsigned char)( 0x00 ^ iv[i] );
  545. else
  546. output[i] = (unsigned char)( input[i] ^ iv[i] );
  547. }
  548. else
  549. output[i] = (unsigned char)( input[i] ^ iv[i] );
  550. }
  551. des_crypt_ecb( &ctx, output, output );
  552. memcpy( iv, output, 8 );
  553. input += 8;
  554. output += 8;
  555. length -= 8;
  556. }
  557. // 将加密结果转换成base64;
  558. // 1.计算出Base64的长度,CalcBase64Len的参数必须是字节长度,而非字符数;
  559. INT nBaseLen = CBase64::CalcBase64Len(nInputlen);
  560. if ( nBaseLen > outMaxlen )
  561. return -1;
  562. // 2.创建Base64缓存;
  563. CHAR *pBase64 = new CHAR[nBaseLen + 1];
  564. memset(pBase64, 0, nBaseLen + 1);
  565. // 3.转化出Base64字符;
  566. CBase64::binToBase64(result, nInputlen, pBase64);
  567. memset(result, 0, outMaxlen);
  568. memcpy(result, pBase64, nBaseLen);
  569. if ( pBase64 )
  570. delete []pBase64;
  571. }
  572. else /* DES_DECRYPT */
  573. {
  574. // 将base64转换成密文状态;
  575. // 1.计算出字节长度;
  576. INT nByteLen = CBase64::CalcBinLen(length);
  577. // 2.创建Byte缓存区;
  578. BYTE *pBytes = new BYTE[nByteLen];
  579. memset(pBytes, 0, nByteLen);
  580. // 3.转化成字节;
  581. length = CBase64::base64ToBin((char*)input, pBytes, nByteLen);
  582. result = pBytes;
  583. while ( pBytes[length-1] == 0x00 )
  584. --length;
  585. while( length > 0 )
  586. {
  587. memcpy( temp, pBytes, 8 );
  588. des_crypt_ecb( &ctx, pBytes, output );
  589. for( i = 0; i < 8; i++ )
  590. output[i] = (unsigned char)( output[i] ^ iv[i] );
  591. memcpy( iv, temp, 8 );
  592. pBytes += 8;
  593. output += 8;
  594. length -= 8;
  595. }
  596. if ( result )
  597. delete []result;
  598. }
  599. return( 0 );
  600. }
  601. int des_crypt(IN CString strInput, IN CString& strOutput, IN const int &mode /* = DES_ENCRYPT */)
  602. {
  603. if ( strInput.IsEmpty() )
  604. return -1;
  605. unsigned char* pInput = NULL;
  606. int nInputlen = strInput.GetLength();
  607. #ifdef UNICODE
  608. pInput = (unsigned char*)EncodingConverion::UNICODE2ASCII(strInput.GetBuffer());
  609. nInputlen = strlen((char*)pInput);
  610. #else
  611. pInput = new unsigned char[nInputlen+1];
  612. memset(pInput, 0, nInputlen+1);
  613. memcpy(pInput, strInput, nInputlen);
  614. #endif
  615. int nOutputlen = nInputlen;
  616. if ( mode == DES_ENCRYPT )
  617. {
  618. // 计算密文长度,不足8位补齐;
  619. nOutputlen = des_enc_len(nInputlen);
  620. // 计算密文转成base64的长度,用于创建输出缓存;
  621. nOutputlen = CBase64::CalcBase64Len(nOutputlen);
  622. }
  623. else
  624. {
  625. nOutputlen = CBase64::CalcBinLen(nInputlen);
  626. }
  627. unsigned char* pOutput = new unsigned char[nOutputlen+1];
  628. memset(pOutput, 0, nOutputlen + 1);
  629. if ( des_crypt_cbc(mode, nInputlen, pInput, pOutput, nOutputlen) == -1)
  630. return -1;
  631. #ifdef UNICODE
  632. WCHAR* pWide = EncodingConverion::ASCII2UNICODE((char*)pOutput);
  633. strOutput.Format(_T("%s"), pWide);
  634. if ( pWide )
  635. delete []pWide;
  636. #else
  637. strOutput.Format(_T("%s"), pOutput);
  638. #endif
  639. if ( pInput )
  640. delete []pInput;
  641. if ( pOutput )
  642. delete []pOutput;
  643. return 0;
  644. }
  645. /************************************************************************/
  646. /* 函数:[12/21/2016 IT];
  647. /* 描述:;
  648. /* 参数:;
  649. /* [IN] :;
  650. /* [OUT] :;
  651. /* [IN/OUT] :;
  652. /* 返回:void;
  653. /* 注意:;
  654. /* 示例:;
  655. /*
  656. /* 修改:;
  657. /* 日期:;
  658. /* 内容:;
  659. /************************************************************************/
  660. void DES_EncryptFile(IN LPCTSTR lpFile, IN LPBYTE lpKey, IN LPBYTE lpVI, IN LPCTSTR lpEncryFile)
  661. {
  662. if ( lpFile == NULL )
  663. return;
  664. if ( lpKey == NULL || lpVI == NULL )
  665. return;
  666. BYTE szKey[9] = {0};
  667. INT nKeyLen = strlen((char*)lpKey);
  668. if (nKeyLen != 8 )
  669. return;
  670. memcpy(szKey, lpKey, 8);
  671. BYTE szvi[9] = {0};
  672. INT nVILen = strlen((char*)lpVI);
  673. if ( nVILen != 8 )
  674. return;
  675. memcpy(szvi, lpVI, 8);
  676. CFile cf;
  677. BYTE *pFileData = NULL;
  678. BYTE *pOutData = NULL;
  679. INT nPadLen = 0;
  680. UINT64 nFileLen = 0;
  681. UINT64 nOutLen = 0;
  682. if ( cf.Open(lpFile, CFile::modeRead) )
  683. {
  684. nFileLen = cf.GetLength();
  685. pFileData = new BYTE[nFileLen];
  686. memset(pFileData, 0, nFileLen);
  687. cf.Read(pFileData,nFileLen);
  688. cf.Close();
  689. }
  690. else
  691. {
  692. DWORD dwError = GetLastError();
  693. TRACE("%d", dwError);
  694. return;
  695. }
  696. if ( strncmp((char*)pFileData, "<?>", 3) == 0 )
  697. {// 已加密的,退出;
  698. delete []pFileData;
  699. return;
  700. }
  701. // 不足8的倍数,补齐;
  702. nOutLen = des_enc_len(nFileLen);
  703. pOutData = new BYTE[nOutLen + 1 ];
  704. memset(pOutData, 0, nOutLen + 1 );
  705. des_context des;
  706. if ( des_key_check_key_parity(szKey) == 1)
  707. {
  708. des_key_set_parity(szKey);
  709. if ( des_key_check_key_parity(szKey) == 0 )
  710. {
  711. des_setkey_enc(&des, szKey);
  712. }
  713. }
  714. else
  715. des_setkey_enc(&des, szKey);
  716. des_crypt_cbc(&des, DES_ENCRYPT, nOutLen, szvi, pFileData, pOutData);
  717. CFile cfo;
  718. if ( cfo.Open(lpEncryFile, CFile::modeCreate|CFile::modeWrite) )
  719. {
  720. cfo.Write("<?>", 3);
  721. cfo.Write(&nFileLen, sizeof(UINT64));
  722. cfo.Write(pOutData, nOutLen);
  723. cfo.Close();
  724. }
  725. if ( pOutData )
  726. delete []pOutData;
  727. if ( pFileData)
  728. delete []pFileData;
  729. }
  730. /************************************************************************/
  731. /* 函数:[12/21/2016 IT];
  732. /* 描述:;
  733. /* 参数:;
  734. /* [IN] :;
  735. /* [OUT] :;
  736. /* [IN/OUT] :;
  737. /* 返回:void;
  738. /* 注意:;
  739. /* 示例:;
  740. /*
  741. /* 修改:;
  742. /* 日期:;
  743. /* 内容:;
  744. /************************************************************************/
  745. void DES_DecryptFile(IN LPCTSTR lpFile, IN LPBYTE lpKey, IN LPBYTE lpVI, IN LPCTSTR lpDecryFile)
  746. {
  747. if ( lpFile == NULL )
  748. return;
  749. if ( lpKey == NULL || lpVI == NULL )
  750. return;
  751. INT nKeyLen = strlen((char*)lpKey);
  752. if (nKeyLen != 8 )
  753. return;
  754. BYTE szvi[9] = {0};
  755. INT nVILen = strlen((char*)lpVI);
  756. if ( nVILen != 8 )
  757. return;
  758. memcpy(szvi, lpVI, 8);
  759. CFile cf;
  760. BYTE *pFileData = NULL;
  761. BYTE *pOutData = NULL;
  762. UINT64 nFileLen = 0;
  763. if ( cf.Open(lpFile, CFile::modeRead) )
  764. {
  765. nFileLen = cf.GetLength();
  766. pFileData = new BYTE[nFileLen];
  767. memset(pFileData, 0, nFileLen);
  768. cf.Read(pFileData,nFileLen);
  769. cf.Close();
  770. }
  771. else
  772. {
  773. DWORD dwError = GetLastError();
  774. TRACE("%d", dwError);
  775. return;
  776. }
  777. if ( strncmp((char*)pFileData, "<?>", 3) != 0 )
  778. {// 不是加密文件;
  779. delete []pFileData;
  780. return;
  781. }
  782. // 加密的长度,必定是8的倍数;
  783. UINT64 nOutLen = nFileLen-3-sizeof(UINT64);
  784. pOutData = new BYTE[nOutLen + 1];
  785. memset(pOutData, 0, nOutLen + 1);
  786. des_context des;
  787. if ( des_key_check_key_parity(lpKey) == 1)
  788. {
  789. des_key_set_parity(lpKey);
  790. if ( des_key_check_key_parity(lpKey) == 0 )
  791. {
  792. des_setkey_dec(&des, lpKey);
  793. }
  794. }
  795. else
  796. des_setkey_dec(&des, lpKey);
  797. des_crypt_cbc(&des, DES_DECRYPT, nOutLen, szvi, pFileData+3+sizeof(UINT64), pOutData);
  798. CFile cfo;
  799. memcpy(&nOutLen, pFileData+3, sizeof(UINT64)); // 获取实际文件长度;
  800. if ( cfo.Open(lpDecryFile, CFile::modeCreate|CFile::modeWrite) )
  801. {
  802. cfo.Write(pOutData, nOutLen);
  803. cfo.Close();
  804. }
  805. if ( pOutData )
  806. delete []pOutData;
  807. if (pFileData)
  808. delete []pFileData;
  809. }