diff --git a/src/Umbraco.Core/StringExtensions.cs b/src/Umbraco.Core/StringExtensions.cs index 3250a21d24..16fea11539 100644 --- a/src/Umbraco.Core/StringExtensions.cs +++ b/src/Umbraco.Core/StringExtensions.cs @@ -22,23 +22,60 @@ namespace Umbraco.Core /// /// Encrypt the string using the MachineKey in medium trust /// - /// + /// The string value to be encrypted. /// - public static string EncryptWithMachineKey(this string toEncrypt) + public static string EncryptWithMachineKey(this string value) { - var output = FormsAuthentication.Encrypt(new FormsAuthenticationTicket(0, "temp", DateTime.Now, DateTime.MaxValue, false, toEncrypt)); - return output; + if (value == null) + return null; + + string valueToEncrypt = value; + List parts = new List(); + + const int EncrpytBlockSize = 500; + + while (valueToEncrypt.Length > EncrpytBlockSize) + { + parts.Add(valueToEncrypt.Substring(0, EncrpytBlockSize)); + valueToEncrypt = valueToEncrypt.Remove(0, EncrpytBlockSize); + } + + if (valueToEncrypt.Length > 0) + { + parts.Add(valueToEncrypt); + } + + StringBuilder encrpytedValue = new StringBuilder(); + + foreach (var part in parts) + { + var encrpytedBlock = FormsAuthentication.Encrypt(new FormsAuthenticationTicket(1, string.Empty, DateTime.Now, DateTime.Now, false, part)); + encrpytedValue.AppendLine(encrpytedBlock); + } + + return encrpytedValue.ToString().TrimEnd(); } /// /// Decrypt the encrypted string using the Machine key in medium trust /// - /// + /// The string value to be decrypted /// - public static string DecryptWithMachineKey(this string encrypted) + public static string DecryptWithMachineKey(this string value) { - var output = FormsAuthentication.Decrypt(encrypted); - return output.UserData; + if (value == null) + return null; + + string[] parts = value.Split('\n'); + + StringBuilder decryptedValue = new StringBuilder(); + + foreach (var part in parts) + { + decryptedValue.Append(FormsAuthentication.Decrypt(part.TrimEnd()).UserData); + } + + return decryptedValue.ToString(); } //this is from SqlMetal and just makes it a bit of fun to allow pluralisation diff --git a/src/Umbraco.Web/HtmlHelperRenderExtensions.cs b/src/Umbraco.Web/HtmlHelperRenderExtensions.cs index cec5267497..f64282d73f 100644 --- a/src/Umbraco.Web/HtmlHelperRenderExtensions.cs +++ b/src/Umbraco.Web/HtmlHelperRenderExtensions.cs @@ -119,7 +119,7 @@ namespace Umbraco.Web if (!string.IsNullOrWhiteSpace(surfaceRouteParams)) { - _base64String = Convert.ToBase64String(Encoding.UTF8.GetBytes(surfaceRouteParams)); + _encryptedString = surfaceRouteParams.EncryptWithMachineKey(); } _textWriter = viewContext.Writer; @@ -127,7 +127,7 @@ namespace Umbraco.Web private bool _disposed; - private readonly string _base64String; + private readonly string _encryptedString; private readonly TextWriter _textWriter; protected override void Dispose(bool disposing) @@ -137,7 +137,7 @@ namespace Umbraco.Web this._disposed = true; //write out the hidden surface form routes - _textWriter.Write(""); + _textWriter.Write(""); base.Dispose(disposing); } diff --git a/src/Umbraco.Web/Mvc/RenderRouteHandler.cs b/src/Umbraco.Web/Mvc/RenderRouteHandler.cs index 73a0916c36..fab342df7b 100644 --- a/src/Umbraco.Web/Mvc/RenderRouteHandler.cs +++ b/src/Umbraco.Web/Mvc/RenderRouteHandler.cs @@ -115,8 +115,8 @@ namespace Umbraco.Web.Mvc return null; var encodedVal = requestContext.HttpContext.Request["uformpostroutevals"]; - var decodedString = Encoding.UTF8.GetString(Convert.FromBase64String(encodedVal)); - var parsedQueryString = HttpUtility.ParseQueryString(decodedString); + var decryptedString = encodedVal.DecryptWithMachineKey(); + var parsedQueryString = HttpUtility.ParseQueryString(decryptedString); var decodedParts = new Dictionary();