private static void Main(string[] args) { var math = Wiskunde.Wiskunde.GetInstance(); float x = 5; float y = 7; float result = math.Divide(x, y); Console.WriteLine(result); result = math.Multiply(result, 2); Console.WriteLine(result); result = math.Subtract(result, 4); Console.WriteLine(result); result = math.Sum(result, x); var noEncryption = new NoEncryption(); var reverseEncryption = new ReverseEncryption(); var shiftEncryption = new ShiftEncryption(); string test = "Time flies like an arrow, fruit flies like a banana."; Console.WriteLine(noEncryption.PreformEncryption(test)); Console.WriteLine(reverseEncryption.PreformEncryption(test)); Console.WriteLine(shiftEncryption.PreformEncryption(test)); }
public void StreamingPolicyComboTest() { using (MockContext context = this.StartMockContextAndInitializeClients(this.GetType())) { try { CreateMediaServicesAccount(); // List StreamingPolicies, which should only contain the predefined policies var policies = MediaClient.StreamingPolicies.List(ResourceGroup, AccountName); Assert.Empty(policies.Where(p => !p.Name.StartsWith("Predefined_"))); string policyName = TestUtilities.GenerateName("StreamingPolicy"); // Get the StreamingPolicy, which should not exist StreamingPolicy policy = MediaClient.StreamingPolicies.Get(ResourceGroup, AccountName, policyName); Assert.Null(policy); // Create a new StreamingPolicy string defaultContentKeyPolicyName = null; CommonEncryptionCbcs commonEncryptionCbcs = null; CommonEncryptionCenc commonEncryptionCenc = null; EnvelopeEncryption envelopeEncryption = new EnvelopeEncryption(enabledProtocols: new EnabledProtocols(false, false, true, false)); NoEncryption noEncryption = null; var input = new StreamingPolicy(envelopeEncryption: envelopeEncryption); StreamingPolicy createdPolicy = MediaClient.StreamingPolicies.Create(ResourceGroup, AccountName, policyName, input); ValidateStreamingPolicy(createdPolicy, policyName, defaultContentKeyPolicyName, commonEncryptionCbcs, commonEncryptionCenc, envelopeEncryption, noEncryption); // List StreamingPolicies and validate the newly created one shows up policies = MediaClient.StreamingPolicies.List(ResourceGroup, AccountName); policy = policies.Where(p => !p.Name.StartsWith("Predefined_")).First(); ValidateStreamingPolicy(policy, policyName, defaultContentKeyPolicyName, commonEncryptionCbcs, commonEncryptionCenc, envelopeEncryption, noEncryption); // Get the newly created StreamingPolicy policy = MediaClient.StreamingPolicies.Get(ResourceGroup, AccountName, policyName); Assert.NotNull(policy); ValidateStreamingPolicy(policy, policyName, defaultContentKeyPolicyName, commonEncryptionCbcs, commonEncryptionCenc, envelopeEncryption, noEncryption); // Delete the StreamingPolicy MediaClient.StreamingPolicies.Delete(ResourceGroup, AccountName, policyName); // List StreamingPolicies, which should only contain the predefined policies policies = MediaClient.StreamingPolicies.List(ResourceGroup, AccountName); Assert.Empty(policies.Where(p => !p.Name.StartsWith("Predefined_"))); // Get the StreamingPolicy, which should not exist policy = MediaClient.StreamingPolicies.Get(ResourceGroup, AccountName, policyName); Assert.Null(policy); } finally { DeleteMediaServicesAccount(); } } }
internal static void ValidateStreamingPolicy( StreamingPolicy policy, string expectedName, string expectedDefaultContentKeyPolicyName, CommonEncryptionCbcs expectedCommonEncryptionCbcs, CommonEncryptionCenc expectedCommonEncryptionCenc, EnvelopeEncryption expectedEnvelopeEncryption, NoEncryption expectedNoEncryption) { Assert.Equal(expectedName, policy.Name); Assert.Equal(expectedDefaultContentKeyPolicyName, policy.DefaultContentKeyPolicyName); Assert.False(string.IsNullOrEmpty(policy.Id)); if (expectedCommonEncryptionCbcs == null) { Assert.Null(policy.CommonEncryptionCbcs); } else { ValidateEnabledProtocols(expectedCommonEncryptionCbcs.EnabledProtocols, policy.CommonEncryptionCbcs.EnabledProtocols); } if (expectedCommonEncryptionCenc == null) { Assert.Null(policy.CommonEncryptionCenc); } else { ValidateEnabledProtocols(expectedCommonEncryptionCenc.EnabledProtocols, policy.CommonEncryptionCenc.EnabledProtocols); } if (expectedEnvelopeEncryption == null) { Assert.Null(policy.EnvelopeEncryption); } else { ValidateEnabledProtocols(expectedEnvelopeEncryption.EnabledProtocols, policy.EnvelopeEncryption.EnabledProtocols); } if (expectedNoEncryption == null) { Assert.Null(policy.NoEncryption); } else { ValidateEnabledProtocols(expectedNoEncryption.EnabledProtocols, policy.NoEncryption.EnabledProtocols); } }
private ConfigProvider Configure(System.Configuration.Configuration configuration) { var storageProviderType = Type.GetType(configuration.AppSettings.Settings["StorageProviderType"].Value, true); var storageProviderConnection = configuration.AppSettings.Settings["StorageProviderConnection"].Value; var storageProvider = TypeFactory.CreateInstance <IConfigStorageProvider>(storageProviderType, storageProviderConnection); var serializerType = Type.GetType(configuration.AppSettings.Settings["SerializerType"].Value, true); var serializer = TypeFactory.CreateInstance <ISerialization>(serializerType); bool encrypted; IEncryption encryptor; if (configuration.AppSettings.Settings["IsEncrypted"] != null && bool.TryParse(configuration.AppSettings.Settings["IsEncrypted"].Value, out encrypted) && encrypted) { var encryptionKey = configuration.AppSettings.Settings["Key"].Value; var encryptionType = Type.GetType(configuration.AppSettings.Settings["EncryptionType"].Value, true); encryptor = TypeFactory.CreateInstance <IEncryption>(encryptionType, serializer); } else { encryptor = new NoEncryption(); } return(new ConfigProvider(storageProvider, serializer, encryptor, this.key)); }
/// <summary> /// Returns a configured instance of System.Management.Automation.PowerShell, pre-populated with the command to run. /// </summary> /// <param name="context">The NativeActivityContext for the currently running activity.</param> /// <returns>A populated instance of Sytem.Management.Automation.PowerShell</returns> /// <remarks>The infrastructure takes responsibility for closing and disposing the PowerShell instance returned.</remarks> protected override ActivityImplementationContext GetPowerShell(NativeActivityContext context) { System.Management.Automation.PowerShell invoker = global::System.Management.Automation.PowerShell.Create(); System.Management.Automation.PowerShell targetCommand = invoker.AddCommand(PSCommandName); // Initialize the arguments if (NoEncryption.Expression != null) { targetCommand.AddParameter("NoEncryption", NoEncryption.Get(context)); } if (CertificateCACheck.Expression != null) { targetCommand.AddParameter("CertificateCACheck", CertificateCACheck.Get(context)); } if (CertificateCNCheck.Expression != null) { targetCommand.AddParameter("CertificateCNCheck", CertificateCNCheck.Get(context)); } if (CertRevocationCheck.Expression != null) { targetCommand.AddParameter("CertRevocationCheck", CertRevocationCheck.Get(context)); } if (EncodePortInServicePrincipalName.Expression != null) { targetCommand.AddParameter("EncodePortInServicePrincipalName", EncodePortInServicePrincipalName.Get(context)); } if (Encoding.Expression != null) { targetCommand.AddParameter("Encoding", Encoding.Get(context)); } if (HttpPrefix.Expression != null) { targetCommand.AddParameter("HttpPrefix", HttpPrefix.Get(context)); } if (MaxEnvelopeSizeKB.Expression != null) { targetCommand.AddParameter("MaxEnvelopeSizeKB", MaxEnvelopeSizeKB.Get(context)); } if (ProxyAuthentication.Expression != null) { targetCommand.AddParameter("ProxyAuthentication", ProxyAuthentication.Get(context)); } if (ProxyCertificateThumbprint.Expression != null) { targetCommand.AddParameter("ProxyCertificateThumbprint", ProxyCertificateThumbprint.Get(context)); } if (ProxyCredential.Expression != null) { targetCommand.AddParameter("ProxyCredential", ProxyCredential.Get(context)); } if (ProxyType.Expression != null) { targetCommand.AddParameter("ProxyType", ProxyType.Get(context)); } if (UseSsl.Expression != null) { targetCommand.AddParameter("UseSsl", UseSsl.Get(context)); } if (Impersonation.Expression != null) { targetCommand.AddParameter("Impersonation", Impersonation.Get(context)); } if (PacketIntegrity.Expression != null) { targetCommand.AddParameter("PacketIntegrity", PacketIntegrity.Get(context)); } if (PacketPrivacy.Expression != null) { targetCommand.AddParameter("PacketPrivacy", PacketPrivacy.Get(context)); } if (Protocol.Expression != null) { targetCommand.AddParameter("Protocol", Protocol.Get(context)); } if (UICulture.Expression != null) { targetCommand.AddParameter("UICulture", UICulture.Get(context)); } if (Culture.Expression != null) { targetCommand.AddParameter("Culture", Culture.Get(context)); } return(new ActivityImplementationContext() { PowerShellInstance = invoker }); }
public static async Task <IActionResult> Run( [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req, ILogger log) { log.LogInformation($"AMS v3 Function - CreateStreamingPolicy was triggered!"); string requestBody = new StreamReader(req.Body).ReadToEnd(); dynamic data = JsonConvert.DeserializeObject(requestBody); if (data.streamingPolicyName == null) { return(new BadRequestObjectResult("Please pass streamingPolicyName in the input object")); } if (data.defaultContentKeyPolicyName == null) { return(new BadRequestObjectResult("Please pass defaultContentKeyPolicyName in the input object")); } string streamingPolicyName = data.streamingPolicyName; string defaultContentKeyPolicyName = data.defaultContentKeyPolicyName; string mode = data.mode; if (mode != "simple" && mode != "advanced") { return(new BadRequestObjectResult("Please pass valid mode in the input object")); } // // Simple Mode // if (mode == "simple") { if (data.noEncryptionProtocols == null && data.cbcsProtocols == null && data.cencProtocols == null && data.envelopeProtocols == null) { return(new BadRequestObjectResult("Please pass one protocol for any encryption scheme at least in the input object")); } } // // Advanced Mode // if (mode == "advanced") { if (data.jsonNoEncryption == null && data.jsonCommonEncryptionCbcs == null && data.jsonCommonEncryptionCenc == null && data.jsonEnvelopeEncryption == null) { return(new BadRequestObjectResult("Please pass one encryption scheme JSON at least in the input object")); } } MediaServicesConfigWrapper amsconfig = new MediaServicesConfigWrapper(); StreamingPolicy policy = null; JsonConverter[] jsonReaders = { new MediaServicesHelperJsonReader(), new MediaServicesHelperTimeSpanJsonConverter() }; try { IAzureMediaServicesClient client = MediaServicesHelper.CreateMediaServicesClientAsync(amsconfig); policy = client.StreamingPolicies.Get(amsconfig.ResourceGroup, amsconfig.AccountName, streamingPolicyName); if (policy == null) { StreamingPolicy parameters = new StreamingPolicy(); parameters.DefaultContentKeyPolicyName = defaultContentKeyPolicyName; if (mode == "simple") { // NoEncryption Arguments if (data.noEncryptionProtocols != null) { String[] noEncryptionProtocols = data.noEncryptionProtocols.ToString().Split(';'); if (Array.IndexOf(noEncryptionProtocols, "Dash") > -1) { parameters.NoEncryption.EnabledProtocols.Dash = true; } if (Array.IndexOf(noEncryptionProtocols, "Download") > -1) { parameters.NoEncryption.EnabledProtocols.Download = true; } if (Array.IndexOf(noEncryptionProtocols, "Hls") > -1) { parameters.NoEncryption.EnabledProtocols.Hls = true; } if (Array.IndexOf(noEncryptionProtocols, "SmoothStreaming") > -1) { parameters.NoEncryption.EnabledProtocols.SmoothStreaming = true; } } // Common Encryption CBCS Argument if (data.cbcsClearTracks != null) { List <TrackSelection> tracks = JsonConvert.DeserializeObject <List <TrackSelection> >(data.cbcsClearTracks.ToString(), jsonReaders); parameters.CommonEncryptionCbcs.ClearTracks = tracks; } if (data.cbcsDefaultKeyLabel != null) { parameters.CommonEncryptionCbcs.ContentKeys.DefaultKey.Label = data.cbcsDefaultKeyLabel; } if (data.cbcsDefaultKeyPolicyName != null) { parameters.CommonEncryptionCbcs.ContentKeys.DefaultKey.PolicyName = data.cbcsDefaultKeyPolicyName; } if (data.cbcsClearTracks != null) { List <StreamingPolicyContentKey> mappings = JsonConvert.DeserializeObject <List <StreamingPolicyContentKey> >(data.cbcsKeyToTrackMappings.ToString(), jsonReaders); parameters.CommonEncryptionCbcs.ContentKeys.KeyToTrackMappings = mappings; } if (data.cbcsFairPlayTemplate != null) { parameters.CommonEncryptionCbcs.Drm.FairPlay.CustomLicenseAcquisitionUrlTemplate = data.cbcsFairPlayTemplate; } if (data.cbcsFairPlayAllowPersistentLicense != null) { parameters.CommonEncryptionCbcs.Drm.FairPlay.AllowPersistentLicense = data.cbcsFairPlayAllowPersistentLicense; } if (data.cbcsPlayReadyTemplate != null) { parameters.CommonEncryptionCbcs.Drm.PlayReady.CustomLicenseAcquisitionUrlTemplate = data.cbcsPlayReadyTemplate; } if (data.cbcsPlayReadyAttributes != null) { parameters.CommonEncryptionCbcs.Drm.PlayReady.PlayReadyCustomAttributes = data.cbcsPlayReadyAttributes; } if (data.cbcsWidevineTemplate != null) { parameters.CommonEncryptionCbcs.Drm.Widevine.CustomLicenseAcquisitionUrlTemplate = data.cbcsWidevineTemplate; } if (data.cbcsProtocols != null) { String[] commonEncryptionCbcsProtocols = data.cbcsProtocols.ToString().Split(';'); if (Array.IndexOf(commonEncryptionCbcsProtocols, "Dash") > -1) { parameters.CommonEncryptionCbcs.EnabledProtocols.Dash = true; } if (Array.IndexOf(commonEncryptionCbcsProtocols, "Download") > -1) { parameters.CommonEncryptionCbcs.EnabledProtocols.Download = true; } if (Array.IndexOf(commonEncryptionCbcsProtocols, "Hls") > -1) { parameters.CommonEncryptionCbcs.EnabledProtocols.Hls = true; } if (Array.IndexOf(commonEncryptionCbcsProtocols, "SmoothStreaming") > -1) { parameters.CommonEncryptionCbcs.EnabledProtocols.SmoothStreaming = true; } } // Common Encryption CENC Argument if (data.cencClearTracks != null) { List <TrackSelection> tracks = JsonConvert.DeserializeObject <List <TrackSelection> >(data.cencClearTracks.ToString(), jsonReaders); parameters.CommonEncryptionCenc.ClearTracks = tracks; } if (data.cencDefaultKeyLabel != null) { parameters.CommonEncryptionCenc.ContentKeys.DefaultKey.Label = data.cencDefaultKeyLabel; } if (data.cencDefaultKeyPolicyName != null) { parameters.CommonEncryptionCenc.ContentKeys.DefaultKey.PolicyName = data.cencDefaultKeyPolicyName; } if (data.cencClearTracks != null) { List <StreamingPolicyContentKey> mappings = JsonConvert.DeserializeObject <List <StreamingPolicyContentKey> >(data.cencKeyToTrackMappings.ToString(), jsonReaders); parameters.CommonEncryptionCenc.ContentKeys.KeyToTrackMappings = mappings; } if (data.cencPlayReadyTemplate != null) { parameters.CommonEncryptionCenc.Drm.PlayReady.CustomLicenseAcquisitionUrlTemplate = data.cencPlayReadyTemplate; } if (data.cencPlayReadyAttributes != null) { parameters.CommonEncryptionCenc.Drm.PlayReady.PlayReadyCustomAttributes = data.cencPlayReadyAttributes; } if (data.cencWidevineTemplate != null) { parameters.CommonEncryptionCenc.Drm.Widevine.CustomLicenseAcquisitionUrlTemplate = data.cencWidevineTemplate; } if (data.cencProtocols != null) { String[] commonEncryptionCencProtocols = data.cencProtocols.ToString().Split(';'); if (Array.IndexOf(commonEncryptionCencProtocols, "Dash") > -1) { parameters.CommonEncryptionCenc.EnabledProtocols.Dash = true; } if (Array.IndexOf(commonEncryptionCencProtocols, "Download") > -1) { parameters.CommonEncryptionCenc.EnabledProtocols.Download = true; } if (Array.IndexOf(commonEncryptionCencProtocols, "Hls") > -1) { parameters.CommonEncryptionCenc.EnabledProtocols.Hls = true; } if (Array.IndexOf(commonEncryptionCencProtocols, "SmoothStreaming") > -1) { parameters.CommonEncryptionCenc.EnabledProtocols.SmoothStreaming = true; } } // Envelope Encryption Argument if (data.envelopeClearTracks != null || data.envelopeDefaultKeyLabel != null || data.envelopeDefaultKeyPolicyName != null || data.envelopeClearTracks || data.envelopeTemplate != null || data.envelopeTemplate != null || data.envelopeProtocols != null) { parameters.EnvelopeEncryption = new EnvelopeEncryption(); if (data.envelopeClearTracks != null) { List <TrackSelection> tracks = JsonConvert.DeserializeObject <List <TrackSelection> >(data.envelopeClearTracks.ToString(), jsonReaders); parameters.EnvelopeEncryption.ClearTracks = tracks; } parameters.EnvelopeEncryption.ContentKeys = new StreamingPolicyContentKeys(); parameters.EnvelopeEncryption.ContentKeys.DefaultKey = new DefaultKey(data.envelopeDefaultKeyLabel as string, data.envelopeDefaultKeyPolicyName as string); if (data.envelopeClearTracks != null) { List <StreamingPolicyContentKey> mappings = JsonConvert.DeserializeObject <List <StreamingPolicyContentKey> >(data.envelopeKeyToTrackMappings.ToString(), jsonReaders); parameters.EnvelopeEncryption.ContentKeys.KeyToTrackMappings = mappings; } if (data.envelopeTemplate != null) { parameters.EnvelopeEncryption.CustomKeyAcquisitionUrlTemplate = data.envelopeTemplate; } if (data.envelopeProtocols != null) { parameters.EnvelopeEncryption.EnabledProtocols = new EnabledProtocols(); String[] envelopeEncryptionProtocols = data.envelopeProtocols.ToString().Split(';'); if (Array.IndexOf(envelopeEncryptionProtocols, "Dash") > -1) { parameters.EnvelopeEncryption.EnabledProtocols.Dash = true; } if (Array.IndexOf(envelopeEncryptionProtocols, "Download") > -1) { parameters.EnvelopeEncryption.EnabledProtocols.Download = true; } if (Array.IndexOf(envelopeEncryptionProtocols, "Hls") > -1) { parameters.EnvelopeEncryption.EnabledProtocols.Hls = true; } if (Array.IndexOf(envelopeEncryptionProtocols, "SmoothStreaming") > -1) { parameters.EnvelopeEncryption.EnabledProtocols.SmoothStreaming = true; } } } } else if (mode == "advanced") { NoEncryption noEncryptionArguments = null; CommonEncryptionCbcs commonEncryptionCbcsArguments = null; CommonEncryptionCenc commonEncryptionCencArguments = null; EnvelopeEncryption envelopeEncryptionArguments = null; if (data.jsonNoEncryption != null) { noEncryptionArguments = JsonConvert.DeserializeObject <NoEncryption>(data.configNoEncryption.ToString(), jsonReaders); } if (data.jsonCommonEncryptionCbcs != null) { commonEncryptionCbcsArguments = JsonConvert.DeserializeObject <CommonEncryptionCbcs>(data.jsonCommonEncryptionCbcs.ToString(), jsonReaders); } if (data.jsonCommonEncryptionCenc != null) { commonEncryptionCencArguments = JsonConvert.DeserializeObject <CommonEncryptionCenc>(data.jsonCommonEncryptionCenc.ToString(), jsonReaders); } if (data.jsonEnvelopeEncryption != null) { envelopeEncryptionArguments = JsonConvert.DeserializeObject <EnvelopeEncryption>(data.jsonEnvelopeEncryption.ToString(), jsonReaders); } parameters.NoEncryption = noEncryptionArguments; parameters.CommonEncryptionCbcs = commonEncryptionCbcsArguments; parameters.CommonEncryptionCenc = commonEncryptionCencArguments; parameters.EnvelopeEncryption = envelopeEncryptionArguments; } parameters.Validate(); policy = client.StreamingPolicies.Create(amsconfig.ResourceGroup, amsconfig.AccountName, streamingPolicyName, parameters); } } catch (ApiErrorException e) { log.LogError($"ERROR: AMS API call failed with error code: {e.Body.Error.Code} and message: {e.Body.Error.Message}"); return(new BadRequestObjectResult("AMS API call error: " + e.Message + "\nError Code: " + e.Body.Error.Code + "\nMessage: " + e.Body.Error.Message)); } catch (Exception e) { log.LogError($"ERROR: Exception with message: {e.Message}"); return(new BadRequestObjectResult("Error: " + e.Message)); } return((ActionResult) new OkObjectResult(new { streamingPolicyName = streamingPolicyName, streamingPolicyId = policy.Id })); }
// Module defining this command // Optional custom code for this activity /// <summary> /// Returns a configured instance of System.Management.Automation.PowerShell, pre-populated with the command to run. /// </summary> /// <param name="context">The NativeActivityContext for the currently running activity.</param> /// <returns>A populated instance of Sytem.Management.Automation.PowerShell</returns> /// <remarks>The infrastructure takes responsibility for closing and disposing the PowerShell instance returned.</remarks> protected override ActivityImplementationContext GetPowerShell(NativeActivityContext context) { System.Management.Automation.PowerShell invoker = global::System.Management.Automation.PowerShell.Create(); System.Management.Automation.PowerShell targetCommand = invoker.AddCommand(PSCommandName); // Initialize the arguments if (ProxyAccessType.Expression != null) { targetCommand.AddParameter("ProxyAccessType", ProxyAccessType.Get(context)); } if (ProxyAuthentication.Expression != null) { targetCommand.AddParameter("ProxyAuthentication", ProxyAuthentication.Get(context)); } if (ProxyCredential.Expression != null) { targetCommand.AddParameter("ProxyCredential", ProxyCredential.Get(context)); } if (SkipCACheck.Expression != null) { targetCommand.AddParameter("SkipCACheck", SkipCACheck.Get(context)); } if (SkipCNCheck.Expression != null) { targetCommand.AddParameter("SkipCNCheck", SkipCNCheck.Get(context)); } if (SkipRevocationCheck.Expression != null) { targetCommand.AddParameter("SkipRevocationCheck", SkipRevocationCheck.Get(context)); } if (SPNPort.Expression != null) { targetCommand.AddParameter("SPNPort", SPNPort.Get(context)); } if (OperationTimeout.Expression != null) { targetCommand.AddParameter("OperationTimeout", OperationTimeout.Get(context)); } if (NoEncryption.Expression != null) { targetCommand.AddParameter("NoEncryption", NoEncryption.Get(context)); } if (UseUTF16.Expression != null) { targetCommand.AddParameter("UseUTF16", UseUTF16.Get(context)); } return(new ActivityImplementationContext() { PowerShellInstance = invoker }); }
public void DecodeIncomingPacket(Socket from, ref byte[] buffer, ref int length, bool ClientLocal) { #region m_Encryption != null //if (m_Encryption is GameEncryption && !ClientLocal && buffer[0] != 232) //{ // return; //} if (m_Encryption != null) { // If we're decrypting using LoginCrypt and we've already been relayed, // only decrypt a single packet using logincrypt and then disable it if (m_Encryption is LoginEncryption) { uint newSeed = ((((LoginEncryption)(m_Encryption)).Key1 + 1) ^ ((LoginEncryption)(m_Encryption)).Key2); // Swap the seed newSeed = ((newSeed >> 24) & 0xFF) | ((newSeed >> 8) & 0xFF00) | ((newSeed << 8) & 0xFF0000) | ((newSeed << 24) & 0xFF000000); // XOR it with the old seed newSeed ^= m_Seed; IClientEncryption newEncryption = new GameEncryption(newSeed); // Game Encryption comes first newEncryption.clientDecrypt(ref buffer, length); // The login encryption is still used for this one packet m_Encryption.clientDecrypt(ref buffer, length); // Swap the encryption schemes Encryption = newEncryption; m_Seed = newSeed; return; } m_Encryption.clientDecrypt(ref buffer, length); return; } #endregion #region Port Scan //11JUN2008 RunUO SVN fix ** START *** // If the client did not connect on the game server port, // it's not our business to handle encryption for it //if (((IPEndPoint)from.Socket.LocalEndPoint).Port != Listener.Port) //{ // m_Encryption = new NoEncryption(); // return; //} bool handle = false; for (int i = 0; i < Listener.EndPoints.Length; i++) { IPEndPoint ipep = (IPEndPoint)Listener.EndPoints[i]; if (((IPEndPoint)from.LocalEndPoint).Port == ipep.Port) { handle = true; } } if (!handle) { Encryption = new NoEncryption(); return; } //11JUN2008 RunUO SVN fix ** END *** #endregion #region !m_Seeded // For simplicities sake, enqueue what we just received as long as we're not initialized m_Buffer.Enqueue(buffer, 0, length); // Clear the array length = 0; // If we didn't receive the seed yet, queue data until we can read the seed //if (!m_Seeded) //{ // // Now check if we have at least 4 bytes to get the seed // if (m_Buffer.Length >= 4) // { // byte[] m_Peek = new byte[m_Buffer.Length]; // m_Buffer.Dequeue( m_Peek, 0, m_Buffer.Length ); // Dequeue everything // m_Seed = (uint)((m_Peek[0] << 24) | (m_Peek[1] << 16) | (m_Peek[2] << 8) | m_Peek[3]); // m_Seeded = true; // Buffer.BlockCopy(m_Peek, 0, buffer, 0, 4); // length = 4; // } // else // { // return; // } //} //http://uodev.de/viewtopic.php?t=5097&postdays=0&postorder=asc&start=15&sid=dfb8e6c73b9e3eb95c1634ca3586e8a7 //if (!m_Seeded) //{ // int seed_length = m_Buffer.GetSeedLength(); // if (m_Buffer.Length >= seed_length) // { // byte[] m_Peek = new byte[m_Buffer.Length]; // m_Buffer.Dequeue(m_Peek, 0, seed_length); // if (seed_length == 4) // m_Seed = (uint)((m_Peek[0] << 24) | (m_Peek[1] << 16) | (m_Peek[2] << 8) | m_Peek[3]); // else if (seed_length == 21) // m_Seed = (uint)((m_Peek[1] << 24) | (m_Peek[2] << 16) | (m_Peek[3] << 8) | m_Peek[4]); // m_Seeded = true; // Buffer.BlockCopy(m_Peek, 0, buffer, 0, seed_length); // length = seed_length; // } // else // { // return; // } //} //11JUN2008 My Version if (!m_Seeded) { if (m_Buffer.Length <= 3) //Short Length, try again. { Console.WriteLine("Encryption: Failed - Short Lenght"); return; } //else if ((m_Buffer.Length == 83) && (m_Buffer.GetPacketID() == 239)) //New Client //{ // byte[] m_Peek = new byte[21]; // m_Buffer.Dequeue(m_Peek, 0, 21); // m_Seed = (uint)((m_Peek[1] << 24) | (m_Peek[2] << 16) | (m_Peek[3] << 8) | m_Peek[4]); // m_Seeded = true; // Buffer.BlockCopy(m_Peek, 0, buffer, 0, 21); // length = 21; // Console.WriteLine("Encryption: Passed - New Client"); //} //05MAR2009 Smjert's fix for double log in. *** START *** else if ((m_Buffer.Length == 83 || m_Buffer.Length == 21) && (m_Buffer.GetPacketID() == 239)) //New Client { length = m_Buffer.Length; byte[] m_Peek = new byte[21]; m_Buffer.Dequeue(m_Peek, 0, 21); m_Seed = (uint)((m_Peek[1] << 24) | (m_Peek[2] << 16) | (m_Peek[3] << 8) | m_Peek[4]); m_Seeded = true; Buffer.BlockCopy(m_Peek, 0, buffer, 0, 21); Console.WriteLine("Encryption: Passed - New Client"); // We need to wait the next packet if (length == 21) { return; } length = 21; } else if (m_Buffer.Length >= 4) //Old Client //05MAR2009 Smjert's fix for double log in. *** END *** { byte[] m_Peek = new byte[4]; m_Buffer.Dequeue(m_Peek, 0, 4); m_Seed = (uint)((m_Peek[0] << 24) | (m_Peek[1] << 16) | (m_Peek[2] << 8) | m_Peek[3]); m_Seeded = true; Buffer.BlockCopy(m_Peek, 0, buffer, 0, 4); length = 4; Console.WriteLine("Encryption: Passed - Old Client"); } else //It should never reach here. { Console.WriteLine("Encryption: Failed - It should never reach here"); return; } } #endregion // If the context isn't initialized yet, that means we haven't decided on an encryption method yet #region m_Encryption == null if (m_Encryption == null) { int packetLength = m_Buffer.Length; int packetOffset = length; m_Buffer.Dequeue(buffer, length, packetLength); // Dequeue everything length += packetLength; // This is special handling for the "special" UOG packet if (packetLength >= 3) { if (buffer[packetOffset] == 0xf1 && buffer[packetOffset + 1] == ((packetLength >> 8) & 0xFF) && buffer[packetOffset + 2] == (packetLength & 0xFF)) { Encryption = new NoEncryption(); return; } } // Check if the current buffer contains a valid login packet (62 byte + 4 byte header) // Please note that the client sends these in two chunks. One 4 byte and one 62 byte. if (packetLength == 62) { Console.WriteLine("Checking packetLength 62 == " + packetLength); // Check certain indices in the array to see if the given data is unencrypted if (buffer[packetOffset] == 0x80 && buffer[packetOffset + 30] == 0x00 && buffer[packetOffset + 60] == 0x00) { if (Configuration.AllowUnencryptedClients) { Encryption = new NoEncryption(); } } else { LoginEncryption encryption = new LoginEncryption(); if (encryption.init(m_Seed, buffer, packetOffset, packetLength)) { Console.WriteLine("Client: {0}: Encrypted client detected, using keys of client {1}", "asd", encryption.Name); Encryption = encryption; Console.WriteLine("Encryption: Check 1"); byte[] packet = new byte[packetLength]; Console.WriteLine("Encryption: Check 2"); Buffer.BlockCopy(buffer, packetOffset, packet, 0, packetLength); Console.WriteLine("Encryption: Check 3"); encryption.clientDecrypt(ref packet, packet.Length); Console.WriteLine("Encryption: Check 4"); Buffer.BlockCopy(packet, 0, buffer, packetOffset, packetLength); Console.WriteLine("Encryption: Check 5"); //return; //Just throwing this in. } else { Console.WriteLine("Detected an unknown client."); } } } else if (packetLength == 65) { Console.WriteLine("Checking packetLength 65 == " + packetLength); // If its unencrypted, use the NoEncryption class if (buffer[packetOffset] == '\x91' && buffer[packetOffset + 1] == ((m_Seed >> 24) & 0xFF) && buffer[packetOffset + 2] == ((m_Seed >> 16) & 0xFF) && buffer[packetOffset + 3] == ((m_Seed >> 8) & 0xFF) && buffer[packetOffset + 4] == (m_Seed & 0xFF)) { if (Configuration.AllowUnencryptedClients) { Encryption = new NoEncryption(); } } else { // If it's not an unencrypted packet, simply assume it's encrypted with the seed Encryption = new GameEncryption(m_Seed); byte[] packet = new byte[packetLength]; Buffer.BlockCopy(buffer, packetOffset, packet, 0, packetLength); m_Encryption.clientDecrypt(ref packet, packet.Length); Buffer.BlockCopy(packet, 0, buffer, packetOffset, packetLength); } } // If it's still not initialized, copy the data back to the queue and wait for more if (m_Encryption == null) { Console.WriteLine("Encryption: Check - Waiting"); m_Buffer.Enqueue(buffer, packetOffset, packetLength); length -= packetLength; return; } } #endregion }