//private DecryptedDaedalusPacket _clearPacket; //internal DecryptedDaedalusPacket clearPacket { get; set; } #endregion #region Constructors /// <summary> /// Builds a DaedalusPacket object from an encrypted byte stream. /// This constructor assumes the buffer passed to it has already been verified using DaedalusPacket.IsValidPacket /// and starts with STX and ends with EOT, with no extraneous bytes. /// </summary> internal EncryptedDaedalusPacket(byte[] inBuffer, int startIndex, int length, out DaedalusTestApp.DaedalusGlobal.ReturnCodes returnCode, string AESkey) { // Check for STX and EOT in the right places if ((inBuffer[startIndex] != GlobalConstants.SOH) || (inBuffer[startIndex + length - 1] != GlobalConstants.EOT)) { returnCode = DaedalusGlobal.ReturnCodes.InvalidPacketStructure; return; } encryptionKey = AESkey; // Parse packet elements encryptedPacketLengthFieldValue = BitConverter.ToUInt16(inBuffer, elementPacketLength.ElementStaticOffset + startIndex); // Copy encrypted payload to internal object encryptedPayload = new byte[startIndex + encryptedPacketLengthFieldValue - elementETX.ElementSize]; Array.Copy(inBuffer, startIndex + elementEncryptedPacket.ElementStaticOffset, encryptedPayload, 0, encryptedPacketLengthFieldValue - elementETX.ElementSize); // Decrypt <encryptedPacket> //decryptedPayload = Convert.FromBase64String(AesEncryptamajig.Decrypt(Convert.ToBase64String( // encryptedPayload), AESkey)); // Force accessor to perform decryption operation and sync payloads byte[] temp = decryptedPayload; returnCode = DaedalusGlobal.ReturnCodes.Valid; }
/// <summary> /// Builds an DaedalusPacket object from a byte stream. /// This constructor assumes the buffer passed to it has already been verified using DaedalusPacket.IsValidPacket /// and starts with STX and ends with EOT, with no extraneous bytes. /// </summary> internal DecryptedDaedalusPacket(byte[] inBuffer, int startIndex, int length, out DaedalusTestApp.DaedalusGlobal.ReturnCodes returnCode) { // Check for STX and EOT in the right places if ((inBuffer[startIndex] != GlobalConstants.STX) || (inBuffer[startIndex + length - 1] != GlobalConstants.EOT)) { returnCode = DaedalusGlobal.ReturnCodes.InvalidPacketStructure; return; } // Parse packet elements packetLength = BitConverter.ToUInt16(inBuffer, startIndex + elementPacketLength.ElementStaticOffset); packetIndex = BitConverter.ToUInt16(inBuffer, startIndex + elementPacketIndex.ElementStaticOffset); //hash = new IDHash(inBuffer, startIndex + elementIDHash.ElementOffset); command = (Commands)inBuffer[startIndex + elementCommand.ElementStaticOffset]; commandVersion = inBuffer[startIndex + elementCommandVersion.ElementStaticOffset]; ushort payloadLength = BitConverter.ToUInt16(inBuffer, startIndex + elementCommandPayloadLength.ElementStaticOffset); payload = new Dictionary <string, DaedalusGlobal.PayloadElement>(); // Select the first of the packet types (should only be one) that match the type of this command IEnumerable <IDaedalusCommandType> commandTypeMatches = commandTypes.Where(p => p.getCommand() == command); // Parse this packet's payload if (commandTypeMatches.Count() == 0) { #if DEBUG GlobalMethods.TestLog("Packet " + BitConverter.ToString(inBuffer, startIndex, length) + " invokes command that is not implemented."); #endif returnCode = DaedalusGlobal.ReturnCodes.CommandNotImplemented; } else { // Just in case there's more than one type for each command, take the first one this.commandType = commandTypeMatches.First(); byte[] payloadBuffer = new byte[length]; Array.Copy(inBuffer, startIndex, payloadBuffer, 0, length); Dictionary <string, DaedalusGlobal.PayloadElement> tryPayload; // Try to parse the packet's payload returnCode = commandType.parsePayload(payloadBuffer, elementCommandPayload.ElementStaticOffset, out tryPayload); this.payload = tryPayload; #if DEBUG if (returnCode == DaedalusGlobal.ReturnCodes.Valid) { GlobalMethods.TestLog("Packet " + BitConverter.ToString(inBuffer, startIndex, length) + " constructed."); } else { GlobalMethods.TestLog("Packet " + BitConverter.ToString(inBuffer, startIndex, length) + " construction failed, code: " + Enum.GetName(typeof(DaedalusGlobal.ReturnCodes), returnCode) + "."); } #endif } }