/// <summary> /// Handles the decryption of the message if needed. /// We need this handled here because the encrypted messages is sent in a different encoding as opposed to the /// unencrypted message. The encrypted message is sent in binary and the unencrypted message is sent UTF-8. /// So an encrypted message should be decrypted first or the conversion might remove some characters or even /// shorten the actual string. /// </summary> /// <param name="bytes">The actual message as a byte array</param> /// <returns>The unencrypted message as a string without STX/ETX</returns> public static string DecryptPlainOrEncryptedMessage(byte[] bytes) { Logger.LogDebug(HexEncoding.ByteArrayToHexString(bytes)); string message = string.Empty; bool isInputPlainText = true; try { // Assume that the message is encrypted (this will become the default). It this fails, it's plain. message = MiniRijndael.DecryptStringFromBytes(bytes); isInputPlainText = !IsPlainTextQboxMessage(message); } catch (ArgumentException) { // Ignore, handled below. } catch (CryptographicException) { // Ignore, handled below. } if (isInputPlainText) { message = Encoding.UTF8.GetString(bytes); Logger.LogDebug("Encryption off"); } else { Logger.LogDebug("Encryption on"); } Logger.LogDebug("Decrypted: {message}", message); //todo: check refactor this smell. The message is not correctly delimited by the chars. return(message.WithoutStxEtxEnvelope().Trim()); }