Advertisement
Guest User

Untitled

a guest
Oct 17th, 2019
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.25 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Text;
  4. using System.Windows.Forms;
  5. using PCSC;
  6. using PCSC.Utils;
  7. using PCSC.Exceptions;
  8.  
  9. namespace lab4BDJD
  10. {
  11. public partial class Form1 : Form
  12. {
  13. private static SCardError error;
  14. private static SCardReader reader;
  15. private static System.IntPtr ptr;
  16. private static SCardContext context;
  17. private static string hexData;
  18.  
  19. public Form1()
  20. {
  21. InitializeComponent();
  22. }
  23.  
  24. private void button1_Click(object sender, EventArgs e)
  25. {
  26. byte[] command;
  27.  
  28. try
  29. {
  30. Connect();
  31.  
  32. command = new byte[] { 0xA0, 0xA4, 0x00, 0x00, 0x02, 0x7F, 0x10 };
  33. SendCommand(command, "SELECT(TELECOM)");
  34.  
  35. command = new byte[] { 0xA0, 0xC0, 0x00, 0x00, 0x0F };
  36. SendCommand(command, "GET RESPONSE");
  37.  
  38. command = new byte[] { 0xA0, 0xA4, 0x00, 0x00, 0x02, 0x6F, 0x3C };
  39. SendCommand(command, "SELECT SMS");
  40.  
  41. command = new byte[] { 0xA0, 0xB2, 0x01, 0x04, 0xB0 };
  42. SendCommand(command, "READ RECORD", true);
  43.  
  44. command = new byte[] { 0xA0, 0xC0, 0x00, 0x00, 0x0F };
  45. SendCommand(command, "GET RESPONSE");
  46. }
  47. catch(PCSCException ex)
  48. {
  49. MessageBox.Show(ex.Message);
  50. }
  51.  
  52. }
  53.  
  54. private void SendCommand(byte[] cmd, string name, bool isSMS = false)
  55. {
  56. hexData = "";
  57. var receivedData = new byte[256];
  58.  
  59. error = reader.Transmit(ptr, cmd, ref receivedData);
  60.  
  61. if (error != SCardError.Success)
  62. {
  63. MessageBox.Show(SCardHelper.StringifyError(error));
  64. }
  65.  
  66. WriteResponse(receivedData, name, isSMS);
  67. }
  68.  
  69. private void WriteResponse(byte[] data, string name, bool isSMS)
  70. {
  71. var builder = new StringBuilder();
  72.  
  73. foreach(byte v in data)
  74. {
  75. builder.Append(string.Format("{0:X2} ", v));
  76. }
  77.  
  78. var result = builder.ToString();
  79.  
  80. if (isSMS)
  81. {
  82. richTextBox1.AppendText(result);
  83. ConvertToAscii(data);
  84. }
  85.  
  86. richTextBox3.Text += $"{name}:\n{result}\n\n";
  87. }
  88.  
  89. private void ConvertToAscii(byte[] data)
  90. {
  91. data = data.Skip(24).Where(x => x != 0xFF).ToArray();
  92.  
  93. var msgBytes = UnpackGsm(data);
  94.  
  95. var builder = new StringBuilder();
  96.  
  97. foreach (var bt in msgBytes)
  98. {
  99. builder.Append(bt);
  100. }
  101.  
  102. Encoding.UTF8.GetString(msgBytes).ToString();
  103.  
  104. richTextBox2.Text = Encoding.UTF8.GetString(msgBytes).ToString();
  105. }
  106.  
  107. private byte[] UnpackGsm(byte[] data)
  108. {
  109. byte[] shifted = new byte[(data.Length * 8) / 7];
  110.  
  111. byte[] decodeMask = new byte[] { 128, 192, 224, 240, 248, 252, 254 };
  112.  
  113. int shiftIndex = 0;
  114. int shiftOffset = 0;
  115.  
  116. foreach (byte bt in data)
  117. {
  118. if (shiftOffset == 7)
  119. {
  120. shifted[shiftIndex] = 0;
  121. shiftOffset = 0;
  122. shiftIndex++;
  123. }
  124.  
  125. shifted[shiftIndex] = (byte)((bt << shiftOffset) & 127);
  126.  
  127. shiftOffset++;
  128. shiftIndex++;
  129. }
  130.  
  131. int moveOffset = 0;
  132. int moveIndex = 0;
  133. int unpackIndex = 1;
  134. byte[] unpackedBytes = new byte[shifted.Length];
  135.  
  136. if (shifted.Length > 0)
  137. {
  138. unpackedBytes[unpackIndex - 1] = shifted[unpackIndex - 1];
  139. }
  140.  
  141. foreach (byte b in data)
  142. {
  143. if (unpackIndex != shifted.Length)
  144. {
  145. if (moveOffset == 7)
  146. {
  147. moveOffset = 0;
  148. unpackIndex++;
  149. unpackedBytes[unpackIndex - 1] = shifted[unpackIndex - 1];
  150. }
  151.  
  152. if (unpackIndex != shifted.Length)
  153. {
  154. int extractedBitsByte = (data[moveIndex] & decodeMask[moveOffset]);
  155.  
  156. extractedBitsByte = (extractedBitsByte >> (7 - moveOffset));
  157.  
  158. int movedBitsByte = (extractedBitsByte | shifted[unpackIndex]);
  159.  
  160. unpackedBytes[unpackIndex] = (byte)movedBitsByte;
  161.  
  162. moveOffset++;
  163. unpackIndex++;
  164. moveIndex++;
  165. }
  166. }
  167. }
  168.  
  169. if (unpackedBytes[unpackedBytes.Length - 1] == 0)
  170. {
  171. byte[] finalResultBytes = new byte[unpackedBytes.Length - 1];
  172. Array.Copy(unpackedBytes, 0, finalResultBytes, 0, finalResultBytes.Length);
  173.  
  174. return finalResultBytes;
  175. }
  176.  
  177. return unpackedBytes;
  178. }
  179.  
  180. private void Connect()
  181. {
  182. context = new SCardContext();
  183.  
  184. context.Establish(SCardScope.System);
  185. var readers = context.GetReaders();
  186.  
  187. reader = new SCardReader(context);
  188. error = reader.Connect(readers.FirstOrDefault(), SCardShareMode.Shared, SCardProtocol.T0 | SCardProtocol.T1);
  189.  
  190. if(error != SCardError.Success)
  191. {
  192. MessageBox.Show(SCardHelper.StringifyError(error));
  193. }
  194.  
  195. switch (reader.ActiveProtocol)
  196. {
  197. case SCardProtocol.T0:
  198. ptr = SCardPCI.T0;
  199. break;
  200. case SCardProtocol.T1:
  201. ptr = SCardPCI.T1;
  202. break;
  203. default:
  204. throw new PCSCException(SCardError.CardUnsupported, "Not supported protocol");
  205. }
  206.  
  207. richTextBox1.Text = hexData;
  208. }
  209.  
  210. private void richTextBox3_TextChanged(object sender, EventArgs e)
  211. {
  212.  
  213. }
  214. }
  215. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement