none
connection string encryption software RRS feed

  • Question

  • 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.

    https://code.msdn.microsoft.com/Secure-connection-string-d9b9b8a8?redir=0

    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.



    • Edited by AbanoubZak Saturday, September 28, 2019 1:32 PM
    Saturday, September 28, 2019 12:49 PM

All replies

  • Hello,

    Without the exact error message all that can be done is guess at what is going on and I would rather not guess.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Saturday, September 28, 2019 1:51 PM
    Moderator
  • Hi,

    I am afraid is not enough for us to narrow down your issue.

    Could you share us more information? What error message did you get? If you are develop with winform, could you share us which code causes this error?

    Best Regards,

    Julie


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, October 1, 2019 7:57 AM
    Moderator
  • Hello,

    Without the exact error message all that can be done is guess at what is going on and I would rather not guess.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    that`s the problem the Error message doesn't give enough information "

    A Problem Caused The Program To Stop Working Correctly. Windows Will Close The Program And Notify You If A Solution Is Available"

    Wednesday, October 9, 2019 4:17 AM
  • Hi,

    I am afraid is not enough for us to narrow down your issue.

    Could you share us more information? What error message did you get? If you are develop with winform, could you share us which code causes this error?

    Best Regards,

    Julie


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    the application wont even start to use "try / catch" so i can't navigate  directly to the error or the code that cause it.

    and it happens only on the client machine so i wont be able to debug my app directly on his side.

    Wednesday, October 9, 2019 4:21 AM