Advertisement
Guest User

Untitled

a guest
Aug 25th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 28.58 KB | None | 0 0
  1. using System;
  2.  
  3. namespace Crypt
  4. {
  5. public static class CryptSharp
  6. {
  7. private const string m_encryptionSaltCharacters =
  8. "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789./";
  9.  
  10. private const int m_desIterations = 16;
  11.  
  12. private static readonly uint[] m_saltTranslation =
  13. {
  14. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  15. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  16. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  17. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  19. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  20. 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
  21. 0x0A, 0x0B, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A,
  22. 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12,
  23. 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A,
  24. 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20, 0x21, 0x22,
  25. 0x23, 0x24, 0x25, 0x20, 0x21, 0x22, 0x23, 0x24,
  26. 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C,
  27. 0x2D, 0x2E, 0x2F, 0x30, 0x31, 0x32, 0x33, 0x34,
  28. 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C,
  29. 0x3D, 0x3E, 0x3F, 0x00, 0x00, 0x00, 0x00, 0x00
  30. };
  31.  
  32. private static readonly bool[] m_shifts =
  33. {
  34. false, false, true, true, true, true, true, true,
  35. false, true, true, true, true, true, true, false
  36. };
  37.  
  38. private static readonly uint[,] m_skb =
  39. {
  40. {
  41. 0x00000000, 0x00000010, 0x20000000, 0x20000010,
  42. 0x00010000, 0x00010010, 0x20010000, 0x20010010,
  43. 0x00000800, 0x00000810, 0x20000800, 0x20000810,
  44. 0x00010800, 0x00010810, 0x20010800, 0x20010810,
  45. 0x00000020, 0x00000030, 0x20000020, 0x20000030,
  46. 0x00010020, 0x00010030, 0x20010020, 0x20010030,
  47. 0x00000820, 0x00000830, 0x20000820, 0x20000830,
  48. 0x00010820, 0x00010830, 0x20010820, 0x20010830,
  49. 0x00080000, 0x00080010, 0x20080000, 0x20080010,
  50. 0x00090000, 0x00090010, 0x20090000, 0x20090010,
  51. 0x00080800, 0x00080810, 0x20080800, 0x20080810,
  52. 0x00090800, 0x00090810, 0x20090800, 0x20090810,
  53. 0x00080020, 0x00080030, 0x20080020, 0x20080030,
  54. 0x00090020, 0x00090030, 0x20090020, 0x20090030,
  55. 0x00080820, 0x00080830, 0x20080820, 0x20080830,
  56. 0x00090820, 0x00090830, 0x20090820, 0x20090830
  57. },
  58. {
  59. 0x00000000, 0x02000000, 0x00002000, 0x02002000,
  60. 0x00200000, 0x02200000, 0x00202000, 0x02202000,
  61. 0x00000004, 0x02000004, 0x00002004, 0x02002004,
  62. 0x00200004, 0x02200004, 0x00202004, 0x02202004,
  63. 0x00000400, 0x02000400, 0x00002400, 0x02002400,
  64. 0x00200400, 0x02200400, 0x00202400, 0x02202400,
  65. 0x00000404, 0x02000404, 0x00002404, 0x02002404,
  66. 0x00200404, 0x02200404, 0x00202404, 0x02202404,
  67. 0x10000000, 0x12000000, 0x10002000, 0x12002000,
  68. 0x10200000, 0x12200000, 0x10202000, 0x12202000,
  69. 0x10000004, 0x12000004, 0x10002004, 0x12002004,
  70. 0x10200004, 0x12200004, 0x10202004, 0x12202004,
  71. 0x10000400, 0x12000400, 0x10002400, 0x12002400,
  72. 0x10200400, 0x12200400, 0x10202400, 0x12202400,
  73. 0x10000404, 0x12000404, 0x10002404, 0x12002404,
  74. 0x10200404, 0x12200404, 0x10202404, 0x12202404
  75. },
  76. {
  77. 0x00000000, 0x00000001, 0x00040000, 0x00040001,
  78. 0x01000000, 0x01000001, 0x01040000, 0x01040001,
  79. 0x00000002, 0x00000003, 0x00040002, 0x00040003,
  80. 0x01000002, 0x01000003, 0x01040002, 0x01040003,
  81. 0x00000200, 0x00000201, 0x00040200, 0x00040201,
  82. 0x01000200, 0x01000201, 0x01040200, 0x01040201,
  83. 0x00000202, 0x00000203, 0x00040202, 0x00040203,
  84. 0x01000202, 0x01000203, 0x01040202, 0x01040203,
  85. 0x08000000, 0x08000001, 0x08040000, 0x08040001,
  86. 0x09000000, 0x09000001, 0x09040000, 0x09040001,
  87. 0x08000002, 0x08000003, 0x08040002, 0x08040003,
  88. 0x09000002, 0x09000003, 0x09040002, 0x09040003,
  89. 0x08000200, 0x08000201, 0x08040200, 0x08040201,
  90. 0x09000200, 0x09000201, 0x09040200, 0x09040201,
  91. 0x08000202, 0x08000203, 0x08040202, 0x08040203,
  92. 0x09000202, 0x09000203, 0x09040202, 0x09040203
  93. },
  94. {
  95. 0x00000000, 0x00100000, 0x00000100, 0x00100100,
  96. 0x00000008, 0x00100008, 0x00000108, 0x00100108,
  97. 0x00001000, 0x00101000, 0x00001100, 0x00101100,
  98. 0x00001008, 0x00101008, 0x00001108, 0x00101108,
  99. 0x04000000, 0x04100000, 0x04000100, 0x04100100,
  100. 0x04000008, 0x04100008, 0x04000108, 0x04100108,
  101. 0x04001000, 0x04101000, 0x04001100, 0x04101100,
  102. 0x04001008, 0x04101008, 0x04001108, 0x04101108,
  103. 0x00020000, 0x00120000, 0x00020100, 0x00120100,
  104. 0x00020008, 0x00120008, 0x00020108, 0x00120108,
  105. 0x00021000, 0x00121000, 0x00021100, 0x00121100,
  106. 0x00021008, 0x00121008, 0x00021108, 0x00121108,
  107. 0x04020000, 0x04120000, 0x04020100, 0x04120100,
  108. 0x04020008, 0x04120008, 0x04020108, 0x04120108,
  109. 0x04021000, 0x04121000, 0x04021100, 0x04121100,
  110. 0x04021008, 0x04121008, 0x04021108, 0x04121108
  111. },
  112. {
  113. 0x00000000, 0x10000000, 0x00010000, 0x10010000,
  114. 0x00000004, 0x10000004, 0x00010004, 0x10010004,
  115. 0x20000000, 0x30000000, 0x20010000, 0x30010000,
  116. 0x20000004, 0x30000004, 0x20010004, 0x30010004,
  117. 0x00100000, 0x10100000, 0x00110000, 0x10110000,
  118. 0x00100004, 0x10100004, 0x00110004, 0x10110004,
  119. 0x20100000, 0x30100000, 0x20110000, 0x30110000,
  120. 0x20100004, 0x30100004, 0x20110004, 0x30110004,
  121. 0x00001000, 0x10001000, 0x00011000, 0x10011000,
  122. 0x00001004, 0x10001004, 0x00011004, 0x10011004,
  123. 0x20001000, 0x30001000, 0x20011000, 0x30011000,
  124. 0x20001004, 0x30001004, 0x20011004, 0x30011004,
  125. 0x00101000, 0x10101000, 0x00111000, 0x10111000,
  126. 0x00101004, 0x10101004, 0x00111004, 0x10111004,
  127. 0x20101000, 0x30101000, 0x20111000, 0x30111000,
  128. 0x20101004, 0x30101004, 0x20111004, 0x30111004
  129. },
  130. {
  131. 0x00000000, 0x08000000, 0x00000008, 0x08000008,
  132. 0x00000400, 0x08000400, 0x00000408, 0x08000408,
  133. 0x00020000, 0x08020000, 0x00020008, 0x08020008,
  134. 0x00020400, 0x08020400, 0x00020408, 0x08020408,
  135. 0x00000001, 0x08000001, 0x00000009, 0x08000009,
  136. 0x00000401, 0x08000401, 0x00000409, 0x08000409,
  137. 0x00020001, 0x08020001, 0x00020009, 0x08020009,
  138. 0x00020401, 0x08020401, 0x00020409, 0x08020409,
  139. 0x02000000, 0x0A000000, 0x02000008, 0x0A000008,
  140. 0x02000400, 0x0A000400, 0x02000408, 0x0A000408,
  141. 0x02020000, 0x0A020000, 0x02020008, 0x0A020008,
  142. 0x02020400, 0x0A020400, 0x02020408, 0x0A020408,
  143. 0x02000001, 0x0A000001, 0x02000009, 0x0A000009,
  144. 0x02000401, 0x0A000401, 0x02000409, 0x0A000409,
  145. 0x02020001, 0x0A020001, 0x02020009, 0x0A020009,
  146. 0x02020401, 0x0A020401, 0x02020409, 0x0A020409
  147. },
  148. {
  149. 0x00000000, 0x00000100, 0x00080000, 0x00080100,
  150. 0x01000000, 0x01000100, 0x01080000, 0x01080100,
  151. 0x00000010, 0x00000110, 0x00080010, 0x00080110,
  152. 0x01000010, 0x01000110, 0x01080010, 0x01080110,
  153. 0x00200000, 0x00200100, 0x00280000, 0x00280100,
  154. 0x01200000, 0x01200100, 0x01280000, 0x01280100,
  155. 0x00200010, 0x00200110, 0x00280010, 0x00280110,
  156. 0x01200010, 0x01200110, 0x01280010, 0x01280110,
  157. 0x00000200, 0x00000300, 0x00080200, 0x00080300,
  158. 0x01000200, 0x01000300, 0x01080200, 0x01080300,
  159. 0x00000210, 0x00000310, 0x00080210, 0x00080310,
  160. 0x01000210, 0x01000310, 0x01080210, 0x01080310,
  161. 0x00200200, 0x00200300, 0x00280200, 0x00280300,
  162. 0x01200200, 0x01200300, 0x01280200, 0x01280300,
  163. 0x00200210, 0x00200310, 0x00280210, 0x00280310,
  164. 0x01200210, 0x01200310, 0x01280210, 0x01280310
  165. },
  166. {
  167. 0x00000000, 0x04000000, 0x00040000, 0x04040000,
  168. 0x00000002, 0x04000002, 0x00040002, 0x04040002,
  169. 0x00002000, 0x04002000, 0x00042000, 0x04042000,
  170. 0x00002002, 0x04002002, 0x00042002, 0x04042002,
  171. 0x00000020, 0x04000020, 0x00040020, 0x04040020,
  172. 0x00000022, 0x04000022, 0x00040022, 0x04040022,
  173. 0x00002020, 0x04002020, 0x00042020, 0x04042020,
  174. 0x00002022, 0x04002022, 0x00042022, 0x04042022,
  175. 0x00000800, 0x04000800, 0x00040800, 0x04040800,
  176. 0x00000802, 0x04000802, 0x00040802, 0x04040802,
  177. 0x00002800, 0x04002800, 0x00042800, 0x04042800,
  178. 0x00002802, 0x04002802, 0x00042802, 0x04042802,
  179. 0x00000820, 0x04000820, 0x00040820, 0x04040820,
  180. 0x00000822, 0x04000822, 0x00040822, 0x04040822,
  181. 0x00002820, 0x04002820, 0x00042820, 0x04042820,
  182. 0x00002822, 0x04002822, 0x00042822, 0x04042822
  183. }
  184. };
  185.  
  186. private static readonly uint[,] m_SPTranslationTable =
  187. {
  188. {
  189. 0x00820200, 0x00020000, 0x80800000, 0x80820200,
  190. 0x00800000, 0x80020200, 0x80020000, 0x80800000,
  191. 0x80020200, 0x00820200, 0x00820000, 0x80000200,
  192. 0x80800200, 0x00800000, 0x00000000, 0x80020000,
  193. 0x00020000, 0x80000000, 0x00800200, 0x00020200,
  194. 0x80820200, 0x00820000, 0x80000200, 0x00800200,
  195. 0x80000000, 0x00000200, 0x00020200, 0x80820000,
  196. 0x00000200, 0x80800200, 0x80820000, 0x00000000,
  197. 0x00000000, 0x80820200, 0x00800200, 0x80020000,
  198. 0x00820200, 0x00020000, 0x80000200, 0x00800200,
  199. 0x80820000, 0x00000200, 0x00020200, 0x80800000,
  200. 0x80020200, 0x80000000, 0x80800000, 0x00820000,
  201. 0x80820200, 0x00020200, 0x00820000, 0x80800200,
  202. 0x00800000, 0x80000200, 0x80020000, 0x00000000,
  203. 0x00020000, 0x00800000, 0x80800200, 0x00820200,
  204. 0x80000000, 0x80820000, 0x00000200, 0x80020200
  205. },
  206. {
  207. 0x10042004, 0x00000000, 0x00042000, 0x10040000,
  208. 0x10000004, 0x00002004, 0x10002000, 0x00042000,
  209. 0x00002000, 0x10040004, 0x00000004, 0x10002000,
  210. 0x00040004, 0x10042000, 0x10040000, 0x00000004,
  211. 0x00040000, 0x10002004, 0x10040004, 0x00002000,
  212. 0x00042004, 0x10000000, 0x00000000, 0x00040004,
  213. 0x10002004, 0x00042004, 0x10042000, 0x10000004,
  214. 0x10000000, 0x00040000, 0x00002004, 0x10042004,
  215. 0x00040004, 0x10042000, 0x10002000, 0x00042004,
  216. 0x10042004, 0x00040004, 0x10000004, 0x00000000,
  217. 0x10000000, 0x00002004, 0x00040000, 0x10040004,
  218. 0x00002000, 0x10000000, 0x00042004, 0x10002004,
  219. 0x10042000, 0x00002000, 0x00000000, 0x10000004,
  220. 0x00000004, 0x10042004, 0x00042000, 0x10040000,
  221. 0x10040004, 0x00040000, 0x00002004, 0x10002000,
  222. 0x10002004, 0x00000004, 0x10040000, 0x00042000
  223. },
  224. {
  225. 0x41000000, 0x01010040, 0x00000040, 0x41000040,
  226. 0x40010000, 0x01000000, 0x41000040, 0x00010040,
  227. 0x01000040, 0x00010000, 0x01010000, 0x40000000,
  228. 0x41010040, 0x40000040, 0x40000000, 0x41010000,
  229. 0x00000000, 0x40010000, 0x01010040, 0x00000040,
  230. 0x40000040, 0x41010040, 0x00010000, 0x41000000,
  231. 0x41010000, 0x01000040, 0x40010040, 0x01010000,
  232. 0x00010040, 0x00000000, 0x01000000, 0x40010040,
  233. 0x01010040, 0x00000040, 0x40000000, 0x00010000,
  234. 0x40000040, 0x40010000, 0x01010000, 0x41000040,
  235. 0x00000000, 0x01010040, 0x00010040, 0x41010000,
  236. 0x40010000, 0x01000000, 0x41010040, 0x40000000,
  237. 0x40010040, 0x41000000, 0x01000000, 0x41010040,
  238. 0x00010000, 0x01000040, 0x41000040, 0x00010040,
  239. 0x01000040, 0x00000000, 0x41010000, 0x40000040,
  240. 0x41000000, 0x40010040, 0x00000040, 0x01010000
  241. },
  242. {
  243. 0x00100402, 0x04000400, 0x00000002, 0x04100402,
  244. 0x00000000, 0x04100000, 0x04000402, 0x00100002,
  245. 0x04100400, 0x04000002, 0x04000000, 0x00000402,
  246. 0x04000002, 0x00100402, 0x00100000, 0x04000000,
  247. 0x04100002, 0x00100400, 0x00000400, 0x00000002,
  248. 0x00100400, 0x04000402, 0x04100000, 0x00000400,
  249. 0x00000402, 0x00000000, 0x00100002, 0x04100400,
  250. 0x04000400, 0x04100002, 0x04100402, 0x00100000,
  251. 0x04100002, 0x00000402, 0x00100000, 0x04000002,
  252. 0x00100400, 0x04000400, 0x00000002, 0x04100000,
  253. 0x04000402, 0x00000000, 0x00000400, 0x00100002,
  254. 0x00000000, 0x04100002, 0x04100400, 0x00000400,
  255. 0x04000000, 0x04100402, 0x00100402, 0x00100000,
  256. 0x04100402, 0x00000002, 0x04000400, 0x00100402,
  257. 0x00100002, 0x00100400, 0x04100000, 0x04000402,
  258. 0x00000402, 0x04000000, 0x04000002, 0x04100400
  259. },
  260. {
  261. 0x02000000, 0x00004000, 0x00000100, 0x02004108,
  262. 0x02004008, 0x02000100, 0x00004108, 0x02004000,
  263. 0x00004000, 0x00000008, 0x02000008, 0x00004100,
  264. 0x02000108, 0x02004008, 0x02004100, 0x00000000,
  265. 0x00004100, 0x02000000, 0x00004008, 0x00000108,
  266. 0x02000100, 0x00004108, 0x00000000, 0x02000008,
  267. 0x00000008, 0x02000108, 0x02004108, 0x00004008,
  268. 0x02004000, 0x00000100, 0x00000108, 0x02004100,
  269. 0x02004100, 0x02000108, 0x00004008, 0x02004000,
  270. 0x00004000, 0x00000008, 0x02000008, 0x02000100,
  271. 0x02000000, 0x00004100, 0x02004108, 0x00000000,
  272. 0x00004108, 0x02000000, 0x00000100, 0x00004008,
  273. 0x02000108, 0x00000100, 0x00000000, 0x02004108,
  274. 0x02004008, 0x02004100, 0x00000108, 0x00004000,
  275. 0x00004100, 0x02004008, 0x02000100, 0x00000108,
  276. 0x00000008, 0x00004108, 0x02004000, 0x02000008
  277. },
  278. {
  279. 0x20000010, 0x00080010, 0x00000000, 0x20080800,
  280. 0x00080010, 0x00000800, 0x20000810, 0x00080000,
  281. 0x00000810, 0x20080810, 0x00080800, 0x20000000,
  282. 0x20000800, 0x20000010, 0x20080000, 0x00080810,
  283. 0x00080000, 0x20000810, 0x20080010, 0x00000000,
  284. 0x00000800, 0x00000010, 0x20080800, 0x20080010,
  285. 0x20080810, 0x20080000, 0x20000000, 0x00000810,
  286. 0x00000010, 0x00080800, 0x00080810, 0x20000800,
  287. 0x00000810, 0x20000000, 0x20000800, 0x00080810,
  288. 0x20080800, 0x00080010, 0x00000000, 0x20000800,
  289. 0x20000000, 0x00000800, 0x20080010, 0x00080000,
  290. 0x00080010, 0x20080810, 0x00080800, 0x00000010,
  291. 0x20080810, 0x00080800, 0x00080000, 0x20000810,
  292. 0x20000010, 0x20080000, 0x00080810, 0x00000000,
  293. 0x00000800, 0x20000010, 0x20000810, 0x20080800,
  294. 0x20080000, 0x00000810, 0x00000010, 0x20080010
  295. },
  296. {
  297. 0x00001000, 0x00000080, 0x00400080, 0x00400001,
  298. 0x00401081, 0x00001001, 0x00001080, 0x00000000,
  299. 0x00400000, 0x00400081, 0x00000081, 0x00401000,
  300. 0x00000001, 0x00401080, 0x00401000, 0x00000081,
  301. 0x00400081, 0x00001000, 0x00001001, 0x00401081,
  302. 0x00000000, 0x00400080, 0x00400001, 0x00001080,
  303. 0x00401001, 0x00001081, 0x00401080, 0x00000001,
  304. 0x00001081, 0x00401001, 0x00000080, 0x00400000,
  305. 0x00001081, 0x00401000, 0x00401001, 0x00000081,
  306. 0x00001000, 0x00000080, 0x00400000, 0x00401001,
  307. 0x00400081, 0x00001081, 0x00001080, 0x00000000,
  308. 0x00000080, 0x00400001, 0x00000001, 0x00400080,
  309. 0x00000000, 0x00400081, 0x00400080, 0x00001080,
  310. 0x00000081, 0x00001000, 0x00401081, 0x00400000,
  311. 0x00401080, 0x00000001, 0x00001001, 0x00401081,
  312. 0x00400001, 0x00401080, 0x00401000, 0x00001001
  313. },
  314. {
  315. 0x08200020, 0x08208000, 0x00008020, 0x00000000,
  316. 0x08008000, 0x00200020, 0x08200000, 0x08208020,
  317. 0x00000020, 0x08000000, 0x00208000, 0x00008020,
  318. 0x00208020, 0x08008020, 0x08000020, 0x08200000,
  319. 0x00008000, 0x00208020, 0x00200020, 0x08008000,
  320. 0x08208020, 0x08000020, 0x00000000, 0x00208000,
  321. 0x08000000, 0x00200000, 0x08008020, 0x08200020,
  322. 0x00200000, 0x00008000, 0x08208000, 0x00000020,
  323. 0x00200000, 0x00008000, 0x08000020, 0x08208020,
  324. 0x00008020, 0x08000000, 0x00000000, 0x00208000,
  325. 0x08200020, 0x08008020, 0x08008000, 0x00200020,
  326. 0x08208000, 0x00000020, 0x00200020, 0x08008000,
  327. 0x08208020, 0x00200000, 0x08200000, 0x08000020,
  328. 0x00208000, 0x00008020, 0x08008020, 0x08200000,
  329. 0x00000020, 0x08208000, 0x00208020, 0x00000000,
  330. 0x08000000, 0x08200020, 0x00008000, 0x00208020
  331. }
  332. };
  333.  
  334. private static readonly uint[] m_characterConversionTable =
  335. {
  336. 0x2E, 0x2F, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35,
  337. 0x36, 0x37, 0x38, 0x39, 0x41, 0x42, 0x43, 0x44,
  338. 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x4B, 0x4C,
  339. 0x4D, 0x4E, 0x4F, 0x50, 0x51, 0x52, 0x53, 0x54,
  340. 0x55, 0x56, 0x57, 0x58, 0x59, 0x5A, 0x61, 0x62,
  341. 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6A,
  342. 0x6B, 0x6C, 0x6D, 0x6E, 0x6F, 0x70, 0x71, 0x72,
  343. 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7A
  344. };
  345.  
  346. private static readonly Random _random = new Random();
  347.  
  348. public static string Crypt(ReadOnlySpan<char> textToEncrypt)
  349. {
  350. int maxGeneratedNumber = m_encryptionSaltCharacters.Length;
  351. int randomIndex;
  352. Span<char> encryptionSalt = stackalloc char[2];
  353. for (int index = 0; index < 2; index++)
  354. {
  355. randomIndex = _random.Next(maxGeneratedNumber);
  356. encryptionSalt[index] = m_encryptionSaltCharacters[randomIndex];
  357. }
  358.  
  359. return Crypt(encryptionSalt, textToEncrypt);
  360. }
  361.  
  362. public static string Crypt(ReadOnlySpan<char> encryptionSalt, ReadOnlySpan<char> textToEncrypt)
  363. {
  364. if (encryptionSalt.Length != 2)
  365. throw new ArgumentException("Encryption salt must be 2 characters long.", nameof(encryptionSalt));
  366.  
  367. Span<byte> encryptionKey = stackalloc byte[8];
  368. for (int index = 0; index < encryptionKey.Length && index < textToEncrypt.Length; index++)
  369. encryptionKey[index] = (byte)(Convert.ToInt32(textToEncrypt[index]) << 1);
  370.  
  371. Span<uint> schedule = stackalloc uint[m_desIterations * 2];
  372. SetDESKey(encryptionKey, schedule);
  373. uint firstSaltTranslator = m_saltTranslation[Convert.ToUInt32(encryptionSalt[0])];
  374. uint secondSaltTranslator = m_saltTranslation[Convert.ToUInt32(encryptionSalt[1])] << 4;
  375. Span<uint> singleOutputKey = stackalloc uint[2];
  376. Body(schedule, firstSaltTranslator, secondSaltTranslator, singleOutputKey);
  377. Span<byte> binaryBuffer = stackalloc byte[9];
  378. IntToFourBytes(singleOutputKey[0], binaryBuffer, 0);
  379. IntToFourBytes(singleOutputKey[1], binaryBuffer, 4);
  380. binaryBuffer[8] = 0;
  381. int binaryBufferIndex = 0;
  382. uint bitChecker = 0x80;
  383. Span<char> encryptionBuffer = stackalloc char[13];
  384. encryptionBuffer[0] = encryptionSalt[0];
  385. encryptionBuffer[1] = encryptionSalt[1];
  386. for (int index = 2; index < 13; index++)
  387. {
  388. uint passwordCharacter = 0;
  389. for (int secondIndex = 0; secondIndex < 6; secondIndex++)
  390. {
  391. passwordCharacter <<= 1;
  392. if ((binaryBuffer[binaryBufferIndex] & bitChecker) != 0)
  393. passwordCharacter |= 1;
  394.  
  395. bitChecker >>= 1;
  396. if (bitChecker == 0)
  397. {
  398. binaryBufferIndex++;
  399. bitChecker = 0x80;
  400. }
  401. }
  402.  
  403. encryptionBuffer[index] = Convert.ToChar(m_characterConversionTable[passwordCharacter]);
  404. }
  405.  
  406. return encryptionBuffer.ToString();
  407. }
  408.  
  409. private static void SetDESKey(Span<byte> encryptionKey, Span<uint> schedule)
  410. {
  411. uint firstInt = FourBytesToInt(encryptionKey, 0);
  412. uint secondInt = FourBytesToInt(encryptionKey, 4);
  413. Span<uint> operationResults = stackalloc uint[2];
  414. PermOperation(secondInt, firstInt, 4, 0x0F0F0F0F, operationResults);
  415. firstInt = HPermOperation(operationResults[1], -2, 0xCCCC0000);
  416. secondInt = HPermOperation(operationResults[0], -2, 0xCCCC0000);
  417. PermOperation(secondInt, firstInt, 1, 0x55555555, operationResults);
  418. secondInt = operationResults[0];
  419. firstInt = operationResults[1];
  420. PermOperation(firstInt, secondInt, 8, 0x00FF00FF, operationResults);
  421. firstInt = operationResults[0];
  422. secondInt = operationResults[1];
  423. PermOperation(secondInt, firstInt, 1, 0x55555555, operationResults);
  424. firstInt = operationResults[1] & 0x0FFFFFFF;
  425. secondInt = (((operationResults[0] & 0xFF) << 16) | (operationResults[0] & 0xFF00) |
  426. ((operationResults[0] & 0xFF0000) >> 16) | ((operationResults[1] & 0xF0000000) >> 4));
  427.  
  428. int scheduleIndex = 0;
  429. for (int index = 0; index < m_desIterations; index++)
  430. {
  431. if (m_shifts[index])
  432. {
  433. firstInt = (firstInt >> 2) | (firstInt << 26);
  434. secondInt = (secondInt >> 2) | (secondInt << 26);
  435. }
  436. else
  437. {
  438. firstInt = (firstInt >> 1) | (firstInt << 27);
  439. secondInt = (secondInt >> 1) | (secondInt << 27);
  440. }
  441.  
  442. firstInt &= 0x0FFFFFFF;
  443. secondInt &= 0xFFFFFFF;
  444. uint firstSkbValue = m_skb[0, firstInt & 0x3F] |
  445. m_skb[1, ((firstInt >> 6) & 0x03) | ((firstInt >> 7) & 0x3C)] |
  446. m_skb[2, ((firstInt >> 13) & 0x0F) | ((firstInt >> 14) & 0x30)] |
  447. m_skb[3, ((firstInt >> 20) & 0x01) | ((firstInt >> 21) & 0x06) | ((firstInt >> 22) & 0x38)];
  448.  
  449. uint secondSkbValue = m_skb[4, secondInt & 0x3F] |
  450. m_skb[5, ((secondInt >> 7) & 0x03) | ((secondInt >> 8) & 0x3C)] |
  451. m_skb[6, (secondInt >> 15) & 0x3F] |
  452. m_skb[7, ((secondInt >> 21) & 0x0F) | ((secondInt >> 22) & 0x30)];
  453.  
  454. schedule[scheduleIndex++] = ((secondSkbValue << 16) | (firstSkbValue & 0xFFFF)) & 0xFFFFFFFF;
  455. firstSkbValue = ((firstSkbValue >> 16) | (secondSkbValue & 0xFFFF0000));
  456. firstSkbValue = (firstSkbValue << 4) | (firstSkbValue >> 28);
  457. schedule[scheduleIndex++] = firstSkbValue & 0xFFFFFFFF;
  458. }
  459. }
  460.  
  461. private static void Body(Span<uint> schedule, uint firstSaltTranslator, uint secondSaltTranslator, Span<uint> singleOutputKey)
  462. {
  463. uint left = 0;
  464. uint right = 0;
  465. uint tempInt;
  466. for (int index = 0; index < 25; index++)
  467. {
  468. for (int secondIndex = 0; secondIndex < m_desIterations * 2; secondIndex += 4)
  469. {
  470. left = DEncrypt(left, right, secondIndex, firstSaltTranslator, secondSaltTranslator, schedule);
  471. right = DEncrypt(right, left, secondIndex + 2, firstSaltTranslator, secondSaltTranslator, schedule);
  472. }
  473.  
  474. tempInt = left;
  475. left = right;
  476. right = tempInt;
  477. }
  478.  
  479. tempInt = right;
  480. right = ((left >> 1) | (left << 31)) & 0xFFFFFFFF;
  481. left = ((tempInt >> 1) | (tempInt << 31)) & 0xFFFFFFFF;
  482. Span<uint> operationResults = stackalloc uint[2];
  483. PermOperation(right, left, 1, 0x55555555, operationResults);
  484. right = operationResults[0];
  485. left = operationResults[1];
  486. PermOperation(left, right, 8, 0x00FF00FF, operationResults);
  487. left = operationResults[0];
  488. right = operationResults[1];
  489. PermOperation(right, left, 2, 0x33333333, operationResults);
  490. right = operationResults[0];
  491. left = operationResults[1];
  492. PermOperation(left, right, 16, 0xFFFF, operationResults);
  493. left = operationResults[0];
  494. right = operationResults[1];
  495. PermOperation(right, left, 4, 0x0F0F0F0F, operationResults);
  496. singleOutputKey[0] = operationResults[1];
  497. singleOutputKey[1] = operationResults[0];
  498. }
  499.  
  500. private static uint DEncrypt(uint left, uint right, int scheduleIndex, uint firstSaltTranslator, uint secondSaltTranslator, Span<uint> schedule)
  501. {
  502. uint thirdInt = right ^ (right >> 16);
  503. uint secondInt = thirdInt & firstSaltTranslator;
  504. thirdInt = thirdInt & secondSaltTranslator;
  505. secondInt = (secondInt ^ (secondInt << 16)) ^ right ^ schedule[scheduleIndex];
  506. uint firstInt = (thirdInt ^ (thirdInt << 16)) ^ right ^ schedule[scheduleIndex + 1];
  507. firstInt = (firstInt >> 4) | (firstInt << 28);
  508. left ^= (m_SPTranslationTable[1, firstInt & 0x3F] |
  509. m_SPTranslationTable[3, (firstInt >> 8) & 0x3F] |
  510. m_SPTranslationTable[5, (firstInt >> 16) & 0x3F] |
  511. m_SPTranslationTable[7, (firstInt >> 24) & 0x3F] |
  512. m_SPTranslationTable[0, secondInt & 0x3F] |
  513. m_SPTranslationTable[2, (secondInt >> 8) & 0x3F] |
  514. m_SPTranslationTable[4, (secondInt >> 16) & 0x3F] |
  515. m_SPTranslationTable[6, (secondInt >> 24) & 0x3F]);
  516.  
  517. return left;
  518. }
  519.  
  520. private static uint FourBytesToInt(Span<byte> inputBytes, int offset)
  521. {
  522. int resultValue;
  523. resultValue = (inputBytes[offset++] & 0xFF);
  524. resultValue |= (inputBytes[offset++] & 0xFF) << 8;
  525. resultValue |= (inputBytes[offset++] & 0xFF) << 16;
  526. resultValue |= (inputBytes[offset] & 0xFF) << 24;
  527. return (uint)resultValue;
  528. }
  529.  
  530. private static void IntToFourBytes(uint inputInt, Span<byte> outputBytes, int offset)
  531. {
  532. outputBytes[offset++] = (byte)(inputInt & 0xFF);
  533. outputBytes[offset++] = (byte)((inputInt >> 8) & 0xFF);
  534. outputBytes[offset++] = (byte)((inputInt >> 16) & 0xFF);
  535. outputBytes[offset] = (byte)((inputInt >> 24) & 0xFF);
  536. }
  537.  
  538. private static void PermOperation(uint firstInt, uint secondInt, uint thirdInt, uint fourthInt, Span<uint> operationResults)
  539. {
  540. uint tempInt = ((firstInt >> (int)thirdInt) ^ secondInt) & fourthInt;
  541. firstInt ^= tempInt << (int)thirdInt;
  542. secondInt ^= tempInt;
  543. operationResults[0] = firstInt;
  544. operationResults[1] = secondInt;
  545. }
  546.  
  547. private static uint HPermOperation(uint firstInt, int secondInt, uint thirdInt)
  548. {
  549. uint tempInt = ((firstInt << (16 - secondInt)) ^ firstInt) & thirdInt;
  550. return firstInt ^ tempInt ^ (tempInt >> (16 - secondInt));
  551. }
  552. }
  553. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement