如何存储在机器级RSA密钥容器的公共密钥 [英] How to store a public key in a machine-level RSA key container

查看:493
本文介绍了如何存储在机器级RSA密钥容器的公共密钥的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有只存储一个公共/私有密钥对中的公共密钥时使用机器级RSA密钥容器中的问题。



下面的代码创建一个公共/私人对并提取从对的公钥。一对和公钥被存储在单独的密钥的容器中。钥匙,然后从在这一点上,他们应该是一样进入容器内的键的键容器内得到的。



代码工作时 CspProviderFlags.UseDefaultKeyContainer CspParameters.Flags 规定(即从公钥的容器是相同的读取密钥退了出去),但是当 CspProviderFlags.UseMachineKeyStore CspParameters.Flags 指定读回键的公钥是不同的。



为什么行为不同,什么做我需要做不同的检索机器级RSA密钥容器公钥?

  VAR publicPrivateRsa =新的RSACryptoServiceProvider(新CspParameters()
{
KeyContainerName =PublicPrivateKey,
标志= CspProviderFlags.UseMachineKeyStore
//标志= CspProviderFlags.UseDefaultKeyContainer
}

{
PersistKeyInCsp = TRUE,

};

变种publicRsa =新的RSACryptoServiceProvider(新CspParameters()
{
KeyContainerName =公钥,
标志= CspProviderFlags.UseMachineKeyStore
//标志= CspProviderFlags.UseDefaultKeyContainer
}

{
PersistKeyInCsp = TRUE
};


//导出密钥。
publicRsa.ImportParameters(publicPrivateRsa.ExportParameters(假));


Console.WriteLine(publicRsa.ToXmlString(假));
Console.WriteLine(publicPrivateRsa.ToXmlString(假));

//处置这两个电信运营商。
使用(publicRsa)
{
publicRsa.Clear();
}使用
(publicPrivateRsa)
{
publicRsa.Clear();
}

publicPrivateRsa =新的RSACryptoServiceProvider(新CspParameters()
{
KeyContainerName =PublicPrivateKey,
标志= CspProviderFlags.UseMachineKeyStore
//标志= CspProviderFlags.UseDefaultKeyContainer
}
);


publicRsa =新的RSACryptoServiceProvider(新CspParameters()
{
KeyContainerName =公钥,
标志= CspProviderFlags.UseMachineKeyStore
/ /标志= CspProviderFlags.UseDefaultKeyContainer
}
);

Console.WriteLine(publicRsa.ToXmlString(假));
Console.WriteLine(publicPrivateRsa.ToXmlString(假));使用


(publicRsa)
{
publicRsa.Clear();
}使用
(publicPrivateRsa)
{
publicRsa.Clear();
}


解决方案

看来,关键的容器不适用于这个目的(这是由隐含的如何:存储对称密钥在密钥容器,从.NET Framework开发人员指南,并通过的 MSDN上一个disccusion)。



其他机制,如存储在XML文件中的关键,需要使用以实现这一目标。


I'm having a problem using a machine level RSA key container when storing only the public key of a public/private key pair.

The following code creates a public/private pair and extracts the public key from that pair. The pair and the public key are stored in separate key containers. The keys are then obtained from those key containers at which point they should be the same as the keys going into the containers.

The code works when CspProviderFlags.UseDefaultKeyContainer is specified for CspParameters.Flags (i.e. the key read back out from the PublicKey container is the same), but when CspProviderFlags.UseMachineKeyStore is specified for CspParameters.Flags the key read back from PublicKey is different.

Why is the behaviour different, and what do I need to do differently to retrieve the public key from a machine-level RSA key container?

var publicPrivateRsa = new RSACryptoServiceProvider(new CspParameters()
{
    KeyContainerName = "PublicPrivateKey",
    Flags = CspProviderFlags.UseMachineKeyStore
    //Flags = CspProviderFlags.UseDefaultKeyContainer
}
    )
{
    PersistKeyInCsp = true,

};

var publicRsa = new RSACryptoServiceProvider(new CspParameters()
{
    KeyContainerName = "PublicKey",
    Flags = CspProviderFlags.UseMachineKeyStore
    //Flags = CspProviderFlags.UseDefaultKeyContainer
}
    )
{
    PersistKeyInCsp = true
};


//Export the key.
publicRsa.ImportParameters(publicPrivateRsa.ExportParameters(false));


Console.WriteLine(publicRsa.ToXmlString(false));
Console.WriteLine(publicPrivateRsa.ToXmlString(false));

//Dispose those two CSPs.
using (publicRsa)
{
    publicRsa.Clear();
}
using (publicPrivateRsa)
{
    publicRsa.Clear();
}

publicPrivateRsa = new RSACryptoServiceProvider(new CspParameters()
{
    KeyContainerName = "PublicPrivateKey",
    Flags = CspProviderFlags.UseMachineKeyStore
    //Flags = CspProviderFlags.UseDefaultKeyContainer
}
    );


publicRsa = new RSACryptoServiceProvider(new CspParameters()
{
    KeyContainerName = "PublicKey",
    Flags = CspProviderFlags.UseMachineKeyStore
    //Flags = CspProviderFlags.UseDefaultKeyContainer
}
    );

Console.WriteLine(publicRsa.ToXmlString(false));
Console.WriteLine(publicPrivateRsa.ToXmlString(false));


using (publicRsa)
{
    publicRsa.Clear();
}
using (publicPrivateRsa)
{
    publicRsa.Clear();
}

解决方案

It seems that key containers are not intended for this purpose (this is implied by "How to: Store Asymmetric Keys in a Key Container" from the .NET Framework Developer's Guide, and confirmed by a disccusion on MSDN).

Other mechanisms, such as storing the key in an XML file, need to be used to achieve this goal.

这篇关于如何存储在机器级RSA密钥容器的公共密钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆