有没有办法从Microsoft \ Crypto \ RSA \ MachineKeys文件夹导入私钥? [英] Is there a way to import private keys from the Microsoft\Crypto\RSA\MachineKeys folder?
问题描述
我正在寻找与私钥文件夹进行交互的方法(本质上是找到一个私钥以尝试与公钥配对,因为公钥尚未与之配对)certutil -repairstore功能基本上就是我正在寻找-我相信能够使用代码来迭代私钥将帮助我实现这一目标
I'm looking to interact with the private keys folder (essentially to find a private key to try to pair to a public key, given that a public key isn't paired with one already ) certutil -repairstore functionality is basically what I'm looking for - I believe being able to use code to iterate over the private keys would help me achieve this
仅尝试从这些文件中读取所有字节并将它们作为Cspblob导入RSACryptoProvider中是行不通的.给出提供程序的错误版本"错误.
Simply trying to read all bytes from these files and import them as a Cspblob into a RSACryptoProvider does not work. Gives a "bad version of provider" error.
startInfo.FileName = "cmd.exe";
startInfo.Arguments = @"/C certutil -repairstore my " + selectedCertificate.Thumbprint;
startInfo.Verb = "runas";
这是我当前的变通方法命令行用法,但我不喜欢在可能的情况下使用命令行-这不是我可以使用或操作的对象,它具有与我要结束的功能相似的功能做.
Is my current work-around command line usage, but I don't prefer to use the command line where possible - This isn't an object I can use or manipulate, it just has similar functionality to what I want to end up doing.
推荐答案
下面是小的测试代码段.在偏移量0x28处,有一个容器名称可用于加载Key参数.我找不到有关私钥文件格式的任何文档,因此可能无法在所有情况下都起作用.
Below is the small test code snippet. At offset 0x28 there's a container name that can be used to load Key parameters. I couldn't find any documentation regarding private key file format so it might not work in all cases.
public static RSAParameters LoadParametersFromFile(string fileName)
{
int provType = 1;
string provName = "Microsoft Enhanced Cryptographic Provider v1.0"
// Load key container name;
StringBuilder containerName = new StringBuilder();
using (var keyFile = File.OpenRead(fileName))
{
keyFile.Position = 0x28;
int c;
while ((c = keyFile.ReadByte()) != 0 && c !=-1) containerName.Append((char) c);
}
CspParameters csp = new CspParameters(provType, provName);
csp.Flags = CspProviderFlags.UseMachineKeyStore; // set it accordingly
csp.KeyContainerName = containerName.ToString();
using (RSACryptoServiceProvider rsaKey = new RSACryptoServiceProvider(csp))
{
RSAParameters loadedParams = rsaKey.ExportParameters(false);
return loadedParams;
}
}
测试电话:
var rsaParams = LoadParametersFromFile(@"C:\ProgramData\Microsoft\Crypto\RSA\MachineKeys\0034dc1b91df7f7d75df774fa568bc73_ba648dc7-2ead-41db-8cde-e6f84e3fb1cc");
rsaParams.Modulus 将包含公钥.
这篇关于有没有办法从Microsoft \ Crypto \ RSA \ MachineKeys文件夹导入私钥?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!