/// <summary>Initializes a new instance of the <see cref="SCPdu"/> structure.</summary> /// <param name="chatID">The field representing the chat ID.</param> /// <param name="type">The field representing the PDU type.</param> /// <param name="encoding">The field representing the character encoding used in the payload of this PDU.</param> /// <param name="payload">The field representing the non-encrypted binary content of this PDU (calculated accordingly to the encoding).</param> public SCPdu(string chatID, SCPduType type, Encoding encoding, byte[] payload) { this.chatID = chatID; this.type = type; this.payload = payload; this.encoding = encoding; }
/// <summary>Encrypts this PDU and provides its binary representation.</summary> /// <param name="key">The encryption key to be used.</param> /// <returns>The binary representation of the PDU to be sent through the network.</returns> public byte[] ToBinary(byte[] key) { if (key.Length != 16) { throw new ScedaException("A SCEDA key must be 16 bytes long."); } string type; switch (this.Type) { case SCPduType.Hello: type = "HLO"; break; case SCPduType.Welcome: type = "ACK"; break; case SCPduType.Leave: type = "LEV"; break; case SCPduType.Message: type = "MSG"; break; case SCPduType.MalformedPduNotification: type = "BAD"; break; case SCPduType.NicknameConflictNotification: type = "CNF"; break; default: type = ""; break; } byte[] iv = SCEDA.GenerateIV(); return((new byte[] { 0, 1 }).Concat(Encoding.ASCII.GetBytes(this.ChatID + '\0')).Concat(iv).Concat(SCEDA.Encrypt(Encoding.ASCII.GetBytes(type).Concat(Encoding.ASCII.GetBytes(this.Encoding.BodyName + '\0')).Concat(this.Payload).ToArray(), key, iv)).ToArray()); }
/// <summary>Encrypts this PDU and provides its binary representation.</summary> /// <param name="key">The encryption key to be used.</param> /// <returns>The binary representation of the PDU to be sent through the network.</returns> public byte[] ToBinary(byte[] key) { if (key.Length != 16) { throw new ScedaException("A SCEDA key must be 16 bytes long."); } string type; switch (this.Type) { case SCPduType.Hello: type = "HLO"; break; case SCPduType.Welcome: type = "ACK"; break; case SCPduType.Leave: type = "LEV"; break; case SCPduType.Message: type = "MSG"; break; case SCPduType.MalformedPduNotification: type = "BAD"; break; case SCPduType.NicknameConflictNotification: type = "CNF"; break; default: type = ""; break; } byte[] iv = SCEDA.GenerateIV(); return (new byte[] { 0, 1 }).Concat(Encoding.ASCII.GetBytes(this.ChatID + '\0')).Concat(iv).Concat(SCEDA.Encrypt(Encoding.ASCII.GetBytes(type).Concat(Encoding.ASCII.GetBytes(this.Encoding.BodyName + '\0')).Concat(this.Payload).ToArray(), key, iv)).ToArray(); }