LoginSignup
1
0

More than 1 year has passed since last update.

userSettingsを暗号化する

Posted at

事前準備

  1. プロジェクトの参照設定にSystem.Configurationを追加
  2. プロジェクトの設定に設定項目を追加
    • 暗号化されるのはスコープがユーザーの項目が対象です。

暗号化

アプリケーションの起動のどこかで以下を行ってください。

Form1.vb
Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Const provider As String = "DataProtectionConfigurationProvider"
        Dim config As Configuration.Configuration = Configuration.ConfigurationManager.OpenExeConfiguration(System.Configuration.ConfigurationUserLevel.PerUserRoamingAndLocal)
        Dim sectiongroup As Configuration.ConfigurationSectionGroup = config.GetSectionGroup("userSettings")
        Dim count As Integer = 0

        For Each section As Configuration.ClientSettingsSection In sectiongroup.Sections
            If Not section.SectionInformation.IsProtected Then
                section.SectionInformation.ProtectSection(provider)
                count += 1
            End If
        Next

        If count > 0 Then
            config.Save()
        End If
    End Sub
End Class

user.configの場所

上記コードでconfig.Save()を呼び出されると以下のフォルダに暗号化されたファイルが作成されます。

%localappdata%\AssemblyCompany\AssemblyTitle.exe_Url_生成された値\version\user.config

内容を確認すると暗号化されているのがわかります。

user.config
<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <userSettings>
        <CnfgEncrypt.My.MySettings configProtectionProvider="DataProtectionConfigurationProvider">
            <EncryptedData>
                <CipherData>
                    <CipherValue>AQAAANCMnd8BFdERjHoAwE/Cl+sBAAAA1xVCoWYWb0q+dkt7bnGzegQAAAACAAAAAAADZgAAwAAAABAAAABAlOcG9igpl6nbcz1EaIGNAAAAAASAAACgAAAAEAAAAPVGNEE0CHNXArdywbF5sNtAAAAAJp2f/6iMcPoxietfeA6tgPS0KFT3U/U0JscezvRRIEhZW/xO1OLzjwVRC6ZQyt0fkeApI5OvV2xmx9S/7bUd2hQAAACS4J1y2jOoJ8zV2OZUGGVomg1NUQ==</CipherValue>
                </CipherData>
            </EncryptedData>
        </CnfgEncrypt.My.MySettings>
    </userSettings>
</configuration>

免責

ドシロートなんで、暗号の強度なんかについてはわかりません。
よって、責任も持てません。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0