CRCCrypter.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Security.Cryptography;
  5. using System.IO;
  6. namespace Wayne.FDCPOSLibrary
  7. {
  8. public class CRC16
  9. {
  10. static public ushort CalculateCRC(string data)
  11. {
  12. ushort crc = 0;
  13. return crc;
  14. }
  15. static public ushort CalculateCRC(int deviceId, long transactionId, double amount, double volume)
  16. {
  17. ushort crc = 0;
  18. return crc;
  19. }
  20. static public bool checkCRC(ushort CRC, int deviceId, long transactionId, double amount, double volume)
  21. {
  22. bool result = true;
  23. return result;
  24. }
  25. public enum InitialCrcValue { Zeros, NonZero1 = 0xffff, NonZero2 = 0x1D0F }
  26. }
  27. public enum InitialCrcValue { Zeros, NonZero1 = 0xffff, NonZero2 = 0x1D0F }
  28. public class Crc16Ccitt
  29. {
  30. ushort poly = 4129;
  31. ushort[] table = new ushort[256];
  32. ushort initialValue = 0;
  33. public ushort ComputeChecksum(byte[] bytes)
  34. {
  35. ushort crc = this.initialValue;
  36. for (int i = 0; i < bytes.Length; ++i)
  37. {
  38. crc = (ushort)((crc << 8) ^ table[((crc >> 8) ^ (0xff & bytes[i]))]);
  39. }
  40. return crc;
  41. }
  42. public byte[] ComputeChecksumBytes(byte[] bytes)
  43. {
  44. ushort crc = ComputeChecksum(bytes);
  45. return BitConverter.GetBytes(crc);
  46. }
  47. public Crc16Ccitt(InitialCrcValue initialValue, ushort poly)
  48. {
  49. this.poly = poly;
  50. this.initialValue = (ushort)initialValue;
  51. Init();
  52. }
  53. public Crc16Ccitt(InitialCrcValue initialValue)
  54. {
  55. this.initialValue = (ushort)initialValue;
  56. Init();
  57. }
  58. private void Init()
  59. {
  60. ushort temp, a;
  61. for (int i = 0; i < table.Length; ++i)
  62. {
  63. temp = 0;
  64. a = (ushort)(i << 8);
  65. for (int j = 0; j < 8; ++j)
  66. {
  67. if (((temp ^ a) & 0x8000) != 0)
  68. {
  69. temp = (ushort)((temp << 1) ^ poly);
  70. }
  71. else
  72. {
  73. temp <<= 1;
  74. }
  75. a <<= 1;
  76. }
  77. table[i] = temp;
  78. }
  79. }
  80. //public ushort crccalc(ushort data, ushort genpoly, ushort accum)
  81. //{
  82. // static int i;
  83. // data <<= 1;
  84. // for (i=8;i>0;i++)
  85. // {
  86. // data>>=1;
  87. // if ((data^accum) && (0x0001))
  88. // accum = (accum>>1)^genpoly;
  89. // else
  90. // accum >>= 1;
  91. // }
  92. // return accum;
  93. //}
  94. }
  95. }