创建具有本机C#的ECC公钥/私钥 [英] Creating an ECC Private/Public key with native C#

查看:2026
本文介绍了创建具有本机C#的ECC公钥/私钥的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在四处寻找如何生成一个公共和私人ECC密钥对谷歌和微软的加密API小时。在 ECDiffieHellmanCng 类(的 http://msdn.microsoft.com/en-us/library/system.security.cryptography.ecdiffiehellmancng.aspx#Y3081 )列出了一个例子,但我不知道如何访问直接的私钥。

I've been looking around for hours on Google and Microsoft's Crypto API on how to generate a public and private ECC key pair. The ECDiffieHellmanCng Class (http://msdn.microsoft.com/en-us/library/system.security.cryptography.ecdiffiehellmancng.aspx#Y3081) lists an example but I don't know how to access the private key directly.

有关该计划的一些背景,这对管理TrueCrypt的会议,AES预共享密钥加密和ECDH / AES加密一个C#控制台应用程序。我需要一个函数来仅仅创建一个公共/私有密钥对保存的文件,然后如何使用在包裹的AES加密文件那些生成的密钥文件(而不是在运行时产生它像例子显示)。另外,我知道了,与文件保存到硬盘但是我并不担心这个计划,我考虑到2的客户端计算机是安全出现的所有漏洞。

For a little background on the program, it's a C# console app for managing TrueCrypt sessions, AES pre-shared key encryption, and ECDH/AES encryption. I need a function to merely create a public/private key pair to save to files, then documentation of how to use those generated keys in a wrapped AES encrypted file (instead of generating it at runtime like the example shows). Also I am aware off all the vulnerabilities that arise with saving files to the harddrive however I'm not worried about that with this program and I'm considering the 2 client computers to be secure.

另外请注意,我不希望使用BouncyCastle的API。

Also note, I don't want to use the BouncyCastle API.

推荐答案

我相信这是你正在找?如果是,请标记为(我的第一次!)答案: - )

I believe this is what you are looking for? If yes, please mark as (my first!) answer :-)

其中大部分涉及到CngKey的,可以在许多变化中使用

Most of this related to the CngKey, that can be used in many variations.

    static void test1()
    {
        CngKey k;

        if (CngKey.Exists("myECDH", CngProvider.MicrosoftSoftwareKeyStorageProvider, CngKeyOpenOptions.MachineKey))
        {
            k = CngKey.Open("myECDH", CngProvider.MicrosoftSoftwareKeyStorageProvider, CngKeyOpenOptions.MachineKey);
        }
        else
        {
            k = CngKey.Create(CngAlgorithm.ECDiffieHellmanP256, "myECDH", new CngKeyCreationParameters
        {
            ExportPolicy = CngExportPolicies.AllowPlaintextExport,
            KeyCreationOptions = CngKeyCreationOptions.MachineKey,
            KeyUsage = CngKeyUsages.AllUsages,
            Provider = CngProvider.MicrosoftSoftwareKeyStorageProvider,
            UIPolicy = new CngUIPolicy(CngUIProtectionLevels.None)
        });
        }

        byte[] privateBytes = k.Export(CngKeyBlobFormat.EccPrivateBlob);
        byte[] publicBytes = k.Export(CngKeyBlobFormat.EccPublicBlob);

        //This:
        var privateTester1 =
            new ECDiffieHellmanCng(CngKey.Import(privateBytes, CngKeyBlobFormat.EccPrivateBlob,
                CngProvider.MicrosoftSoftwareKeyStorageProvider));

        //Or that:
        var privateTester2 = new ECDiffieHellmanCng(k);
    }

这篇关于创建具有本机C#的ECC公钥/私钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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