Skip to content

Instantly share code, notes, and snippets.

@duncansmart
Created March 28, 2013 23:22
Show Gist options
  • Save duncansmart/5267625 to your computer and use it in GitHub Desktop.
Save duncansmart/5267625 to your computer and use it in GitHub Desktop.
Backup current user's EFS Certificates (including private keys)
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
class CertDoodle
{
static void BackupEfsCerts()
{
X509Store store = new X509Store(StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadOnly);
//Encrypting File System (1.3.6.1.4.1.311.10.3.4)
var certs = from cert in store.Certificates.Cast<X509Certificate2>()
from ext in cert.Extensions.OfType<X509EnhancedKeyUsageExtension>()
from usage in ext.EnhancedKeyUsages.Cast<Oid>()
where usage.FriendlyName == "Encrypting File System"
select cert;
foreach (var cert in certs)
{
Debug.WriteLine(cert.Subject);
File.WriteAllBytes(@"C:\temp\efs-" + cert.Subject + ".pfx", cert.Export(X509ContentType.Pkcs12, "p@55w0rd"));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment