I am using a below encryption technique to encryot a query string value in MVC application.
So the url will be something like controller/details/rlSRlRZwzbU%3d
But when param =2
and KEY=mcbgp23njwmcinu0tij05vc2
this function returns UhMZc7/MNK8=
.
So the formatted url will be controller/details/UhMZc7/MNK8%3d
and giving 404 Error
public static string Encrypt(string dataStr)
{
Byte[] inputByteArray = Encoding.ASCII.GetBytes(dataStr);
var mstr = new MemoryStream();
string KEY = HttpContext.Current.Session.SessionID;
var key = Encoding.ASCII.GetBytes(KEY.Substring(0, 8));
var des = new DESCryptoServiceProvider();
var cstr = new CryptoStream(mstr, des.CreateEncryptor(key, key), CryptoStreamMode.Write);
cstr.Write(inputByteArray, 0, inputByteArray.Length);
cstr.FlushFinalBlock();
return Convert.ToBase64String(ms.ToArray());
}
How can I make this function to be MVC URL safe? Is there any other good technique to produce the url-safe encrypted format?
Edit:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}