Advertisement
BurningBunny

Enumerate Private Key Containers Store C#

Apr 8th, 2014
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.18 KB | None | 0 0
  1. public void EnumerateKeyContainers()
  2. {
  3.     string appDataPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
  4.     string programDataPath = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
  5.    
  6.     //string userStore = Path.Combine(appDataPath,"Microsoft\\Crypto\\RSA");
  7.     string machineStore = Path.Combine(programDataPath,"Application Data\\Microsoft\\Crypto\\RSA\\MachineKeys");
  8.    
  9.     string[] files = Directory.GetFiles(machineStore);
  10.     foreach (var file in files)
  11.     {
  12.         try
  13.         {
  14.             byte[] container = File.ReadAllBytes(file);
  15.             string containerName = Encoding.UTF8.GetString(container, 40, container[8] - 1);
  16.            
  17.             CspParameters cspp = new CspParameters();
  18.             cspp.KeyContainerName = containerName;
  19.             cspp.Flags |= CspProviderFlags.UseMachineKeyStore;
  20.             RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(cspp);
  21.             rsa.PersistKeyInCsp = true;
  22.            
  23.             string outputFileName = containerName + ".txt";
  24.             using(StreamWriter streamWriter = new StreamWriter(Path.GetFullPath(outputFileName),false))
  25.             {
  26.                 streamWriter.Write(rsa.ToXmlString(!rsa.PublicOnly));
  27.                 streamWriter.Flush();
  28.             }
  29.             rsa.Clear();                   
  30.         }
  31.         catch { }
  32.     }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement