Last active
August 29, 2015 14:01
-
-
Save draganjovanovic1/da19b5d0aa82c1c5085e to your computer and use it in GitHub Desktop.
StrongNameKeyManager
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
using System.Security.Cryptography; | |
using System.Security.Cryptography.X509Certificates; | |
namespace CryptoPlayground.StrongNameKeyManagement | |
{ | |
public class StrongNameKeyManager | |
{ | |
public StrongNameKeyManager(string containerName, bool machineScope = true) | |
{ | |
if (containerName == null) | |
throw new ArgumentNullException("containerName"); | |
_containerName = containerName; | |
_machineScope = machineScope; | |
} | |
public StrongNameKeyManager CreateKeyContainer(KeySize keySize = KeySize.L1024) | |
{ | |
var cspParameters = GetCspParametres(_containerName, _machineScope); | |
var csp = new RSACryptoServiceProvider((int)keySize, cspParameters) | |
{ | |
PersistKeyInCsp = true | |
}; | |
return this; | |
} | |
public byte[] ExportPublicKeyFromContainer() | |
{ | |
var cspParameters = GetCspParametres(_containerName, _machineScope); | |
var csp = new RSACryptoServiceProvider(cspParameters) | |
{ | |
PersistKeyInCsp = true | |
}; | |
return csp.ExportCspBlob(false); | |
} | |
public StrongNameKeyManager DeleteKeyContainer() | |
{ | |
var cspParameters = GetCspParametres(_containerName, _machineScope); | |
var csp = new RSACryptoServiceProvider(cspParameters) | |
{ | |
PersistKeyInCsp = false | |
}; | |
csp.Clear(); | |
return this; | |
} | |
public StrongNameKeyManager ImportKeyStrongNameKeyFile(string fileName) | |
{ | |
var cspParameters = GetCspParametres(_containerName, _machineScope); | |
var csp = new RSACryptoServiceProvider(cspParameters) | |
{ | |
PersistKeyInCsp = true | |
}; | |
var fileContent = File.ReadAllBytes(fileName); | |
csp.ImportCspBlob(fileContent); | |
return this; | |
} | |
public StrongNameKeyManager ImportFromPersonalInformationExchangeFile(string fileName, string password) | |
{ | |
var cspParameters = GetCspParametres(_containerName, _machineScope); | |
var csp = new RSACryptoServiceProvider(cspParameters) | |
{ | |
PersistKeyInCsp = true | |
}; | |
var fileContent = GetStrongNameKeyPairFromPfx(fileName, password); | |
csp.ImportCspBlob(fileContent); | |
return this; | |
} | |
private CspParameters GetCspParametres(string containerName, bool machineScope) | |
{ | |
var cspParametres = new CspParameters | |
{ | |
KeyContainerName = containerName, | |
KeyNumber = (int)KeyNumber.Signature, | |
Flags = CspProviderFlags.UseNonExportableKey | |
}; | |
if (machineScope) | |
cspParametres.Flags |= CspProviderFlags.UseMachineKeyStore; | |
return cspParametres; | |
} | |
private byte[] GetStrongNameKeyPairFromPfx(string pfxFile, string password) | |
{ | |
var certs = new X509Certificate2Collection(); | |
certs.Import(pfxFile, password, X509KeyStorageFlags.Exportable); | |
if (certs.Count == 0) | |
throw new ArgumentException(null, "pfxFile"); | |
var provider = certs[0].PrivateKey as RSACryptoServiceProvider; | |
if (provider == null) // not a good pfx file | |
throw new ArgumentException(null, "pfxFile"); | |
return provider.ExportCspBlob(true); | |
} | |
private readonly string _containerName; | |
private readonly bool _machineScope; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment