日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

asp.net URL參數(shù)加密解密的問(wèn)題

 mybook564 2014-09-15


  最近我遇到一個(gè)關(guān)于asp.net
URL參數(shù)加密解密的問(wèn)題,在加密時(shí)沒(méi)問(wèn)題,可是解密就有問(wèn)題了,有些參數(shù)解密出來(lái)是空(并不是原來(lái)的數(shù)據(jù))。下面是一個(gè)需要加密URL參數(shù)的地
址:http://www.px?username=marry&password=marry

  在index.aspx頁(yè)添加的代碼:

        private static byte[] key = { };

        private static byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };

        private static string EncryptionKey = "!5623a#de"; 

        public static string Encrypt(string Input)

        {

            try

            {

                key = System.Text.Encoding.UTF8.GetBytes(EncryptionKey.Substring(0, 8));

                DESCryptoServiceProvider des = new DESCryptoServiceProvider();

                Byte[] inputByteArray = Encoding.UTF8.GetBytes(Input);

                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 ex)

            {

                return "";

            }

        }

        Response.Redirect(“http://www.px?username=” + Encrypt(marry) + "&password=" Encrypt(marry

));



  在px網(wǎng)頁(yè)需要的代碼:

        private static byte[] key = { };

        private static byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };

        private static string EncryptionKey = "!5623a#de";


       public static string Decrypt(string Input)

        {

            if (!string.IsNullOrEmpty(Input))

            {

                Input = Input.Replace(" ", "+");


                Byte[] inputByteArray = new Byte[Input.Length];

                try

                {

                    key = System.Text.Encoding.UTF8.GetBytes(EncryptionKey.Substring(0, 8));

                    DESCryptoServiceProvider des = new DESCryptoServiceProvider();

                    inputByteArray = Convert.FromBase64String(Input);

                    MemoryStream ms = new MemoryStream();

                    CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(key, IV), CryptoStreamMode.Write);

                    cs.Write(inputByteArray, 0, inputByteArray.Length);

                    cs.FlushFinalBlock();


                    Encoding encoding = Encoding.UTF8;

                    return encoding.GetString(ms.ToArray());


                }

                catch (Exception ex)

                {

                    return "";

                }

            }

            else

            {

                return "";

            }

        }

在Page_Load事件中要添加的代碼:

            Decrypt(Request.QueryString["username"]);

            Decrypt(Request.QueryString["password"]);



總結(jié):如果去除紅色部分的代碼就會(huì)出現(xiàn)上面出現(xiàn)所說(shuō)的情況,出錯(cuò)或者解密出來(lái)的數(shù)據(jù)變成空值。


    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶(hù)發(fā)布,不代表本站觀(guān)點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買(mǎi)等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶(hù) 評(píng)論公約

    類(lèi)似文章 更多