SQL2005中部署.net 3DES加密函数
2007-11-11 12:51:14 来源:WEB开发网SQL2005支持在sql server(WINDOWS平台上强大的数据库平台)里部署.net应用程序了,那么以前一直写在.net里的一些运算如加密等已经完全可以移到sql里来进行了,而不需要再从程序中运算了,使数据库可以和程序相对独立开来,方便了不少。
前阵子,需要给数据加密,所以写了个3DES加密函数部署到SQL2005中,当然,现在秘钥也作为一个表存在在数据库中,至于这个密钥,以后可以考虑用usb-key使用rsa加密或者别的方式进行加密,以保证数据安全。现在先说怎么实现部署到sqlserver中的加密函数吧。
新建一个项目。VB中的database-->sql server(WINDOWS平台上强大的数据库平台) 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
<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()
Return Convert.ToBase64String(ms.ToArray())
Catch e As Exception
Return CType(e.Message, SqlString)
End Try
End Function
<Microsoft.SqlServer.Server.SqlFunction()> _
Public Shared Function DecryptFromBase64String()Function DecryptFromBase64String(ByVal stringToDecryptsql As SqlString, ByVal sEncryptionKeysql As SqlString) As SqlString
Dim stringToDecrypt As String = CType(stringToDecryptsql, String)
Dim sEncryptionKey As String = CType(sEncryptionKeysql, String)
Dim inputByteArray(stringToDecrypt.Length) As Byte
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()
inputByteArray = Convert.FromBase64String(stringToDecrypt)
Dim ms As New MemoryStream()
Dim cs As New CryptoStream(ms, des.CreateDecryptor(key, IV), CryptoStreamMode.Write)
cs.Write(inputByteArray, 0, inputByteArray.Length)
cs.FlushFinalBlock()
Dim encoding As System.Text.Encoding = System.Text.Encoding.UTF8
Return encoding.GetString(ms.ToArray())
Catch e As Exception
Return CType(e.Message, SqlString)
End Try
End Function
End Class
完成之后选择生成,部署即可
至于测试,是否可用,打开查询分析器,执行下面语句,
SELECT dbo.DecryptFromBase64String(dbo.EncryptToBase64String('123456','12345678'),'12345678')
如果出现正确的值:123456,说明成功了
- ››SQL Server 2008 R2 下如何清理数据库日志文件
- ››sqlite 存取中文的解决方法
- ››SQL2005、2008、2000 清空删除日志
- ››SQL Server 2005和SQL Server 2000数据的相互导入...
- ››sql server 2008 在安装了活动目录以后无法启动服...
- ››sqlserver 每30分自动生成一次
- ››sqlite 数据库 对 BOOL型 数据的插入处理正确用法...
- ››sql server自动生成批量执行SQL脚本的批处理
- ››sql server 2008亿万数据性能优化
- ››SQL Server 2008清空数据库日志方法
- ››sqlserver安装和简单的使用
- ››SQL Sever 2008 R2 数据库管理
更多精彩
赞助商链接