Click here to Skip to main content
15,914,165 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Guys, I have a program that runs straight from a USB stick, I would like to encrypt and decrypt your app.config on any computer, but I'm new in this area and I don't know how to do that. Can anyone help me a functional example?

I am using this code to encrypt and decrypt, but it does not work on all computers.

What I have tried:

VB
System.Configuration

Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        'Encrypt
        Dim config As System.Configuration.Configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
        Dim configSection As ConfigurationSection
        configSection = config.AppSettings

        If Not (configSection Is Nothing) Then
            If Not (configSection.ElementInformation.IsLocked) Then   configSection.SectionInformation.ProtectSection("rsaProtectedConfigurationProvider")
                configSection.SectionInformation.ForceSave = True
                config.Save(ConfigurationSaveMode.Full)
            End If
        End If
        MsgBox("Success!")
    End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        'Decrypt
        Dim config As System.Configuration.Configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
        Dim configSection As ConfigurationSection
        configSection = config.AppSettings

        If Not (configSection Is Nothing) Then
            If Not (configSection.ElementInformation.IsLocked) Then
                configSection.SectionInformation.UnprotectSection()
                configSection.SectionInformation.ForceSave = True
                config.Save(ConfigurationSaveMode.Full)
            End If
        End If
        MsgBox("Success!")
    End Sub
End Class
Posted
Updated 16-Aug-19 9:39am
v2
Comments
Patrice T 16-Aug-19 17:18pm    
Just wondering: what is the legitimate usage of having an exe on usb which will encrypt a file on every computer it visit ?
RafaelFranckilin 16-Aug-19 19:42pm    
Exactly, I need it to decrypt and encrypt on every computer I visit, the function of my program is to do automatic program installs silently and other system settings, can you help me with this question?
other people will use this program and I don't want them to be able to access app.config.

1 solution

Chances are that the affected machines do not have the appropriate key in their RSA Key Container... Your program will need access to the NetFrameworkConfigurationKey for decryption.

References:
Importing and Exporting Protected Configuration RSA Key Containers[^]
Rostyslav's blog: Encrypting and Decrypting Web.config Sections in .NET 4.0[^]
 
Share this answer
 
Comments
RafaelFranckilin 16-Aug-19 19:38pm    
I had already seen the links you showed, I just don't know how I can use it in my program, could you explain to me how to give my program access to NetFrameworkConfigurationKey for decryption? enjoying this code of mine?
MadMyche 16-Aug-19 20:55pm    
Research export/import the keys, and add the key onto that USB of yours
RafaelFranckilin 19-Aug-19 12:10pm    
I researched long before seeking help here, found no practical example that I could use in my program, as I'm new to this area, found no material that I can understand, need some functional example in the language I'm using for myself to understand.
If anyone can help me, I'm very grateful

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900