protect data using user or machine credentials
Published: 5. January 2009 | Updated: 5. January 2009License: Microsoft Public License (MS-PL)
Categories: Security » Encryption, Windows
Tags: C# Security Text Windows
This example requires reference to System.Security
Import namespaces
using System.Security.Cryptography; using System.Text;
Code
string original = "this is text I want to protect"; Console.WriteLine("Original string: {0}", original); byte[] entropy = { 5, 12, 0, 55, 10, 46, 106, 201, 81 }; byte[] encoded = ProtectedData.Protect(Encoding.Unicode.GetBytes(original), entropy, DataProtectionScope.CurrentUser); Console.WriteLine("Encoded text: {0}", BitConverter.ToString(encoded)); byte[] decoded = ProtectedData.Unprotect(encoded, entropy, DataProtectionScope.CurrentUser); Console.WriteLine("Decoded text: {0}", Encoding.Unicode.GetString(decoded));Console Output:
Original string: this is text I want to protect
Encoded text: 01-00-00-00-D0-8C-9D-DF-01-15-D1-11-8C-7A-00-C0-4F-C2-97-EB-01-00-00-00-89-4C-5A-47-82-BC-E0-4A-94-61-7E-99-88-99-44-5A-00-00-00-00-02-00-00-00-00-00-03-66-00-00-A8-00-00-00-10-00-00-00-C0-70-C2-B8-53-AD-3C-B9-AF-0C-D9-DB-3A-6B-94-EF-00-00-00-00-04-80-00-00-A0-00-00-00-10-00-00-00-8D-30-3E-16-92-F7-A9-6D-83-E1-52-C2-B9-D7-91-B0-40-00-00-00-84-09-3D-D5-23-95-89-E5-0B-80-E7-17-2B-FD-C3-00-F3-DD-07-8E-F0-F3-47-D2-A3-46-E9-28-AC-07-BC-AC-97-A0-19-F5-9C-CD-A1-4C-13-0C-B1-4F-50-55-D8-3B-2B-84-B9-A6-18-C4-78-B2-31-9B-A3-08-6F-69-62-E7-14-00-00-00-27-76-AE-2A-94-B1-C4-DB-BE-97-B5-7A-18-AA-BF-BA-11-BC-F2-B8
Decoded text: this is text I want to protect
Encoded text: 01-00-00-00-D0-8C-9D-DF-01-15-D1-11-8C-7A-00-C0-4F-C2-97-EB-01-00-00-00-89-4C-5A-47-82-BC-E0-4A-94-61-7E-99-88-99-44-5A-00-00-00-00-02-00-00-00-00-00-03-66-00-00-A8-00-00-00-10-00-00-00-C0-70-C2-B8-53-AD-3C-B9-AF-0C-D9-DB-3A-6B-94-EF-00-00-00-00-04-80-00-00-A0-00-00-00-10-00-00-00-8D-30-3E-16-92-F7-A9-6D-83-E1-52-C2-B9-D7-91-B0-40-00-00-00-84-09-3D-D5-23-95-89-E5-0B-80-E7-17-2B-FD-C3-00-F3-DD-07-8E-F0-F3-47-D2-A3-46-E9-28-AC-07-BC-AC-97-A0-19-F5-9C-CD-A1-4C-13-0C-B1-4F-50-55-D8-3B-2B-84-B9-A6-18-C4-78-B2-31-9B-A3-08-6F-69-62-E7-14-00-00-00-27-76-AE-2A-94-B1-C4-DB-BE-97-B5-7A-18-AA-BF-BA-11-BC-F2-B8
Decoded text: this is text I want to protect
| Send us feedback about this snippet » |





