public IgnitionPipeData Decrypt(string encryptedDataStr) { if (string.IsNullOrEmpty(encryptedDataStr)) { return(null); } // convert string to bytes var encryptedBytes = Convert.FromBase64String(encryptedDataStr.Replace('-', '+').Replace('_', '/')); var flag = (DataFlag)encryptedBytes[0]; // get AES keys if (flag == DataFlag.Key) { var combinedAesKeyAndIVEncrypted = encryptedBytes.Skip(1).ToArray(); InitializeAesDecryptor(combinedAesKeyAndIVEncrypted); return(null); } var dataBytes = encryptedBytes.Skip(1).ToArray(); var dateBytes = dataBytes.Take(8).ToArray(); var date = Encoding.UTF8.GetString(dateBytes); string dataMessage; using (var msDecrypt = new MemoryStream(dataBytes.Skip(8).ToArray())) { using (var csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)) { using (var swDecrypt = new StreamReader(csDecrypt)) { dataMessage = swDecrypt.ReadToEnd(); } } } var ignitionPipeData = new IgnitionPipeData { TimeString = date, Message = dataMessage }; return(ignitionPipeData); }
public void Send(IgnitionPipeData pipeData) { if (!pipeClient.IsConnected || pipeData == null || string.IsNullOrEmpty(pipeData.Message)) { return; } var messageTime = ParseTime(pipeData.TimeString); if (previousTime.HasValue) { var delay = messageTime - previousTime.Value; Thread.Sleep((int)delay.TotalMilliseconds); } previousTime = messageTime; var bytesToSend = Encoding.ASCII.GetBytes(pipeData.Message); pipeClient.Write(bytesToSend, 0, bytesToSend.Count()); pipeClient.Flush(); }