SQL2005中部署.net 3DES加密函数_PHP教程
程序员文章站
2022-06-01 15:25:16
...
SQL2005支持在SQL SERVER里部署.net应用程序了,那么以前一直写在.net里的一些运算如加密等已经完全可以移到sql里来进行了,而不需要再从程序中运算了,使数据库可以和程序相对独立开来,方便了不少。
前阵子,需要给数据加密,所以写了个3DES加密函数部署到SQL2005中,当然,现在秘钥也作为一个表存在在数据库中,至于这个密钥,以后可以考虑用usb-key使用rsa加密或者别的方式进行加密,以保证数据安全。现在先说怎么实现部署到sqlserver中的加密函数吧。
新建一个项目。VB中的database-->SQL Server Project,我新建了以后叫做DESCryptoService的项目,之后,会让你添加数据库引用,选择你要部署到的数据库。
写入如下代码,就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()
前阵子,需要给数据加密,所以写了个3DES加密函数部署到SQL2005中,当然,现在秘钥也作为一个表存在在数据库中,至于这个密钥,以后可以考虑用usb-key使用rsa加密或者别的方式进行加密,以保证数据安全。现在先说怎么实现部署到sqlserver中的加密函数吧。
新建一个项目。VB中的database-->SQL Server Project,我新建了以后叫做DESCryptoService的项目,之后,会让你添加数据库引用,选择你要部署到的数据库。
写入如下代码,就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()