ASP. NET happy website series-Ingenious encryption of customer's unique link in the content of e-journals (Enewsletter)

Source: Internet
Author: User

The title is a bit difficult. Please forgive me. Here I will explain...

Background:

The company often sends some Enewsletter, which contains some specific links. When a user clicks the link in the email, the customer will be taken to different pages based on the user's information, some customers are asked to fill out their forms, which have their basic information in advance at the beginning.

The customer may be from the original registered users of the company site, or from the company's information collected from other places. -- In addition, This is different from the general spam, but an invitation letter for a specific group.

Problem:

The faster the request is, the better. Therefore, the customer's email is placed directly in the Link. But how can we prevent customers or others from seeing or modifying it?

Implementation:

Ha! Fortunately, asp.net has a country called System. Security. Cryptography.

A group dedicated to encryption and decryption within this country...

Create a class for this requirement:

 public static class SimpleSecurity{    private const string MyKey = "uptoyou";    static public string Decrypt(string myString, bool urlDecode)    {        TripleDESCryptoServiceProvider cryptDES3 = new TripleDESCryptoServiceProvider();        MD5CryptoServiceProvider cryptMD5Hash = new MD5CryptoServiceProvider();        cryptDES3.Key = cryptMD5Hash.ComputeHash(ASCIIEncoding.ASCII.GetBytes(MyKey));        cryptDES3.Mode = CipherMode.ECB;        ICryptoTransform desdencrypt = cryptDES3.CreateDecryptor();        byte[] buff = Convert.FromBase64String(myString);        return ASCIIEncoding.ASCII.GetString(desdencrypt.TransformFinalBlock(buff, 0, buff.Length));    }    static public string Encrypt(string myString)    {        TripleDESCryptoServiceProvider cryptDES3 = new TripleDESCryptoServiceProvider();        MD5CryptoServiceProvider cryptMD5Hash = new MD5CryptoServiceProvider();        cryptDES3.Key = cryptMD5Hash.ComputeHash(ASCIIEncoding.ASCII.GetBytes(MyKey));        cryptDES3.Mode = CipherMode.ECB;        ICryptoTransform desdencrypt = cryptDES3.CreateEncryptor();        byte[] buff = ASCIIEncoding.ASCII.GetBytes(myString);        return s = Convert.ToBase64String(desdencrypt.TransformFinalBlock(buff, 0, buff.Length));    }}

To Encrypt customers' emails and generate the desired url address, you can:

 SimpleSecurity.Encrypt(email);
 

The generated string may be similar to: 6OpKSqWe57SAHBhmJJsF7Q % 3d % 3 dccc. Because the Key is in your hand, it is very unlikely that others will decrypt it.

Next, if you want to decrypt the email from the address requested by the customer page, you only need:

 if (Request.QueryString["u"] != null)    email = SimpleSecurity.Decrypt(Request.QueryString["u"]);

It is simple and safe. I hope you can reply to the post for discussion and follow up. You are welcome to come up with different ideas.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.