NewCamdMessage ParseMessage(byte[] buffer) { _logger.Debug("Read TripleDES Initialization Vector from encrypted message"); var ivec = buffer.Skip(buffer.Length - 8).Take(8).ToArray(); _logger.Debug("Decrypt the rest of the message"); var decryptedData = _crypto.Decrypt(buffer, buffer.Length - 8, _keyblock, ivec); _logger.Debug("Parse decrypted message"); var len = (((decryptedData[3 + NewCamdMessage.HeaderLength] << 8) | decryptedData[4 + NewCamdMessage.HeaderLength]) & 0x0FFF) + 3; if (len > decryptedData.Length) { throw new InvalidNewcamdMessage($"Decryption of the message from {Name} failed"); } var retValue = new NewCamdMessage { MessageId = ((decryptedData[0] << 8) | decryptedData[1]) & 0xFFFF, ServiceId = ((decryptedData[2] << 8) | decryptedData[3]) & 0xFFFF, ProviderId = decryptedData[4] << 16 | decryptedData[5] << 8 | decryptedData[6], Data = decryptedData.Skip(2 + NewCamdMessage.HeaderLength).Take(len).ToArray(), Type = (NewCamdMessageType)decryptedData[2 + NewCamdMessage.HeaderLength] }; _logger.Debug($"Received decrypted message msgid: {retValue.MessageId}, serviceid: {retValue.ServiceId}, providerid: {retValue.ProviderId}, length: {len}, type {retValue.Type} from {Name}"); return(retValue); }
#pragma warning disable CA1822 // Mark members as static public void EncryptAndDecrypt() #pragma warning restore CA1822 // Mark members as static { const string Unencoded = "Foo"; byte[] encoded = EncryptionHelpers.Encrypt( Encoding.UTF8.GetBytes(Unencoded)); string decoded = Encoding.UTF8.GetString( EncryptionHelpers.Decrypt(encoded)); Assert.Equal(Unencoded, decoded); }