Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using PCSC;
- using PCSC.Utils;
- using PCSC.Exceptions;
- namespace lab4BDJD
- {
- public partial class Form1 : Form
- {
- private static SCardError error;
- private static SCardReader reader;
- private static System.IntPtr ptr;
- private static SCardContext context;
- private static string hexData;
- public Form1()
- {
- InitializeComponent();
- }
- private void button1_Click(object sender, EventArgs e)
- {
- byte[] command;
- try
- {
- Connect();
- command = new byte[] { 0xA0, 0xA4, 0x00, 0x00, 0x02, 0x7F, 0x10 };
- SendCommand(command, "SELECT(TELECOM)");
- command = new byte[] { 0xA0, 0xC0, 0x00, 0x00, 0x0F };
- SendCommand(command, "GET RESPONSE");
- command = new byte[] { 0xA0, 0xA4, 0x00, 0x00, 0x02, 0x6F, 0x3C };
- SendCommand(command, "SELECT SMS");
- command = new byte[] { 0xA0, 0xB2, 0x01, 0x04, 0xB0 };
- SendCommand(command, "READ RECORD", true);
- command = new byte[] { 0xA0, 0xC0, 0x00, 0x00, 0x0F };
- SendCommand(command, "GET RESPONSE");
- }
- catch(PCSCException ex)
- {
- MessageBox.Show(ex.Message);
- }
- }
- private void SendCommand(byte[] cmd, string name, bool isSMS = false)
- {
- hexData = "";
- var receivedData = new byte[256];
- error = reader.Transmit(ptr, cmd, ref receivedData);
- if (error != SCardError.Success)
- {
- MessageBox.Show(SCardHelper.StringifyError(error));
- }
- WriteResponse(receivedData, name, isSMS);
- }
- private void WriteResponse(byte[] data, string name, bool isSMS)
- {
- var builder = new StringBuilder();
- foreach(byte v in data)
- {
- builder.Append(string.Format("{0:X2} ", v));
- }
- var result = builder.ToString();
- if (isSMS)
- {
- richTextBox1.AppendText(result);
- ConvertToAscii(data);
- }
- richTextBox3.Text += $"{name}:\n{result}\n\n";
- }
- private void ConvertToAscii(byte[] data)
- {
- data = data.Skip(24).Where(x => x != 0xFF).ToArray();
- var msgBytes = UnpackGsm(data);
- var builder = new StringBuilder();
- foreach (var bt in msgBytes)
- {
- builder.Append(bt);
- }
- Encoding.UTF8.GetString(msgBytes).ToString();
- richTextBox2.Text = Encoding.UTF8.GetString(msgBytes).ToString();
- }
- private byte[] UnpackGsm(byte[] data)
- {
- byte[] shifted = new byte[(data.Length * 8) / 7];
- byte[] decodeMask = new byte[] { 128, 192, 224, 240, 248, 252, 254 };
- int shiftIndex = 0;
- int shiftOffset = 0;
- foreach (byte bt in data)
- {
- if (shiftOffset == 7)
- {
- shifted[shiftIndex] = 0;
- shiftOffset = 0;
- shiftIndex++;
- }
- shifted[shiftIndex] = (byte)((bt << shiftOffset) & 127);
- shiftOffset++;
- shiftIndex++;
- }
- int moveOffset = 0;
- int moveIndex = 0;
- int unpackIndex = 1;
- byte[] unpackedBytes = new byte[shifted.Length];
- if (shifted.Length > 0)
- {
- unpackedBytes[unpackIndex - 1] = shifted[unpackIndex - 1];
- }
- foreach (byte b in data)
- {
- if (unpackIndex != shifted.Length)
- {
- if (moveOffset == 7)
- {
- moveOffset = 0;
- unpackIndex++;
- unpackedBytes[unpackIndex - 1] = shifted[unpackIndex - 1];
- }
- if (unpackIndex != shifted.Length)
- {
- int extractedBitsByte = (data[moveIndex] & decodeMask[moveOffset]);
- extractedBitsByte = (extractedBitsByte >> (7 - moveOffset));
- int movedBitsByte = (extractedBitsByte | shifted[unpackIndex]);
- unpackedBytes[unpackIndex] = (byte)movedBitsByte;
- moveOffset++;
- unpackIndex++;
- moveIndex++;
- }
- }
- }
- if (unpackedBytes[unpackedBytes.Length - 1] == 0)
- {
- byte[] finalResultBytes = new byte[unpackedBytes.Length - 1];
- Array.Copy(unpackedBytes, 0, finalResultBytes, 0, finalResultBytes.Length);
- return finalResultBytes;
- }
- return unpackedBytes;
- }
- private void Connect()
- {
- context = new SCardContext();
- context.Establish(SCardScope.System);
- var readers = context.GetReaders();
- reader = new SCardReader(context);
- error = reader.Connect(readers.FirstOrDefault(), SCardShareMode.Shared, SCardProtocol.T0 | SCardProtocol.T1);
- if(error != SCardError.Success)
- {
- MessageBox.Show(SCardHelper.StringifyError(error));
- }
- switch (reader.ActiveProtocol)
- {
- case SCardProtocol.T0:
- ptr = SCardPCI.T0;
- break;
- case SCardProtocol.T1:
- ptr = SCardPCI.T1;
- break;
- default:
- throw new PCSCException(SCardError.CardUnsupported, "Not supported protocol");
- }
- richTextBox1.Text = hexData;
- }
- private void richTextBox3_TextChanged(object sender, EventArgs e)
- {
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement