如何检查容器中是否已存在密钥? [英] How to check if a key already exists in container?

查看:155
本文介绍了如何检查容器中是否已存在密钥?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个用于在多个客户端之间进行安全消息传递的应用程序.为了实现这一目标,我先用AES加密消息,然后再使用收件人的公共RSA密钥加密AES密钥,然后将这两个组件(RSA加密的AES密钥和AES加密的消息)发送给收件人.整个过程运行良好,没有任何错误.

I'm building an application for secure messaging between multiple clients. In order to achieve that, I encrypt the message with AES, then I encrypt the AES key with the recipients public RSA key, then I send these two components (RSA-encrypted AES key and AES-encrypted message) to the recipient. The whole process works well and without any errors.

现在我遇到了一个问题,我想知道什么是最佳实践:为了保留一个参与者的私钥和公钥,我需要存储密钥对.可以将其另存为XML文件,但这显然不是一种选择.因此决定使用密钥容器,如此处所述所述.实现容器的使用非常容易,但是我怎么能

Now I ran into a problem and I'm wondering what would be the best practice: In order to persist the private and public key of one participent, I need to store the key pair. Saving it somewhere as an XML file is possible, but obviously not an option. So decided to use a key container as described here. Implementing the use of the container is quite easy, but how can I

  1. 检查指定的容器是否已经存在?
  2. 检查密钥大小是否匹配给定值?

据我所知这是不可能的,因为如果容器不存在,则RSACryptoServiceProvider会生成一个新密钥-无需告知.但是我需要知道是否存在以前存储的密钥对,或者是否创建了新的密钥对.

As far as I can see this is not possible, because the RSACryptoServiceProvider generates a new key if the container does not exist - without telling so. But I need to know if there is a previously stored key pair available or if a new one is created.

我该如何解决?还是这是完全错误的方法?

How can I fix that? Or is this a completely wrong approach?

推荐答案

尝试一下:

    public static bool DoesKeyExists(string containerName)
    {
        var cspParams = new CspParameters
        {
            Flags = CspProviderFlags.UseExistingKey,
            KeyContainerName = containerName
        };

        try
        {
            var provider = new RSACryptoServiceProvider(cspParams);
        }
        catch (Exception e)
        {
            return false;
        }
        return true;
    }

类似的问题:如何检查.NET中是否存在RSA密钥容器

这篇关于如何检查容器中是否已存在密钥?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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