环境为VS2008 C# ,SQL ,本人已测试通过,有人会说直接把连接串写程序里不就行了,可以不用那个连接串的,确实是这样的,不过只要你的项目中DataSet里有TableAdapter,那么App.config中的连接串就是必不可少的,所以就要加密。。。。不过话说回来用.NET做数据库的项目不用这功能有点。。。。。。
此种方法虽不是完美,但作为一般应用已经足够。
另外在CSDN上找了半天,也下了一些例子不过不是把网上文章抄一下就是基本不能用!大部分都是骗分的,这个例子我是在国外网站上找的,自己小改了一下测试通过!
1、建表,我是SQL2008,建不了自己建一个吧,就一个字段
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[tmp_ID](
[ID] [varchar](50) NOT NULL,
CONSTRAINT [PK_tmp_ID] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
2、插入数据
insert into [tmp_ID] (id) values ('AAA')
insert into [tmp_ID] (id) values ('BBB')
insert into [tmp_ID] (id) values ('CCC')
3、用VS打开项目,在App.config文件中修改连接字符串信息
4、启动项目,点击“Query”按钮,如能成功查出信息,基本就OK了。
5、验证,生成解决方案后,查看“Security01.exe.config”,如连接串已加密则成功。
6、生成解决方案后,如有“Security01.vshost.exe.config”,请如下操作:
在VS菜单中依次点击:项目,XXX属性,选“调试”,将启用Visual Studiio宿主进程选项取消选中
因为这个文件是不加密的!!!切记!!!
7、在App.config文件中请不要将加密后的信息放进去,因为加密解密是和本地机器有关,所以这样做之后,用户机器上解密会出错。
8、虽然用程序在生成时“Security01.exe.config”中的连接串是明码的,但是我们可以使用ClickOnce发布安装程序,安装结束后程序会自动运行,只要将执行加密的代码放在程序一开始就要运行的地方,就可以实现加密。如果你是把生成程序后直接把Debug子目录中的文件发给用户,那么这种方法不行,因为“Security01.exe.config”文件中连接串还是明码的,只有用户启动程序后才会被加密!用ClickOnce吧!VS不用这功能太可惜了,HEHE
9、如果用户会编程,加密后的字符串应该可以解密出来,我本人没试过。有兴趣的童鞋可以试试,并留言通知一下。
10、此种方法虽不是完美,但作为一般应用已经足够。如果你想追求完美去研究“RSAProtectedConfigurationProvider”加密方式吧。
示例是C#的,把那个核心过程整个VB的,因为俺的项目用VB
建个类clsApp
调用:
Sub main()
clsApp.ToggleConnectionStringProtection(System.Windows.Forms.Application.ExecutablePath.ToString, True) '连接串加密
fLoginForm = New frmLoginForm
fLoginForm.ShowDialog()
.......
End Sub
Public Class clsApp
' <summary>
' '加密字符串 20130326
'</summary>
Public Shared Sub ToggleConnectionStringProtection(ByVal pathName As String, ByVal protect As Boolean)
' Define the Dpapi provider name.
Dim strProvider As String = "DataProtectionConfigurationProvider"
' string strProvider = "RSAProtectedConfigurationProvider";
Dim oConfiguration As System.Configuration.Configuration = Nothing
Dim oSection As System.Configuration.ConnectionStringsSection = Nothing
Try
' Open the configuration file and retrieve the connectionStrings section.
' For Web!
' oConfiguration = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");
' For Windows!
' Takes the executable file name without the config extension.
oConfiguration = System.Configuration.ConfigurationManager.OpenExeConfiguration(pathName)
If oConfiguration IsNot Nothing Then
Dim blnChanged As Boolean = False
oSection = TryCast(oConfiguration.GetSection("connectionStrings"), System.Configuration.ConnectionStringsSection)
If oSection IsNot Nothing Then
If (Not (oSection.ElementInformation.IsLocked)) AndAlso (Not (oSection.SectionInformation.IsLocked)) Then
If protect Then
If Not (oSection.SectionInformation.IsProtected) Then
blnChanged = True
' Encrypt the section.
oSection.SectionInformation.ProtectSection(strProvider)
End If
Else
If oSection.SectionInformation.IsProtected Then
blnChanged = True
' Remove encryption.
oSection.SectionInformation.UnprotectSection()
End If
End If
End If
If blnChanged Then
' Indicates whether the associated configuration section will be saved even if it has not been modified.
oSection.SectionInformation.ForceSave = True
' Save the current configuration.
oConfiguration.Save()
End If
End If
End If
Catch ex As System.Exception
Throw (ex)
Finally
End Try
End Sub
End Class
- 1
- 2
前往页