-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
168 lines (151 loc) · 6.77 KB
/
Copy pathProgram.cs
File metadata and controls
168 lines (151 loc) · 6.77 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
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
namespace CN.Wangxinhong.DotnetConfigEncUtil
{
class Program
{
//TODO: add diagnostics debug infomations
private readonly static String XPATH_Configuration = "/configuration";
private readonly static String XPATH_Configuration_ConfigSections = "/configuration/configSections";
/// <summary>
/// default encoding of the config file(utf-8)
/// </summary>
private readonly static Encoding DEFAULT_XML_Encoding = Encoding.UTF8;
/// <summary>
/// default protection provider(rsa protection provider)
/// </summary>
private readonly static String DEFAULT_ProtectProvider = "RsaProtectedConfigurationProvider";
static void ThrowArgumentException()
{
Console.WriteLine(
"Usage: --configPath C:\\test\\web.config " +
" --configSection DatabaseConfiguration" +
" --mode encrypt/decrypt"
//+ " --output C:\\test\\web.config.new "
);
throw new ArgumentException();
}
static int Main(string[] args)
{
// the config file path(web.config or app.config)
String configPath = String.Empty;
// config section to encrypt/decrypt
String configSection = String.Empty;
// encrypt or decrypt
Boolean encryptionFlag = true;
#region process the commandline arguments
Arguments CommandLine = new Arguments(args);
if (CommandLine["configPath"] != null)
configPath = CommandLine["configPath"];
else
ThrowArgumentException();
if (CommandLine["configSection"] != null)
configSection = CommandLine["configSection"];
else
ThrowArgumentException();
if (CommandLine["mode"] != null)
{
if (CommandLine["mode"].Equals("encrypt", StringComparison.InvariantCultureIgnoreCase))
encryptionFlag = true;
else if (CommandLine["mode"].Equals("decrypt", StringComparison.InvariantCultureIgnoreCase))
encryptionFlag = false;
else
ThrowArgumentException();
}
else
ThrowArgumentException();
#endregion
// cut over header section list
var swapHeaderSectionsList = CutSelectedConfigSection(configPath);
try
{
// encrypt the config path
var fileMap = new ExeConfigurationFileMap();
fileMap.ExeConfigFilename = configPath;
var configuration = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
var section = configuration.GetSection(configSection);
if (section != null && encryptionFlag && !section.SectionInformation.IsProtected)
{
section.SectionInformation.ProtectSection(DEFAULT_ProtectProvider);
}
else if (section != null && !encryptionFlag && section.SectionInformation.IsProtected)
{
section.SectionInformation.UnprotectSection();
}
else
{
if (section == null)
// no such section, unexpected
return (int)ReturnResult.CONFIG_SECTION_NOT_FOUND;
else
// to encrypt what already encrypted, or decrypt which plain text
// take no action, and nop to the file
return (int)ReturnResult.NOACTION_TAKEN;
}
configuration.Save(ConfigurationSaveMode.Minimal, true);
}
catch (Exception ex)
{
return (int)ReturnResult.EXCEPTION_DETECTED;
}
finally
{
// write back header Section List
RestoreSwapedConfigSection(configPath, swapHeaderSectionsList);
}
return (int)ReturnResult.SUCCESS;
}
/// <summary>
/// restore the swaped config section to file
/// </summary>
/// <param name="configPath"></param>
/// <param name="swapHeaderSectionsList"></param>
private static void RestoreSwapedConfigSection(String configPath, XmlNode swapHeaderSectionsList)
{
var xmlDocument = new XmlDocument();
using (XmlTextReader xmlReader = new XmlTextReader(configPath))
{
xmlDocument.Load(xmlReader);
}
var headConfiguration = xmlDocument.SelectSingleNode(XPATH_Configuration);
var currentSwapHeaderSectionsList = xmlDocument.ImportNode(swapHeaderSectionsList, deep: true);
var headerSectionsList = xmlDocument.SelectSingleNode(XPATH_Configuration_ConfigSections);
headConfiguration.ReplaceChild(currentSwapHeaderSectionsList, headerSectionsList);
using (XmlTextWriter xmlWriter = new XmlTextWriter(configPath, DEFAULT_XML_Encoding))
{
xmlWriter.Formatting = Formatting.Indented;
xmlDocument.Save(xmlWriter);
}
}
/// <summary>
/// cut over the selected config section and save to file
/// </summary>
/// <param name="configPath">config file path</param>
/// <returns>the selected config section</returns>
private static XmlNode CutSelectedConfigSection(String configPath)
{
XmlDocument xmlDocument = new XmlDocument();
using (XmlTextReader xmlReader = new XmlTextReader(configPath))
{
xmlDocument.Load(xmlReader);
}
var headerSectionsList = xmlDocument.SelectSingleNode(XPATH_Configuration_ConfigSections);
if (headerSectionsList == null)
return null;
XmlNode swapHeaderSectionsList = headerSectionsList.CloneNode(deep: true);
headerSectionsList.RemoveAll();
using (XmlTextWriter xmlWriter = new XmlTextWriter(configPath, DEFAULT_XML_Encoding))
{
xmlWriter.Formatting = Formatting.Indented;
xmlDocument.Save(xmlWriter);
}
return swapHeaderSectionsList;
}
}
}