Deploy. net 3DES encryption function in SQL2005

Source: Internet
Author: User
SQL2005 supports deployment in SQL SERVER.. net application. some operations such as encryption in. net can be completely moved to SQL, without the need to run the program, so that the database can be relatively independent from the program, making it much easier.
A while ago, we needed to encrypt the data, so we wrote a 3DES encryption function to deploy it in SQL2005. Of course, the key also exists as a table in the database. As for this key, in the future, you can use the usb-key to encrypt data using rsa or other methods to ensure data security. Now let's talk about how to implement the encryption function deployed in sqlserver.
Create a project. Database --> SQL Server Project in VB. After I create a Project called DESCryptoService, I will ask you to add database references and select the database to be deployed.
Write the following code.
Imports System
Imports System. Data
Imports System. Data. SqlClient
Imports System. Data. SqlTypes
Imports Microsoft. SqlServer. Server
Imports System. IO
Imports System. Xml
Imports System. Text
Imports System. Security. Cryptography
Partial Public Class EncryptToBase64DecryptFromBase64Class EncryptToBase64DecryptFromBase64
<Microsoft. SqlServer. Server. SqlFunction ()> _
Public Shared Function EncryptToBase64String () Function EncryptToBase64String (ByVal stringToEncryptsql As SqlString, ByVal SEncryptionKeysql As SqlString) As SqlString
Dim stringToEncrypt As String = CType (stringToEncryptsql, String)
Dim SEncryptionKey As String = CType (SEncryptionKeysql, String)
Dim IV () As Byte = {& H12, & H34, & H56, & H78, & H90, & HAB, & HCD, & HEF}
Dim key () As Byte = {}
Try
Key = System. Text. Encoding. UTF8.GetBytes (Left (SEncryptionKey, 8 ))
Dim des As New DESCryptoServiceProvider ()
Dim inputByteArray () As Byte = Encoding. UTF8.GetBytes (stringToEncrypt)
Dim MS As New MemoryStream ()
Dim cs As New CryptoStream (MS, des. CreateEncryptor (key, IV), CryptoStreamMode. Write)
Cs. Write (inputByteArray, 0, inputByteArray. Length)
Cs. FlushFinalBlock ()

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.