Deploy C # program DES encryption and decryption on SQL Server

Source: Internet
Author: User

First enable the SQL server2005 Service

 

 

 

 

Create a project:

 

Select the database to be deployed

 

Add a class

 

 

/*************************************** ***************************
* Copyright (c)
* Description: data encryption and decryption
* Createdate: 2009-05-22
* Creater: Oliver. Dong
* Lastchangedate:
* Lastchanger:
* Version info: 1.0
**************************************** ****************************/

Using system;
Using system. Collections. Generic;
Using system. text;
Using system. Security. cryptography;
Using system. IO;

 

Public partial class desencrypt
{
/// <Summary>
/// Encryption
/// </Summary>
/// <Param name = "data"> string to be encrypted </param>
/// <Returns> encrypted string </returns>
[Microsoft. sqlserver. server. sqlfunction ()]
Public static string encode (string data, string key_64, string iv_64)
{
Byte [] bykey = system. Text. asciiencoding. ASCII. getbytes (key_64 );
Byte [] byiv = system. Text. asciiencoding. ASCII. getbytes (iv_64 );

Descryptoserviceprovider cryptoprovider = new descryptoserviceprovider ();
Int I = cryptoprovider. keysize;
Memorystream MS = new memorystream ();
Cryptostream CST = new cryptostream (MS, cryptoprovider. createencryptor (bykey, byiv), cryptostreammode. Write );

Streamwriter Sw = new streamwriter (Cst );
Sw. Write (data );
Sw. Flush ();
CST. flushfinalblock ();
Sw. Flush ();
Return convert. tobase64string (Ms. getbuffer (), 0, (INT) ms. Length );
}

/// <Summary>
/// Decrypt
/// </Summary>
/// <Param name = "data"> string to be decrypted </param>
/// <Returns> decrypted string </returns>
[Microsoft. sqlserver. server. sqlfunction ()]
Public static string decode (string data, string key_64, string iv_64)
{
If (data. Length <= 0)
{
Return "";
}

Byte [] bykey = system. Text. asciiencoding. ASCII. getbytes (key_64 );
Byte [] byiv = system. Text. asciiencoding. ASCII. getbytes (iv_64 );
Byte [] byenc;
Try
{
Byenc = convert. frombase64string (data );
}
Catch
{
Return NULL;
}
Descryptoserviceprovider cryptoprovider = new descryptoserviceprovider ();
Memorystream MS = new memorystream (byenc );
Cryptostream CST = new cryptostream (MS, cryptoprovider. createdecryptor (bykey, byiv), cryptostreammode. Read );
Streamreader sr = new streamreader (Cst );
Return Sr. readtoend ();
}
}

 

 

SQL call:

Select DBO. encode (UID, 'aaaaaaa', 'bbbbbbbbbbbb') from tb_users TU

 

 

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.