Skip to content

Instantly share code, notes, and snippets.

@njmube
Forked from draganjovanovic1/StrongNameKeyManager.cs
Last active August 29, 2015 14:23
Show Gist options
  • Save njmube/7d617b3b533aeabfaedc to your computer and use it in GitHub Desktop.
Save njmube/7d617b3b533aeabfaedc to your computer and use it in GitHub Desktop.
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