Secure storage of passwords in .NET

Storing passwords securely in an application, for instance between different invocations, is a major challenge for a software developer. Storing a password or other sensitive data in plain text is very dangerous. One can attempt to obfuscate the data by means of a symmetric algorithm, but it can be always reverse-engineered from the application.

On Windows there is a safer alternative - DPAPI (Data Protection API). The idea is that sensitive data can be encoded using current user's credentials. This means, that the data can be stored safely in an encrypted form and will not be compromised until it can be assumed, that the user's credentials have been not compromised. The API is also accessible in .NET. A simple example, copied from the relevant section of MSDN will demonstrate the principle:

  1. using System;
  2. using System.Security.Cryptography;
  3.  
  4. public class DataProtectionSample
  5. {
  6. 	// Create byte array for additional entropy when using Protect method.
  7. 	static byte [] s_aditionalEntropy = { 9, 8, 7, 6, 5 };
  8.  
  9. 	public static void Main()
  10. 	{
  11. 		// Create a simple byte array containing data to be encrypted.
  12. 		byte [] secret = { 0, 1, 2, 3, 4, 1, 2, 3, 4 };
  13.  
  14. 		//Encrypt the data.
  15. 		byte [] encryptedSecret = Protect( secret );
  16. 		Console.WriteLine("The encrypted byte array is:");
  17. 		PrintValues(encryptedSecret);
  18.  
  19. 		// Decrypt the data and store in a byte array.
  20. 		byte [] originalData = Unprotect( encryptedSecret );
  21. 		Console.WriteLine("{0}The original data is:", Environment.NewLine);
  22. 		PrintValues(originalData);
  23. 	}
  24.  
  25. 	public static byte [] Protect( byte [] data )
  26. 	{
  27. 		try
  28. 		{
  29. 			// Encrypt the data using DataProtectionScope.CurrentUser. The result can be decrypted
  30. 			//  only by the same current user.
  31. 			return ProtectedData.Protect( data, s_aditionalEntropy, DataProtectionScope.CurrentUser );
  32. 		} 
  33. 		catch (CryptographicException e)
  34. 		{
  35. 			Console.WriteLine("Data was not encrypted. An error occurred.");
  36. 			Console.WriteLine(e.ToString());
  37. 			return null;
  38. 		}
  39. 	}
  40.  
  41. 	public static byte [] Unprotect( byte [] data )
  42. 	{
  43. 		try
  44. 		{
  45. 			//Decrypt the data using DataProtectionScope.CurrentUser.
  46. 			return ProtectedData.Unprotect( data, s_aditionalEntropy, DataProtectionScope.CurrentUser );
  47. 		} 
  48. 		catch (CryptographicException e)
  49. 		{
  50. 			Console.WriteLine("Data was not decrypted. An error occurred.");
  51. 			Console.WriteLine(e.ToString());
  52. 			return null;
  53. 		}
  54. 	}
  55.  
  56. 	public static void PrintValues( Byte[] myArr )  
  57. 	{
  58. 	        foreach ( Byte i in myArr )  
  59. 	  	{
  60. 		         Console.Write( "\t{0}", i );
  61. 		 }
  62.                  Console.WriteLine();
  63. 	 }
  64. }

Add new comment

Subscribe to Comments for "Secure storage of passwords in .NET"