PageRenderTime 67ms CodeModel.GetById 31ms RepoModel.GetById 7ms app.codeStats 0ms

/WebconnectDataService/NHDevice/NHDeviceClass/NHSecurity.cs

https://gitlab.com/d.sreejith/MIS_wds
C# | 317 lines | 311 code | 4 blank | 2 comment | 0 complexity | bd63be8b02660c75917d6594764e12c3 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace NHDeviceClass
  6. {
  7. public static class NHSecurity
  8. {
  9. static ASCIIEncoding enc = new ASCIIEncoding();
  10. // encryption constants
  11. public const byte LOWEST_VALID_CHARACTER = 0x20;
  12. public const byte HIGHEST_VALID_CHARACTER = 0x7A;
  13. public const byte NUMBER_OF_CHARACTERS_EXCLUDED_FROM_RANGE = 14;
  14. public const byte EXCLUDE_FROM_RANGE = 0xFF;
  15. public const byte VALID_CHARACTER_RANGE_SIZE = (((HIGHEST_VALID_CHARACTER - LOWEST_VALID_CHARACTER) + 1) - NUMBER_OF_CHARACTERS_EXCLUDED_FROM_RANGE);
  16. static public char[] CodecWorkingRangeToAsciiConversionTable = new char[VALID_CHARACTER_RANGE_SIZE] //+ NUMBER_OF_CHARACTERS_EXCLUDED_FROM_RANGE - 1 ] =
  17. {
  18. '!',
  19. '#',
  20. '%',
  21. '(',
  22. ')',
  23. '*',
  24. '+',
  25. ',',
  26. '-',
  27. '.',
  28. '/',
  29. '0',
  30. '1',
  31. '2',
  32. '3',
  33. '4',
  34. '5',
  35. '6',
  36. '7',
  37. '8',
  38. '9',
  39. ':',
  40. ';',
  41. '=',
  42. '?',
  43. 'A',
  44. 'B',
  45. 'C',
  46. 'D',
  47. 'E',
  48. 'F',
  49. 'G',
  50. 'H',
  51. 'I',
  52. 'J',
  53. 'K',
  54. 'L',
  55. 'M',
  56. 'N',
  57. 'O',
  58. 'P',
  59. 'Q',
  60. 'R',
  61. 'S',
  62. 'T',
  63. 'U',
  64. 'V',
  65. 'W',
  66. 'X',
  67. 'Y',
  68. 'Z', // 4 characters removed from here
  69. 'a',
  70. 'b',
  71. 'c',
  72. 'd',
  73. 'e',
  74. 'f',
  75. 'g',
  76. 'h',
  77. 'i',
  78. 'j',
  79. 'k',
  80. 'l',
  81. 'm',
  82. 'n',
  83. 'o',
  84. 'p',
  85. 'q',
  86. 'r',
  87. 's',
  88. 't',
  89. 'u',
  90. 'v',
  91. 'w',
  92. 'x',
  93. 'y',
  94. 'z'
  95. };
  96. static public byte[] AsciiToCodecWorkingRangeConversionTable = new byte[VALID_CHARACTER_RANGE_SIZE + NUMBER_OF_CHARACTERS_EXCLUDED_FROM_RANGE]
  97. {
  98. // table converts ascii character to range used in EncryptCharacter
  99. EXCLUDE_FROM_RANGE, // space
  100. ' ' - ' ', // !
  101. EXCLUDE_FROM_RANGE, // " not permitted
  102. '!' - ' ', // #
  103. EXCLUDE_FROM_RANGE, // $
  104. '"' - ' ', // %
  105. EXCLUDE_FROM_RANGE, // & not permitted
  106. EXCLUDE_FROM_RANGE, // ' not permitted
  107. '#' - ' ', // (
  108. '$' - ' ', // )
  109. '%' - ' ', // *
  110. '&' - ' ', // +
  111. '\'' - ' ', // ,
  112. '(' - ' ', // -
  113. ')' - ' ', // .
  114. '*' - ' ', // /
  115. '+' - ' ', // 0
  116. ',' - ' ', // 1
  117. '-' - ' ', // 2
  118. '.' - ' ', // 3
  119. '/' - ' ', // 4
  120. '0' - ' ', // 5
  121. '1' - ' ', // 6
  122. '2' - ' ', // 7
  123. '3' - ' ', // 8
  124. '4' - ' ', // 9
  125. '5' - ' ', // :
  126. '6' - ' ', // ;
  127. EXCLUDE_FROM_RANGE, // < not permitted
  128. '7' - ' ', // =
  129. EXCLUDE_FROM_RANGE, // > not permitted
  130. '8' - ' ', // ?
  131. EXCLUDE_FROM_RANGE, // @
  132. '9' - ' ', // A
  133. ':' - ' ', // B
  134. ';' - ' ', // C
  135. '<' - ' ', // D
  136. '=' - ' ', // E
  137. '>' - ' ', // F
  138. '?' - ' ', // G
  139. '@' - ' ', // H
  140. 'A' - ' ', // I
  141. 'B' - ' ', // J
  142. 'C' - ' ', // K
  143. 'D' - ' ', // L
  144. 'E' - ' ', // M
  145. 'F' - ' ', // N
  146. 'G' - ' ', // O
  147. 'H' - ' ', // P
  148. 'I' - ' ', // Q
  149. 'J' - ' ', // R
  150. 'K' - ' ', // S
  151. 'L' - ' ', // T
  152. 'M' - ' ', // U
  153. 'N' - ' ', // V
  154. 'O' - ' ', // W
  155. 'P' - ' ', // X
  156. 'Q' - ' ', // Y
  157. 'R' - ' ', // Z
  158. EXCLUDE_FROM_RANGE, // [ not permitted GSM
  159. EXCLUDE_FROM_RANGE, // \ not permitted GSM
  160. EXCLUDE_FROM_RANGE, // ] not permitted GSM
  161. EXCLUDE_FROM_RANGE, // ^ not permitted GSM
  162. EXCLUDE_FROM_RANGE, // _ // 'V' etc different here on down from original
  163. EXCLUDE_FROM_RANGE, // `
  164. 'S' - ' ', // a
  165. 'T' - ' ', // b
  166. 'U' - ' ', // c
  167. 'V' - ' ', // d
  168. 'W' - ' ', // e
  169. 'X' - ' ', // f
  170. 'Y' - ' ', // g
  171. 'Z' - ' ', // h
  172. '[' - ' ', // i
  173. '\\' - ' ', // j
  174. ']' - ' ', // k
  175. '^' - ' ', // l
  176. '_' - ' ', // m
  177. '`' - ' ', // n
  178. 'a' - ' ', // o
  179. 'b' - ' ', // p
  180. 'c' - ' ', // q
  181. 'd' - ' ', // r
  182. 'e' - ' ', // s
  183. 'f' - ' ', // t
  184. 'g' - ' ', // u
  185. 'h' - ' ', // v
  186. 'i' - ' ', // w
  187. 'j' - ' ', // x
  188. 'k' - ' ', // y
  189. 'l' - ' ', // z
  190. //EXCLUDE_FROM_RANGE, // { not permitted GSM
  191. //EXCLUDE_FROM_RANGE, // | not permitted GSM
  192. //EXCLUDE_FROM_RANGE, // } not permitted GSM
  193. //EXCLUDE_FROM_RANGE, // ~ not permitted GSM
  194. };
  195. //
  196. // EncryptMessage
  197. //
  198. // on entry the message is located in CommandMessage
  199. // and the Encrypted message is put in Buffer
  200. // and the encryption seed is passed in 'Seed'.
  201. //
  202. public static string EncryptMessage(char Seed, string CommandMessage)
  203. {
  204. int i;
  205. string Buffer = "";
  206. Buffer += Seed; // Make sure start of buffer has the unencrypted seed to use for decoding
  207. // encrypt message into temporary buffer
  208. for (i = 0; i < CommandMessage.Length; i++)
  209. {
  210. Seed = EncryptCharacter(Seed, CommandMessage[i]);
  211. Buffer += Seed;
  212. };
  213. return (Buffer);
  214. }
  215. public static char EncryptCharacter(char es, char ch)
  216. {
  217. ch = (char)AsciiToCodecWorkingRangeConversionTable[ch - LOWEST_VALID_CHARACTER]; // move character from range 0x20 - 0x7E to 0x00 - ( 0x5E - 5 )
  218. es = (char)AsciiToCodecWorkingRangeConversionTable[es - LOWEST_VALID_CHARACTER]; // move seed from range 0x20 - 0x7E to 0x00 - ( 0x5E - 5 )
  219. ch += es;
  220. ch %= (char)VALID_CHARACTER_RANGE_SIZE;
  221. return CodecWorkingRangeToAsciiConversionTable[(byte)ch]; // move character from range 0x00 - ( 0x5E - 5 ) to permitted ascii range
  222. }
  223. //
  224. // DecryptMessage
  225. //
  226. // on entry the encoded message is located in Buffer
  227. // and the Decoded message is put in local DecodedMessage buffer
  228. // and the encryption seed is passed in 'Seed'.
  229. //
  230. public static string DecryptMessage(string Buffer)
  231. {
  232. int i;
  233. string DecodedMessage = "";
  234. // decrypt message into temporary buffer
  235. i = 0;
  236. char Seed;
  237. try
  238. {
  239. Seed = Buffer[i++];
  240. Seed = DecryptCharacter(Seed, Buffer[i++]);
  241. if (Seed.CompareTo('~') == 0)
  242. return ("ERROR DecryptCharacter");
  243. DecodedMessage += Seed;
  244. }
  245. catch (Exception ex)
  246. {
  247. return ("ERROR DecryptMessage[" + ex.Message.ToString());
  248. }
  249. do
  250. {
  251. i--;
  252. Seed = Buffer[i++];
  253. Seed = DecryptCharacter(Seed, Buffer[i++]);
  254. if (Seed.CompareTo('~') == 0)
  255. return ("ERROR DecryptCharacter");
  256. DecodedMessage += Seed;
  257. } while (i < Buffer.Length);
  258. return (DecodedMessage);
  259. }
  260. public static char DecryptCharacter(char ds, char ch)
  261. {
  262. try
  263. {
  264. ch = (char)AsciiToCodecWorkingRangeConversionTable[ch - LOWEST_VALID_CHARACTER]; // move character from range 0x20 - 0x7E to 0x00 - ( 0x5E - 5 )
  265. ds = (char)AsciiToCodecWorkingRangeConversionTable[ds - LOWEST_VALID_CHARACTER]; // move seed from range 0x20 - 0x7E to 0x00 - ( 0x5E - 5 )
  266. if (ds > ch)
  267. {
  268. ch += (char)VALID_CHARACTER_RANGE_SIZE;
  269. }
  270. ch -= ds;
  271. return CodecWorkingRangeToAsciiConversionTable[ch]; // move character from range 0x00 - ( 0x5E - 5 ) to permitted ascii range
  272. }
  273. catch (Exception)
  274. {
  275. return ('~');
  276. }
  277. }
  278. public static byte CheckSum(string MessageString)
  279. {
  280. byte ChecksumByte = 0;
  281. byte[] pString;
  282. byte ch;
  283. pString = enc.GetBytes(MessageString);
  284. for (int indx = 0; indx < MessageString.Length; indx++)
  285. {
  286. ch = pString[indx];
  287. ChecksumByte ^= ch;
  288. }
  289. return (ChecksumByte);
  290. }
  291. }
  292. }