Is there a way to import private keys from the Microsoft\Crypto\RSA\MachineKeys folder?

前端 未结 1 914
太阳男子
太阳男子 2021-01-27 11:59

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 )

相关标签:
1条回答
  • 2021-01-27 12:28

    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;
        }
    }
    

    Test call:

    var rsaParams =  LoadParametersFromFile(@"C:\ProgramData\Microsoft\Crypto\RSA\MachineKeys\0034dc1b91df7f7d75df774fa568bc73_ba648dc7-2ead-41db-8cde-e6f84e3fb1cc");
    

    rsaParams.Modulus will contain the public key.

    0 讨论(0)
提交回复
热议问题