Illustration C # create an SQL Server MD5 encryption function

Source: Internet
Author: User

 

The procedure is as follows:

1. Create a project:

 

2. The following prompt is displayed: click OK.

 

 

3. The database to be deployed appears.

 

 

4. Select Yes

 

5. Write the following code:

Public partial class userdefinedfunctions {[Microsoft. sqlserver. server. sqlfunction] public static sqlstring md5function (string source) {// place the code return md5string (source) here );} /// <summary> /// use MD5 encryption /// </Summary> /// <Param name = "Source"> string to be encrypted </param> // /<returns> return encrypted ciphertext </returns> Public static string md5string (string source) {try {byte [] DATA = system. text. encoding. getencoding (1, 1252 ). getbytes (source); Data = new md5cryptoserviceprovider (). computehash (data); string returnvalue = ""; for (INT I = 0; I <data. length; I ++)/* tostring ("X2") is the string format control character in C #. X is hexadecimal. 2 indicates that each time it is two digits, for example, 0x0a. If there is no 2, only 0 XA is output. Assume that there are two numbers: 10 and 26. Normally, 0xa and 0 x 1A are displayed in hexadecimal notation. This will make it look untidy. To look good, you can specify "X2 ", the result is: 0x0a, 0x1a */returnvalue + = data [I]. tostring ("X2"); Return returnvalue;} catch (systemexception ex) {Throw ex ;}}};

 

 

6. Compile and deploy the framework (note: the framework must be reduced to 3.5 to be deployed successfully)

7. The database executes the following statements:

exec sp_configure 'show advanced options', '1';goRECONFIGURE;goexec sp_configure 'clr enabled', '1'goRECONFIGURE;exec sp_configure 'show advanced options', '1';goRECONFIGURE;

8. Execute the newly created function:

 

 

Related Article

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.