connection string encryption software

  • Thread starter Thread starter AbanoubZak
  • Start date Start date
A

AbanoubZak

Guest
Hello,

days ago i asked i i can encrypt and decrypt connection string in my application and "Kareninstructor" help me with that by the flowing code and i quote "

There are methods in the .NET Framework to permit encryption and decryption which I have create a class to do those operations on any connection string (does not matter the database) stored under My.Setting under project properties.

In the code sample I use SQL-Server.

Secure connection string for Windows Forms (VB.NET) in VB.NET for Visual Studio 2017

The base class

Imports System.Configuration
Imports System.IO
Public Class ConnectionProtection
Public Property FileName As String
''' <summary>
''' Determine if configuration file exists for application
''' </summary>
''' <param name="FileName">Current executable and path</param>
Public Sub New(FileName As String)
If Not File.Exists(String.Concat(FileName, ".config")) Then
Throw New FileNotFoundException(String.Concat(FileName, ".config"))
End If
Me.FileName = FileName
End Sub
''' <summary>
''' Encrypt ConnectionString
''' </summary>
''' <param name="encrypt"></param>
''' <param name="fileName"></param>
''' <returns></returns>
Private Function EncryptConnectionString(encrypt As Boolean, fileName As String) As Boolean
Dim success As Boolean = True
Dim configuration As Configuration = Nothing

Try
configuration = ConfigurationManager.OpenExeConfiguration(fileName)
Dim configSection = TryCast(configuration.GetSection("connectionStrings"), ConnectionStringsSection)

If (Not (configSection.ElementInformation.IsLocked)) AndAlso (Not (configSection.SectionInformation.IsLocked)) Then
If encrypt AndAlso (Not configSection.SectionInformation.IsProtected) Then
' encrypt the file
configSection.SectionInformation.ProtectSection("DataProtectionConfigurationProvider")
End If

If (Not encrypt) AndAlso configSection.SectionInformation.IsProtected Then 'encrypt is true so encrypt
' decrypt the file.
configSection.SectionInformation.UnprotectSection()
End If

configSection.SectionInformation.ForceSave = True
configuration.Save()

success = True

End If
Catch ex As Exception
success = False
End Try

Return success

End Function
Public Function IsProtected() As Boolean
Dim configuration As Configuration = ConfigurationManager.OpenExeConfiguration(FileName)
Dim configSection = TryCast(configuration.GetSection("connectionStrings"), ConnectionStringsSection)
Return configSection.SectionInformation.IsProtected
End Function
Public Function EncryptFile() As Boolean
If File.Exists(FileName) Then
Return EncryptConnectionString(True, FileName)
Else
Return False
End If
End Function
Public Function DecryptFile() As Boolean
If File.Exists(FileName) Then
Return EncryptConnectionString(False, FileName)
Else
Return False
End If
End Function
End Class


In regards to more than one table, sure this is possible to decrypt once. Under project properties, Application tab click "View Application Events" and place the following code in.

Imports ConfigurationLibrary_vb
Imports Microsoft.VisualBasic.ApplicationServices

Namespace My
Partial Friend Class MyApplication
Private operations As New ConnectionProtection(
Windows.Forms.Application.ExecutablePath)

Private Sub MyApplication_Startup(sender As Object, e As StartupEventArgs) Handles Me.Startup
If Not operations.IsProtected() Then
operations.EncryptFile()
End If
operations.DecryptFile()
End Sub
Private Sub MyApplication_Shutdown(sender As Object, e As EventArgs) Handles Me.Shutdown
operations.EncryptFile()
End Sub
End Class
End Namespace



""

now i'm suing this code and it worked fine on my machine and still but on the client machine the app does even start and give an error the older version worked fine in his machine and after i tried to remove the encryption totally the app worked but with no access to the data base.

now do i need a special framwork or software on the client machine to make my app work using the method above?

Thank you.

Continue reading...
 
Back
Top