Connecting to a cell phone in an easy way and getting information is very important for some applications such as SMS senders and Bluetooth connections and this tip shows how to connect to cell phones that are connected to the computer by Bluetooth or USB cable that have one COM port and request an AT Command from mobile set.
Decrypting and encrypting files and data is a very sophisticated subject but I am using that in this tip just for the main subject.
The project scenario steps are as follows:
Text and binary files format and their standards vary, for example character standards (ASCII, Unicode v1,2,3,4 and other), video and image files header and data.
For this reason, read and write files must be done in a basic way. Read and write byte to byte (8 bit) from source to target.
From the static method in the SerialPort class load all COM (serial) ports to port combo:
static
SerialPort
private void btnRefreshports_Click(object sender, EventArgs e) { cmbPort.Items.Clear(); string[] lPorts = SerialPort.GetPortNames(); foreach (string str in lPorts) { cmbPort.Items.Add(str); } if (cmbPort.Items.Count > 0) cmbPort.SelectedIndex = 0; }
Before reading the mobile serial number, you must accept your mobile clarification about computer connection or pair your mobile set with computer before running the application.
AT: Attention Command
AT+CGSN: AT Command for requesting product serial number identification (IMEI)
private string GetMobileSerialNumber(string PortName) { string key = ""; SerialPort serialPort = new SerialPort(); serialPort.PortName = PortName; serialPort.BaudRate = 56700; try { if (!(serialPort.IsOpen)) serialPort.Open(); serialPort.Write("AT\r\n"); Thread.Sleep(3000); key = serialPort.ReadExisting(); serialPort.Write("AT+CGSN\r\n"); Thread.Sleep(3000); key = serialPort.ReadExisting(); serialPort.Close(); string Serial = ""; for (int i = 0; i < key.Length; i++) if (char.IsDigit(key[i])) Serial += key[i]; return Serial; } catch (Exception ex) { MessageBox.Show("Error in opening/writing to serial port :: " + ex.Message, "Error!"); return ""; } }
This method reads from the source file fileName from byte number Offset size of bc (byte count):
fileName
Offset
bc
public byte[] ReadFileToByteArray(string fileName, int bc, long offset) { try { byte[] buff = null; FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read); BinaryReader br = new BinaryReader(fs); br.BaseStream.Position = offset; buff = br.ReadBytes(bc); fs.Close(); return buff; } catch { return null; } }
This method writes to the target file fileName from byte number Offset size of bc (byte count):
public bool WriteBytesToFile(byte[] buff, string fileName, int bc, long offset) { try { FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Write); BinaryWriter br = new BinaryWriter(fs); br.BaseStream.Position = offset; br.Write(buff, 0, bc); fs.Close(); return true; } catch { return false; } }
The following method is the manager method for other method calls:
In this method, call the serial number reader method and split the source file in smaller sizes, for example, 1 Mega Byte and read from the source by using the ReadFileToByteArray method and Decrypt/Encrypt bytes and write by WriteBytesToFile method.
ReadFileToByteArray
WriteBytesToFile
private void btnEncDec_Click(object sender, EventArgs e) { if (cmbPort.Text != "" && txtSource.Text != "" && txtDistenation.Text != "") { string Key = GetMobileSerialNumber(cmbPort.Text); if (Key != "") { try { Stream stream = new FileStream (txtDistenation.Text, FileMode.Create, FileAccess.Write, FileShare.Read); stream.Close(); stream = new FileStream (txtSource.Text, FileMode.Open, FileAccess.Read, FileShare.Read); long len = stream.Length; stream.Close(); if (len>=1) { try { byte[] buffer; long Used=0; int Mb=(int) Math.Pow(2,20); while (Used < len) { int Itr = 0; if (Used + Mb < len) { Itr = Mb; } else Itr = (int)(len-Used); if (Itr >= 1) { buffer = ReadFileToByteArray(txtSource.Text, Itr, Used); for (int i = 0; i < Itr; i++) { buffer[i] = (byte)(buffer[i] ^ Key[i%Key.Length]); } WriteBytesToFile(buffer, txtDistenation.Text, Itr, Used); Used += Itr; } else break; } MessageBox.Show("Successfully Done!"); } catch { MessageBox.Show("Error!"); } } } catch { MessageBox.Show("Error!"); } } } else MessageBox.Show("Please Fill Information"); }