关于url加密设置

本文介绍了如何在项目中为URL加密以隐藏第三方地址。通过提供的`Encode`和`Decrypt`方法,利用DESCryptoServiceProvider进行加密和解密操作,确保URL参数的安全。示例代码展示了如何使用这些方法进行字符串的加密和解密。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 最近项目中又一个功能是嵌套第三方地址,于是乎就弄了一个https域名,又加了一个ifram嵌套,但是问题出现了,

url跳转的时候在url地址栏还是会出现第三方的url地址,即便是作为参数传递的,但是也是不允许的,那么问题来了,

 如何给url加密看不出来是什么呢,下面encodeurl来了,

 public static string Encode(string str, string key)
    {


        DESCryptoServiceProvider provider = new DESCryptoServiceProvider();


        provider.Key = Encoding.ASCII.GetBytes(key.Substring(0, 8));


        provider.IV = Encoding.ASCII.GetBytes(key.Substring(0, 8));


        byte[] bytes = Encoding.UTF8.GetBytes(str);


        MemoryStream stream = new MemoryStream();


        CryptoStream stream2 = new CryptoStream(stream, provider.CreateEncryptor(), CryptoStreamMode.Write);


        stream2.Write(bytes, 0, bytes.Length);


        stream2.FlushFinalBlock();


        StringBuilder builder = new StringBuilder();


        foreach (byte num in stream.ToArray())
        {


            builder.AppendFormat("{0:X2}", num);


        }


        stream.Close();


        return builder.ToString();


    }

多说无益,直接调用

 string posturl = Encode(posturl.Trim(), "Rainight").Trim();

收工。

下面是解密:

 public string Decrypt(string pToDecrypt, string sKey)
    {


        DESCryptoServiceProvider des = new DESCryptoServiceProvider();






        //Put  the  input  string  into  the  byte  array     


        byte[] inputByteArray = new byte[pToDecrypt.Length / 2];


        for (int x = 0; x < pToDecrypt.Length / 2; x++)
        {


            int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16));


            inputByteArray[x] = (byte)i;


        }






        //建立加密对象的密钥和偏移量,此值重要,不能修改     


        des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);


        des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);


        MemoryStream ms = new MemoryStream();


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


        //Flush  the  data  through  the  crypto  stream  into  the  memory  stream     


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


        cs.FlushFinalBlock();






        //Get  the  decrypted  data  back  from  the  memory  stream     


        //建立StringBuild对象,CreateDecrypt使用的是流对象,必须把解密后的文本变成流对象     


        StringBuilder ret = new StringBuilder();






        return System.Text.Encoding.Default.GetString(ms.ToArray());


    }     



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值