Skip to content

Instantly share code, notes, and snippets.

@FacuM
Last active July 2, 2019 06:33
Show Gist options
  • Save FacuM/48b1a2ab382f27ed8dfdfe1f59627898 to your computer and use it in GitHub Desktop.
Save FacuM/48b1a2ab382f27ed8dfdfe1f59627898 to your computer and use it in GitHub Desktop.
A small snippet to encrypt any VB.Net WinForms application using the RSA algorithm. Decryption is handled automatically on runtime, due to the proper implementation.
Dim Configuration As Configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal) ' Take the configuration file in %APPDATA%\..\Local\<Application.Info.Title>\<Version>\user.config
Dim Section As ConfigurationSection = Configuration.GetSection("userSettings/" & My.Application.Info.Title & ".My.MySettings") ' Take the sections group of the file called userSettings and select the section <Application.Info.Title>.My.Settings (i.e.: WindowsApplication1.My.Settings).
If Not Section.SectionInformation.IsProtected Then ' If it's not encrypted...
Section.SectionInformation.ProtectSection("RsaProtectedConfigurationProvider") ' Encrypt it. Note the selection of the proper provider "RsaProtectedConfigurationProvider".
End If
Section.SectionInformation.ForceSave = True ' Force save, it might not work else because it might think it shouldn't be saved (i.e.: already protected, but with changes).
Configuration.Save(ConfigurationSaveMode.Full) ' Force saving the full configuration file, just in case, it might skip it too (it happens just too often).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment