วันพฤหัสบดีที่ 20 กุมภาพันธ์ พ.ศ. 2557

Encode decode Url

step 1.
using System.Security.Cryptography;
using System.IO;
step 2.
create class
public class EncryptDecryptQueryString
{
    private byte[] key = { };
    private byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef };
    public string Decrypt(string stringToDecrypt, string sEncryptionKey)
    {
        byte[] inputByteArray = new byte[stringToDecrypt.Length + 1];
        try
        {
            key = System.Text.Encoding.UTF8.GetBytes(sEncryptionKey);
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            inputByteArray = Convert.FromBase64String(stringToDecrypt);
            MemoryStream ms = new MemoryStream();
            CryptoStream cs = new CryptoStream(ms,
              des.CreateDecryptor(key, IV), CryptoStreamMode.Write);
            cs.Write(inputByteArray, 0, inputByteArray.Length);
            cs.FlushFinalBlock();
            System.Text.Encoding encoding = System.Text.Encoding.UTF8;
            return encoding.GetString(ms.ToArray());
        }
        catch (Exception e)
        {
            return e.Message;
        }
    }

    public string Encrypt(string stringToEncrypt, string SEncryptionKey)
    {
        try
        {
            key = System.Text.Encoding.UTF8.GetBytes(SEncryptionKey);
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            byte[] inputByteArray = Encoding.UTF8.GetBytes(stringToEncrypt);
            MemoryStream ms = new MemoryStream();
            CryptoStream cs = new CryptoStream(ms,
              des.CreateEncryptor(key, IV), CryptoStreamMode.Write);
            cs.Write(inputByteArray, 0, inputByteArray.Length);
            cs.FlushFinalBlock();
            return Convert.ToBase64String(ms.ToArray());
        }
        catch (Exception e)
        {
            return e.Message;
        }
    }
}

step 3.
Page 1

if (HttpContext.Current != null)
                                {
                                    string strURLWithData = "Backoffice.aspx?" + EncryptQueryString(string.Format("Typeuser={0}", strtype));
                                    HttpContext.Current.Response.Redirect(strURLWithData);
                                }
                                else
                                {
                                }

public string EncryptQueryString(string strQueryString)
        {
            EncryptDecryptQueryString objEDQueryString = new EncryptDecryptQueryString();
            return objEDQueryString.Encrypt(strQueryString, "r0b1nr0y");
        }

step 4.
Page 2.

if (!IsPostBack)
            {
                string strReq = "";
                strReq = Request.RawUrl;
                strReq = strReq.Substring(strReq.IndexOf('?') + 1);

                if (!strReq.Equals(""))
                {
                    strReq = DecryptQueryString(strReq);

                    //Parse the value... this is done is very raw format..
                    //you can add loops or so to get the values out of the query string...
                    string[] arrMsgs = strReq.Split('&');
                    string[] arrIndMsg;
                    string strtype = "";
                    arrIndMsg = arrMsgs[0].Split('='); //Get the Name
                    strtype = arrIndMsg[1].ToString().Trim();
                    if (strtype == "Admin")
                    {
                        Label1.Visible = true;
                    }
                    else if (strtype == "User")
                    {
                        Label1.Visible = false;
                    }
                }
                else
                {
                    Response.Redirect("Page1.aspx");
                }

end

ไม่มีความคิดเห็น:

แสดงความคิดเห็น