-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfigEncryption.cs
More file actions
229 lines (212 loc) · 11.9 KB
/
Copy pathConfigEncryption.cs
File metadata and controls
229 lines (212 loc) · 11.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
using System;
using System.Configuration;
using System.Reflection;
namespace FileAutoCleaner
{
/// <summary>
/// Utility class for encrypting and decrypting sections of a configuration file.
/// 구성 파일의 섹션을 암호화하고 복호화하는 유틸리티 클래스
/// </summary>
public class ConfigEncryption
{
/// <summary>
/// Encrypts the connectionStrings section.
/// connectionStrings 섹션을 암호화합니다.
/// </summary>
/// <param name="configPath">Path to the configuration file to encrypt (if null, uses the current app's config file). 암호화할 구성 파일의 경로 (null인 경우 현재 앱의 구성 파일 사용)</param>
/// <returns>True if encryption succeeded. 암호화 성공 여부</returns>
public static bool EncryptConnectionStrings(string configPath = null)
{
try
{
// If config path is not specified, use the current app's config file.
// 구성 파일 경로가 지정되지 않은 경우 현재 앱의 구성 파일 사용
Configuration config = OpenConfiguration(configPath);
// Get the connectionStrings section
// connectionStrings 섹션 가져오기
ConfigurationSection section = config.GetSection("connectionStrings");
if (section != null && !section.SectionInformation.IsProtected)
{
// Encrypt the section using DPAPI (current user or machine account)
// DPAPI를 사용하여 섹션 암호화 (현재 사용자 또는 머신 계정)
section.SectionInformation.ProtectSection("DataProtectionConfigurationProvider");
section.SectionInformation.ForceSave = true;
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("connectionStrings");
Console.WriteLine("connectionStrings section encrypted successfully.\nconnectionStrings 섹션이 성공적으로 암호화되었습니다.");
return true;
}
else if (section != null && section.SectionInformation.IsProtected)
{
Console.WriteLine("connectionStrings section is already encrypted.\nconnectionStrings 섹션은 이미 암호화되어 있습니다.");
return true;
}
else
{
Console.WriteLine("connectionStrings section not found.\nconnectionStrings 섹션을 찾을 수 없습니다.");
return false;
}
}
catch (Exception ex)
{
Console.WriteLine($"Error occurred while encrypting connectionStrings section: {ex.Message}\nconnectionStrings 섹션 암호화 중 오류 발생: {ex.Message}");
return false;
}
}
/// <summary>
/// Decrypts the connectionStrings section.
/// connectionStrings 섹션의 암호화를 해제합니다.
/// </summary>
/// <param name="configPath">Path to the configuration file to decrypt (if null, uses the current app's config file). 복호화할 구성 파일의 경로 (null인 경우 현재 앱의 구성 파일 사용)</param>
/// <returns>True if decryption succeeded. 복호화 성공 여부</returns>
public static bool DecryptConnectionStrings(string configPath = null)
{
try
{
// If config path is not specified, use the current app's config file.
// 구성 파일 경로가 지정되지 않은 경우 현재 앱의 구성 파일 사용
Configuration config = OpenConfiguration(configPath);
// Get the connectionStrings section
// connectionStrings 섹션 가져오기
ConfigurationSection section = config.GetSection("connectionStrings");
if (section != null && section.SectionInformation.IsProtected)
{
// Decrypt the section
// 섹션 암호화 해제
section.SectionInformation.UnprotectSection();
section.SectionInformation.ForceSave = true;
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("connectionStrings");
Console.WriteLine("connectionStrings section decrypted successfully.\nconnectionStrings 섹션이 성공적으로 복호화되었습니다.");
return true;
}
else if (section != null && !section.SectionInformation.IsProtected)
{
Console.WriteLine("connectionStrings section is not encrypted.\nconnectionStrings 섹션은 암호화되어 있지 않습니다.");
return true;
}
else
{
Console.WriteLine("connectionStrings section not found.\nconnectionStrings 섹션을 찾을 수 없습니다.");
return false;
}
}
catch (Exception ex)
{
Console.WriteLine($"Error occurred while decrypting connectionStrings section: {ex.Message}\nconnectionStrings 섹션 복호화 중 오류 발생: {ex.Message}");
return false;
}
}
/// <summary>
/// Encrypts the appSettings section.
/// appSettings 섹션을 암호화합니다.
/// </summary>
/// <param name="configPath">Path to the configuration file to encrypt (if null, uses the current app's config file). 암호화할 구성 파일의 경로 (null인 경우 현재 앱의 구성 파일 사용)</param>
/// <returns>True if encryption succeeded. 암호화 성공 여부</returns>
public static bool EncryptAppSettings(string configPath = null)
{
try
{
// If config path is not specified, use the current app's config file.
// 구성 파일 경로가 지정되지 않은 경우 현재 앱의 구성 파일 사용
Configuration config = OpenConfiguration(configPath);
// Get the appSettings section
// appSettings 섹션 가져오기
ConfigurationSection section = config.GetSection("appSettings");
if (section != null && !section.SectionInformation.IsProtected)
{
// Encrypt the section using DPAPI (current user or machine account)
// DPAPI를 사용하여 섹션 암호화 (현재 사용자 또는 머신 계정)
section.SectionInformation.ProtectSection("DataProtectionConfigurationProvider");
section.SectionInformation.ForceSave = true;
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");
Console.WriteLine("appSettings section encrypted successfully.\nappSettings 섹션이 성공적으로 암호화되었습니다.");
return true;
}
else if (section != null && section.SectionInformation.IsProtected)
{
Console.WriteLine("appSettings section is already encrypted.\nappSettings 섹션은 이미 암호화되어 있습니다.");
return true;
}
else
{
Console.WriteLine("appSettings section not found.\nappSettings 섹션을 찾을 수 없습니다.");
return false;
}
}
catch (Exception ex)
{
Console.WriteLine($"Error occurred while encrypting appSettings section: {ex.Message}\nappSettings 섹션 암호화 중 오류 발생: {ex.Message}");
return false;
}
}
/// <summary>
/// Decrypts the appSettings section.
/// appSettings 섹션의 암호화를 해제합니다.
/// </summary>
/// <param name="configPath">Path to the configuration file to decrypt (if null, uses the current app's config file). 복호화할 구성 파일의 경로 (null인 경우 현재 앱의 구성 파일 사용)</param>
/// <returns>True if decryption succeeded. 복호화 성공 여부</returns>
public static bool DecryptAppSettings(string configPath = null)
{
try
{
// If config path is not specified, use the current app's config file.
// 구성 파일 경로가 지정되지 않은 경우 현재 앱의 구성 파일 사용
Configuration config = OpenConfiguration(configPath);
// Get the appSettings section
// appSettings 섹션 가져오기
ConfigurationSection section = config.GetSection("appSettings");
if (section != null && section.SectionInformation.IsProtected)
{
// Decrypt the section
// 섹션 암호화 해제
section.SectionInformation.UnprotectSection();
section.SectionInformation.ForceSave = true;
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");
Console.WriteLine("appSettings section decrypted successfully.\nappSettings 섹션이 성공적으로 복호화되었습니다.");
return true;
}
else if (section != null && !section.SectionInformation.IsProtected)
{
Console.WriteLine("appSettings section is not encrypted.\nappSettings 섹션은 암호화되어 있지 않습니다.");
return true;
}
else
{
Console.WriteLine("appSettings section not found.\nappSettings 섹션을 찾을 수 없습니다.");
return false;
}
}
catch (Exception ex)
{
Console.WriteLine($"Error occurred while decrypting appSettings section: {ex.Message}\nappSettings 섹션 복호화 중 오류 발생: {ex.Message}");
return false;
}
}
/// <summary>
/// Opens the configuration file at the specified path or the current app's config file.
/// 지정된 경로의 구성 파일을 열거나 현재 앱의 구성 파일을 엽니다.
/// </summary>
/// <param name="configPath">Configuration file path (if null, uses the current app's config file). 구성 파일 경로 (null인 경우 현재 앱의 구성 파일 사용)</param>
/// <returns>The opened configuration object. 열린 구성 객체</returns>
private static Configuration OpenConfiguration(string configPath)
{
if (string.IsNullOrEmpty(configPath))
{
// Open the current executable's configuration
// 현재 실행 파일의 구성 열기
return ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
}
else
{
// Open the configuration file at the specified path
// 지정된 경로의 구성 파일 열기
ExeConfigurationFileMap configFileMap = new ExeConfigurationFileMap();
configFileMap.ExeConfigFilename = configPath;
return ConfigurationManager.OpenMappedExeConfiguration(configFileMap, ConfigurationUserLevel.None);
}
}
}
}