.Net 網頁信箱 防止垃圾郵件 .Net 網頁信箱 防止垃圾郵件
  .NET       ez      2012-06-04

網頁中常常會出現明碼的信箱位址,時常會被機器人爬取,做為廣告發送目標。

本篇我利用 .Net 產生 JavaScript,防止機器人直接抓取原始碼中的電子信箱位址。

using System;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Text;

namespace Test
{
    public partial class Test : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Write(MailToJS("[email protected]"));
        }

        /// <summary>
        /// 產生指定範圍的亂數
        /// </summary>
        /// <param name="Min"></param>
        /// <param name="Max"></param>
        /// <returns></returns>
        public int RandomNext(int Min, int Max)
        {
            RNGCryptoServiceProvider rngp = new RNGCryptoServiceProvider();
            byte[] rb = new byte[4];

            rngp.GetBytes(rb);
            int value = BitConverter.ToInt32(rb, 0);
            value = (value % (Max - Min + 1));
            if (value < 0) value = -value;
            value += Min;
            return value;
        }

        /// <summary>
        /// 將信箱轉為JS
        /// </summary>
        /// <param name="Mail"></param>
        /// <returns></returns>
        protected string MailToJS(string Mail)
        {
            List<string> CList = new List<string>();
            for (int i = 32; i <= 64; i++) CList.Add(Convert.ToChar(i).ToString());
            for (int i = 91; i <= 126; i++) CList.Add(Convert.ToChar(i).ToString());
            List<string> NList = new List<string>();
            while (CList.Count > 0)
            {
                int R = RandomNext(0, CList.Count - 1);
                NList.Add(CList[R]);
                CList.RemoveAt(R);
            }

            List<int> EList = new List<int>();
            Mail = Mail.ToLower();
            Mail = "<a href=\"mailto:" + Mail + "\" hidefocus=\"true\">" + Mail + "</a>";
            foreach (char c in Mail) EList.Add(NList.IndexOf(c.ToString()));

            for (int i = 0; i < NList.Count; i++)
            {
                if (NList[i] == "'")
                    NList[i] = "'" + NList[i].Replace("'", "\\'") + "'";
                else if (NList[i] == "\\")
                    NList[i] = "'" + NList[i].Replace("\\", "\\\\") + "'";
                else
                    NList[i] = "'" + NList[i] + "'";
            }

            StringBuilder SB = new StringBuilder();
            SB.AppendLine("<script type=\"text/javascript\">");
            SB.AppendLine("<!--");
            SB.AppendLine("\t(function(){");
            SB.AppendLine("\t\tvar ek = [" + String.Join(",", NList.ToArray()) + "];");
            SB.AppendLine("\t\tvar en = [" + String.Join(",", EList.ToArray()) + "];");
            SB.AppendLine("\t\tvar ds = \"\";");
            SB.AppendLine("\t\tfor(var i in en) ds += ek[en[i]];");
            SB.AppendLine("\t\tdocument.write(ds);");
            SB.AppendLine("\t})();");
            SB.AppendLine("// -->");
            SB.AppendLine("</script>");

            return SB.ToString();
        }
    }
}

結果截圖:


標籤:   .NET

我要留言