悬疑灵异

通过代码加密配呢

.NET Framework从2.0版本开始就引进了加密技术来支持配置文件。上个星期的文章主要关注使用 命令行工具加密部分配置文件,这个星期的文章包括编码选项。在代码中,.NET Framework库对控制加密和解密提供了完全支持。我下面所举的例子,使用了和C#两种语言来向大家演示配置文件节的加密和解密。

保护敏感数据

即使用户访问了配置文件,你也可以通过加密配置数据,使用户很难看到配置文件的数据来提高应用软件的安全性。在中有两种受保护的配置提供者,它们分别是:RSAProtectedConfigurationProvider和 DPAPIProtectedConfigurationProvider。RSAProtectedConfigurationProvider使用RSACryptoServiceProvider来加密配置文件节,这里的配置文件节使用RSA公共密钥加密方法来加密和解密数据。

DPAPIProtectedConfigurationProvider使用Windows Data Protection API (DPAPI)来加密配置文件节,这里的配置文件节使用内置的Windows密码系统。如果有必要你也可以创建你自己的受保护的设置提供者。虽然用户很难处理加密数据,但是处理加密数据却易如反掌。在代码你可以使用这两个提供者。

使用代码

.NET Framework 2.0允许你在Web配置文件和机器配置文件中加密大部分配置节。nfiguration命名空间过去习惯于通过代码加密配置文件。它包括两种方法进行加密:ProtectSection方法和UnprotectSection方法。

ProtectSection方法:将一个配置节标记为保护。这个方法使用唯一参数——提供者的名字(一个字符串值)来进行加密。 UnprotectSection方法:从关联的配置节中移去被保护的配置加密。它不需要参数。 作为一个例子,下面这个简单的nfig文件演示了配置数据的加密和解密:

?xml version=\"1.0\"?

configuration

appSettings/

connectionStrings

add name=\"db\" connectionString=\"connection details\" /

/connectionStrings

b

compilation debug=\"false\" /

authentication mode=\"Windows\" /

/b

/configuration

下面的代码来自 Web表格加密文件的connectionStrings部分:

Public Sub Page_Load()

Dim config As Configuration

Dim configSection As ConfigurationSection

config = enWebConfiguration(plicationPath)

If Not (config Is Nothing) Then

configSection = tSection(\"connectionStrings\")

If Not (configSection Is Nothing) Then

If Not (Locked) Then

otectSection(\"DataProtectionConfigurationProvider\")

ve()

End If

End If

End If

End Sub

代码执行下面步骤:

它使用nfiguration命名空间与必要的类一起工作。 它通过OpenWebConfiguration方法的WebConfigurationManager类访问应用程序的nfig文件。 它通过GetSection方法访问nfig文件的connectionStrings节。 如果文件没有被锁上,节可以使用默认的Windows加密方案进行加密。 这些变化被保存在文件中。 等价的C#代码如下:

protected void Page_Load(object sender, EventArgs e) {

Configuration config;

ConfigurationSection configSection;

config = enWebConfiguration(plicationPath);

configSection = tSection(\"connectionStrings\");

if (configSection != null) {

if (!(Locked)) {

otectSection(\"DataProtectionConfigurationProvider\");

ve();

} } }

一旦这个代码运行,现在,我的nfig文件中的connectionStrings节显示的内容将如下所示:

connectionStrings configProtectionProvider=\"DataProtectionConfigurationProvider\"

EncryptedData

CipherData

CipherValueAQAAANCMnd8BFdERjHoAwE/Cl+sBAAAAaZ2eussTfEmhwe+kgLsWVwQAAAACAAAAAAADZgAAqAAAABAAAADnyhn4dHzOLGFsIj8QUrXgAAAAAASAAACgAAAAEAAAAKFRR3MAelpxxV6J+KEhfqnQAAAAFJOBaI5ciKhw3Ywra+G0hkZb67k0YTJmXYe5+5cpZ3Wd3H2696mEhAGQiTecOVGixqtF9lHa+QipmMSHcVECiWYjOh/6CIQL6GED37erb4TLZSNo4U7FrE2JscNCnKaKZUtvnxVqRmjcDWU7Gm2rYRAHoDSEy0UE7ebbcqr7LQ+Y+C7WrFk+VKf6NmN4js4vl7TJXl/Nr36Z65bvZDCxcle66rZ2yebtXMTP2bX95NasbQx0trvnjJrdIdMMrLOqLDPhQLwZ4ObCxkh+Rlg4NxQAAABU+1akHFhrg+4d0AmCGE8Egt3HrA==/CipherValue

/CipherData

/EncryptedData

/connectionStrings

下面的代码是解密节代码,同时显示了它的值:

“这使朝鲜领导人感到莫大的失望。金日成建议中国同志在战俘问题上做出让步 Public Sub Page_Load()

Dim config As Configuration

Dim configSection As ConfigurationSection

config = enWebConfiguration(plicationPath)

configSection = tSection(\"connectionStrings\")

If Not (configSection Is Nothing) Then

If Not (Locked) Then

protectSection()

ve()

End If

End If

End Sub

这个解密代码除了对指定的节调用UnprotectSection方法进行解密与先前的例子不同之外,其它部分都与先前的例子一样。等价的C#代码如下:

protected void Page_Load(object sender, EventArgs e) {

Configuration config;

ConfigurationSection configSection;

config = enWebConfiguration(plicationPath);

configSection = tSection(\"connectionStrings\");

if (configSection != null) {

if (!(Locked)) {

protectSection();ve();

} } }

钦州治疗白癜风哪家好
西安男科治疗哪家好
避孕药有几种类型
友情链接