C#中的RSACryptoServiceProvider

C#中的RSACryptoServiceProvider

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

问题描述

我建立了多个客户端之间的安全通信的应用程序。为了做到这一点,我加密与AES的消息,然后我加密AES密钥与收件人的RSA公钥,然后我送这两个组件(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.

现在我遇到了一个问题,我不知道什么是最好的做法:为了持续1 participent的私钥和公钥,我需要存储的密钥对。地方保存为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生成一个新的密钥,如果容器不存在 - 瞒着左右。但我需要知道,如果有一个previously存储密钥对现有或创建一个新的。

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?Thanks for your help on that!

问候,罗布

推荐答案

试试这个:

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

similiar问题:How检查是否在.NET

这篇关于C#中的RSACryptoServiceProvider:如何检查是否一个键已经存在于容器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-07 06:36