-
Notifications
You must be signed in to change notification settings - Fork 0
/
DPAPI with ProtectedData class.linq
42 lines (33 loc) · 1.5 KB
/
DPAPI with ProtectedData class.linq
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
<Query Kind="Statements">
<Reference><RuntimeDirectory>\System.Security.dll</Reference>
<Namespace>System.Security.Cryptography</Namespace>
</Query>
byte[] entropy = { 9, 8, 7, 6, 5, 100, 105, 203, 215, 1, 9, 22, 30, 56, 27, 88, 97, 143, 173 };
entropy = Encoding.ASCII.GetBytes("Talmer");
string message = @"{""__type"":""UserQuery.Alpha:#"",""Betas"":[{""Id"":""beta1"",""Name"":""x-men""},{""Id"":""beta2"",""Name"":""super-man""},{""Id"":""beta3"",""Name"":""spider-man""},{""Id"":""beta4"",""Name"":""iron man""},{""Id"":""beta5"",""Name"":""wonder woman""}],""CreateDt"":""\/Date(950590800000-0500)\/"",""Id"":""alpha1"",""Name"":""Bob Smith"",""Value"":101.09}";
byte[] data = Encoding.ASCII.GetBytes(message);
DataProtectionScope scope = DataProtectionScope.LocalMachine;
byte[] encrypted = ProtectedData.Protect(data, entropy, scope);
Console.WriteLine("Encrypted Data:");
for(int i=0; i<encrypted.Length; i++)
{
Console.Write(encrypted[i]);
Console.Write(", ");
}
Console.WriteLine();
Console.WriteLine();
//----
//entropy = new byte[entropy.Length];
entropy = Encoding.ASCII.GetBytes("Talmer");
//encrypted = new byte[encrypted.Length];
//----
byte[] decrypted = ProtectedData.Unprotect(encrypted, entropy, DataProtectionScope.LocalMachine);
Console.WriteLine("Decrypted Data:");
for(int i=0; i<decrypted.Length; i++)
{
Console.Write(decrypted[i]);
Console.Write(", ");
}
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("Decrypted Message: \"{0}\"", Encoding.ASCII.GetString(decrypted));