Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

App Config Encryption in VB.net

$
0
0
Hello All,

I have search to net on how can I encrypt my App.config that I created in VB Winform.

What I`m expecting is that when I view the App.Config file in the Application Folder I should not be able to view the Database user and Password but, it does not seems to be working.

Here is my App Config Code.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="Vault"
             type="System.Configuration.NameValueSectionHeader"/>
    <sectionGroup name="applicationSettings"
                  type="System.Configuration.ApplicationSettingsGroup, System,
                  Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <section name="EncrpytOracle_Connection"
               type="System.Configuration.ClientSettingsSection, System,
                    Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a561934e089"
               requirePermission="false"/>
    </sectionGroup>
  </configSections>

  <connectionStrings>
    <add name="Oracle_Connection" connectionString="Driver=Microsoft ODBC for Oracle;uid=SOMEID;pwd=SOMEPWD;server=SOMEDB"/>
  </connectionStrings>
    <system.diagnostics>
        <sources>
            &lt!-- This section defines the logging configuration for My.Application.Log -->
            <source name="DefaultSource" switchName="DefaultSwitch">
                <listeners>
                    <add name="FileLog"/>
                    &lt!-- Uncomment the below section to write to the Application Event Log -->
                    &lt!--<add name="EventLog"/>-->
                </listeners>
            </source>
        </sources>
        <switches>
            <add name="DefaultSwitch" value="Information" />
        </switches>
        <sharedListeners>
            <add name="FileLog"
                 type="Microsoft.VisualBasic.Logging.FileLogTraceListener, Microsoft.VisualBasic, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" 
                 initializeData="FileLogWriter"/>
            &lt!-- Uncomment the below section and replace APPLICATION_NAME with the name of your application to write to the Application Event Log -->
            &lt!--<add name="EventLog" type="System.Diagnostics.EventLogTraceListener" initializeData="APPLICATION_NAME"/> -->
        </sharedListeners>
    </system.diagnostics>
</configuration>


As you can see in my connection string DB user Name, Password and Server is visible.

How do I encrypt it?

Please also check the my Class for decryption.

Imports System.Configuration

Public Class Protection
    Private m_section As String


    Public Sub New(ByVal section As String)
        If String.IsNullOrEmpty(section) Then _
            Throw New ArgumentNullException("ConfigurationSection")

        m_section = section
    End Sub


    Public Sub Protection()
        Dim config As Configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)

        Dim ProtectedSection As ConfigurationSection = config.GetSection(m_section)

        If ((ProtectedSection IsNot Nothing) _
            AndAlso (Not ProtectedSection.IsReadOnly) _
            AndAlso (Not ProtectedSection.SectionInformation.IsProtected) _
            AndAlso (Not ProtectedSection.SectionInformation.IsLocked) _
            AndAlso (ProtectedSection.SectionInformation.IsDeclared)) Then
            ProtectedSection.SectionInformation.ProtectSection(Nothing)

            ProtectedSection.SectionInformation.ForceSave = True
            config.Save(ConfigurationSaveMode.Full)
        End If
    End Sub
End Class


This is actually my 1st time to use app config encryption.

Can someone kindly give me an advice.

Thank you.

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>