本文整理汇总了VB.NET中System.Configuration.ProtectedConfigurationProvider类的典型用法代码示例。如果您正苦于以下问题:VB.NET ProtectedConfigurationProvider类的具体用法?VB.NET ProtectedConfigurationProvider怎么用?VB.NET ProtectedConfigurationProvider使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了ProtectedConfigurationProvider类的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的VB.NET代码示例。
示例1: Initialize
' 导入命名空间
Imports System.Xml
Imports System.Security.Cryptography
Imports System.IO
Imports System.Text
Imports System.Configuration.Provider
Imports System.Collections.Specialized
Imports System.Configuration
' Shows how to create a custom protected configuration
' provider.
Namespace Samples.AspNet
Public Class TripleDESProtectedConfigurationProvider
Inherits ProtectedConfigurationProvider
Private des _
As New TripleDESCryptoServiceProvider()
Private pKeyFilePath As String
Private pName As String
' Gets the path of the file
' containing the key used to
' encrypt/decrypt.
Public ReadOnly Property KeyFilePath() As String
Get
Return pKeyFilePath
End Get
End Property
' Gets the provider name.
Public Overrides ReadOnly Property Name() As String
Get
Return pName
End Get
End Property
' Performs provider initialization.
Public Overrides Sub Initialize( _
ByVal name As String, _
ByVal config As NameValueCollection)
pName = name
pKeyFilePath = config("keyContainerName")
ReadKey(KeyFilePath)
End Sub
' Performs encryption.
Public Overrides Function Encrypt( _
ByVal node As XmlNode) As XmlNode
Dim encryptedData As String = _
EncryptString(node.OuterXml)
Dim xmlDoc As New XmlDocument()
xmlDoc.PreserveWhitespace = True
xmlDoc.LoadXml( _
("<EncryptedData>" + encryptedData + _
"</EncryptedData>"))
Return xmlDoc.DocumentElement
End Function 'Encrypt
' Performs decryption.
Public Overrides Function Decrypt( _
ByVal encryptedNode As XmlNode) As XmlNode
Dim decryptedData As String = _
DecryptString(encryptedNode.InnerText)
Dim xmlDoc As New XmlDocument()
xmlDoc.PreserveWhitespace = True
xmlDoc.LoadXml(decryptedData)
Return xmlDoc.DocumentElement
End Function 'Decrypt
' Encrypts a configuration section and returns
' the encrypted XML as a string.
Private Function EncryptString( _
ByVal encryptValue As String) As String
Dim valBytes As Byte() = _
Encoding.Unicode.GetBytes(encryptValue)
Dim transform As ICryptoTransform = _
des.CreateEncryptor()
Dim ms As New MemoryStream()
Dim cs As New CryptoStream(ms, _
transform, CryptoStreamMode.Write)
cs.Write(valBytes, 0, valBytes.Length)
cs.FlushFinalBlock()
Dim returnBytes As Byte() = ms.ToArray()
cs.Close()
Return Convert.ToBase64String(returnBytes)
End Function 'EncryptString
' Decrypts an encrypted configuration section and
' returns the unencrypted XML as a string.
Private Function DecryptString( _
ByVal encryptedValue As String) As String
Dim valBytes As Byte() = _
Convert.FromBase64String(encryptedValue)
Dim transform As ICryptoTransform = _
des.CreateDecryptor()
Dim ms As New MemoryStream()
Dim cs As New CryptoStream(ms, _
transform, CryptoStreamMode.Write)
cs.Write(valBytes, 0, valBytes.Length)
cs.FlushFinalBlock()
Dim returnBytes As Byte() = ms.ToArray()
cs.Close()
Return Encoding.Unicode.GetString(returnBytes)
End Function 'DecryptString
' Generates a new TripleDES key and vector and
' writes them to the supplied file path.
Public Sub CreateKey(ByVal filePath As String)
des.GenerateKey()
des.GenerateIV()
Dim sw As New StreamWriter(filePath, False)
sw.WriteLine(ByteToHex(des.Key))
sw.WriteLine(ByteToHex(des.IV))
sw.Close()
End Sub
' Reads in the TripleDES key and vector from
' the supplied file path and sets the Key
' and IV properties of the
' TripleDESCryptoServiceProvider.
Private Sub ReadKey(ByVal filePath As String)
Dim sr As New StreamReader(filePath)
Dim keyValue As String = sr.ReadLine()
Dim ivValue As String = sr.ReadLine()
des.Key = HexToByte(keyValue)
des.IV = HexToByte(ivValue)
End Sub
' Converts a byte array to a hexadecimal string.
Private Function ByteToHex( _
ByVal byteArray() As Byte) As String
Dim outString As String = ""
Dim b As [Byte]
For Each b In byteArray
outString += b.ToString("X2")
Next b
Return outString
End Function 'ByteToHex
' Converts a hexadecimal string to a byte array.
Private Function HexToByte(ByVal hexString As String) As Byte()
Dim returnBytes(hexString.Length / 2) As Byte
Dim i As Integer
For i = 0 To returnBytes.Length - 1
returnBytes(i) = _
Convert.ToByte(hexString.Substring(i * 2, 2), 16)
Next i
Return returnBytes
End Function 'HexToByte
End Class
End Namespace
示例2: ProtectConfiguration
' 导入命名空间
Imports System.Configuration
Namespace Samples.AspNet
' Show how to use a custom protected configuration
' provider.
Public Class TestingProtectedConfigurationProvider
' Protect the connectionStrings section.
Private Shared Sub ProtectConfiguration()
' Get the application configuration file.
Dim config _
As System.Configuration.Configuration = _
ConfigurationManager.OpenExeConfiguration( _
ConfigurationUserLevel.None)
' Define the provider name.
Dim provider As String = _
"TripleDESProtectedConfigurationProvider"
' Get the section to protect.
Dim connStrings _
As ConfigurationSection = config.ConnectionStrings
If Not (connStrings Is Nothing) Then
If Not connStrings.SectionInformation.IsProtected Then
If Not connStrings.ElementInformation.IsLocked Then
' Protect the section.
connStrings.SectionInformation.ProtectSection( _
provider)
connStrings.SectionInformation.ForceSave = True
config.Save(ConfigurationSaveMode.Full)
Console.WriteLine( _
"Section {0} is now protected by {1}", _
connStrings.SectionInformation.Name, _
connStrings.SectionInformation.ProtectionProvider.Name)
Else
Console.WriteLine( _
"Can't protect, section {0} is locked", _
connStrings.SectionInformation.Name)
End If
Else
Console.WriteLine( _
"Section {0} is already protected by {1}", _
connStrings.SectionInformation.Name, _
connStrings.SectionInformation.ProtectionProvider.Name)
End If
Else
Console.WriteLine( _
"Can't get the section {0}", _
connStrings.SectionInformation.Name)
End If
End Sub
' Unprotect the connectionStrings section.
Private Shared Sub UnProtectConfiguration()
' Get the application configuration file.
Dim config _
As System.Configuration.Configuration = _
ConfigurationManager.OpenExeConfiguration( _
ConfigurationUserLevel.None)
' Get the section to unprotect.
Dim connStrings _
As ConfigurationSection = config.ConnectionStrings
If Not (connStrings Is Nothing) Then
If connStrings.SectionInformation.IsProtected Then
If Not connStrings.ElementInformation.IsLocked Then
' Unprotect the section.
connStrings.SectionInformation.UnprotectSection()
connStrings.SectionInformation.ForceSave = True
config.Save(ConfigurationSaveMode.Full)
Console.WriteLine( _
"Section {0} is now unprotected.", _
connStrings.SectionInformation.Name)
Else
Console.WriteLine( _
"Can't unprotect, section {0} is locked", _
connStrings.SectionInformation.Name)
End If
Else
Console.WriteLine( _
"Section {0} is already unprotected.", _
connStrings.SectionInformation.Name)
End If
Else
Console.WriteLine( _
"Can't get the section {0}", _
connStrings.SectionInformation.Name)
End If
End Sub
Public Shared Sub Main(ByVal args() As String)
Dim selection As String = String.Empty
If args.Length = 0 Then
Console.WriteLine( _
"Select createkey, protect or unprotect")
Return
End If
selection = args(0).ToLower()
Select Case selection
' Create the key to use during
' encryption/decryption.
Case "createkey"
Dim keyContainer As String = "pcKey.txt"
' Instantiate the custom provider.
Dim provider _
As New TripleDESProtectedConfigurationProvider()
' Create the encryption/decryption key.
provider.CreateKey(keyContainer)
Console.WriteLine( _
"New TripleDES key created in {0}", _
keyContainer)
Case "protect"
ProtectConfiguration()
Case "unprotect"
UnProtectConfiguration()
Case Else
Console.WriteLine("Unknown selection")
End Select
Console.Read()
End Sub
End Class
End Namespace