SQL2005 supports the deployment of .net applications in SQL SERVER, so some operations such as encryption that have been written in .net can be completely moved to sql, without the need to perform calculations from the program, making the database It can be relatively independent from the program, which is a lot more convenient.
A while ago, I needed to encrypt data, so I wrote a 3DES encryption function and deployed it in SQL2005. Of course, the secret key now also exists as a table in the database. As for this key, you can consider using usb-key in the future. rsa encryption or other encryption methods to ensure data security. Now let’s talk about how to implement the encryption function deployed in sqlserver.
Create a new project. Database in VB-->SQL Server Project, I created a new project called DESCryptoService. After that, you will be asked to add a database reference and select the database you want to deploy to.
Write the following code and it will be OK
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
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()