-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathProtectedConfigurationProviderLib.cs
More file actions
168 lines (141 loc) · 5.14 KB
/
ProtectedConfigurationProviderLib.cs
File metadata and controls
168 lines (141 loc) · 5.14 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
// <Snippet1>
// WARNING: TripleDES (3DES) is considered deprecated for new applications.
// Use a NIST-approved symmetric encryption algorithm instead.
using System;
using System.Xml;
using System.Security.Cryptography;
using System.IO;
using System.Text;
using System.Configuration.Provider;
using System.Collections.Specialized;
using System.Configuration;
namespace Samples.AspNet
{
// Shows how to create a custom protected configuration
// provider.
public class TripleDESProtectedConfigurationProvider :
ProtectedConfigurationProvider
{
private TripleDESCryptoServiceProvider des =
new TripleDESCryptoServiceProvider();
private string pKeyFilePath;
private string pName;
// Gets the path of the file
// containing the key used to
// encryption or decryption.
public string KeyFilePath
{
get { return pKeyFilePath; }
}
// Gets the provider name.
public override string Name
{
get { return pName; }
}
// Performs provider initialization.
public override void Initialize(string name,
NameValueCollection config)
{
pName = name;
pKeyFilePath = config["keyContainerName"];
ReadKey(KeyFilePath);
}
// <Snippet3>
// Performs encryption.
public override XmlNode Encrypt(XmlNode node)
{
string encryptedData = EncryptString(node.OuterXml);
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.PreserveWhitespace = true;
xmlDoc.LoadXml("<EncryptedData>" +
encryptedData + "</EncryptedData>");
return xmlDoc.DocumentElement;
}
// </Snippet3>
// <Snippet2>
// Performs decryption.
public override XmlNode Decrypt(XmlNode encryptedNode)
{
string decryptedData =
DecryptString(encryptedNode.InnerText);
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.PreserveWhitespace = true;
xmlDoc.LoadXml(decryptedData);
return xmlDoc.DocumentElement;
}
// </Snippet2>
// Encrypts a configuration section and returns
// the encrypted XML as a string.
private string EncryptString(string encryptValue)
{
byte[] valBytes =
Encoding.Unicode.GetBytes(encryptValue);
ICryptoTransform transform = des.CreateEncryptor();
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms,
transform, CryptoStreamMode.Write);
cs.Write(valBytes, 0, valBytes.Length);
cs.FlushFinalBlock();
byte[] returnBytes = ms.ToArray();
cs.Close();
return Convert.ToBase64String(returnBytes);
}
// Decrypts an encrypted configuration section and
// returns the unencrypted XML as a string.
private string DecryptString(string encryptedValue)
{
byte[] valBytes =
Convert.FromBase64String(encryptedValue);
ICryptoTransform transform = des.CreateDecryptor();
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms,
transform, CryptoStreamMode.Write);
cs.Write(valBytes, 0, valBytes.Length);
cs.FlushFinalBlock();
byte[] returnBytes = ms.ToArray();
cs.Close();
return Encoding.Unicode.GetString(returnBytes);
}
// Generates a new TripleDES key and vector and
// writes them to the supplied file path.
public void CreateKey(string filePath)
{
des.GenerateKey();
des.GenerateIV();
StreamWriter sw = new StreamWriter(filePath, false);
sw.WriteLine(ByteToHex(des.Key));
sw.WriteLine(ByteToHex(des.IV));
sw.Close();
}
// Reads in the TripleDES key and vector from
// the supplied file path and sets the Key
// and IV properties of the
// TripleDESCryptoServiceProvider.
private void ReadKey(string filePath)
{
StreamReader sr = new StreamReader(filePath);
string keyValue = sr.ReadLine();
string ivValue = sr.ReadLine();
des.Key = HexToByte(keyValue);
des.IV = HexToByte(ivValue);
}
// Converts a byte array to a hexadecimal string.
private string ByteToHex(byte[] byteArray)
{
string outString = "";
foreach (Byte b in byteArray)
outString += b.ToString("X2");
return outString;
}
// Converts a hexadecimal string to a byte array.
private byte[] HexToByte(string hexString)
{
byte[] returnBytes = new byte[hexString.Length / 2];
for (int i = 0; i < returnBytes.Length; i++)
returnBytes[i] =
Convert.ToByte(hexString.Substring(i * 2, 2), 16);
return returnBytes;
}
}
}
// </Snippet1>