如何使用BouncyCastle的的的Diffie-Hellman在C#中? [英] How to use BouncyCastle's Diffie-Hellman in C#?

查看:713
本文介绍了如何使用BouncyCastle的的的Diffie-Hellman在C#中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个应用程序,会在手机和一台Windows PC之间交换数据,我想保护与一个Diffie-Hellman交换生成的密钥发送的数据。

I'm writing an app that'll exchange data between a phone and a Windows PC, and I want to protect the data sent with key generated with a Diffie-Hellman exchange.

我试图使用BouncyCastle的对于这一点,但对于C#实现几乎不存在的文件有我难住了。

I'm trying to use BouncyCastle for that, but the almost non-existant documentation for the C# implementation has me stumped.

我想知道是什么是:什么是用于生成DH密钥和接收对方的钥匙时计算共享密钥的工作流程? (我假设我可以把我的钥匙为一个字符串,我可以作为一个字符串对方的重点工作。)/方法我在C#中使用了哪些对象?

What I want to know is: what's the workflow for generating a DH key and computing a shared key when the other side's key is received? (I'm assuming I can send my key as a string and I can work with the other side's key as a string.) What objects/methods do I use in C# for that?

推荐答案

好吧,大量的试验后,我得到了它的工作。发布的情况下,其他人需要它的答案。

Alright, after a lot of trial, I got it working. Posting answer in case someone else needs it.

我假设读者(1)知道的Diffie-Hellman是什么,对(读的此处了解详细信息)和(2)已经导入BouncyCastle的通过的NuGet一个.NET项目。

I'll assume the reader (1) knows what Diffie-Hellman is and what it's useful for (read here for details) and (2) already imported Bouncycastle to a .NET project via NuGet.

导入你需要:

using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Generators;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Security;



如何生成g和p:

How to generate g and p:

public DHParameters GenerateParameters()
{
    var generator = new DHParametersGenerator();
    generator.Init(BitSize, DefaultPrimeProbability, new SecureRandom());
    return generator.GenerateParameters();
}



想获得g和p为字符串?

Wanna get g and p as strings?

public string GetG(DHParameters parameters)
    {
        return parameters.G.ToString();
    }

public string GetP(DHParameters parameters)
    {
        return parameters.P.ToString();
    }



如何产生,并答:

How to generate a and A:

public AsymmetricCipherKeyPair GenerateKeys(DHParameters parameters)
    {
    var keyGen = GeneratorUtilities.GetKeyPairGenerator("DH");
    var kgp = new DHKeyGenerationParameters(new SecureRandom(), parameters);
    keyGen.Init(kgp);
    return keyGen.GenerateKeyPair();
}



想读取和A作为一个字符串?

Wanna read a and A as a string?

// This returns A
public string GetPublicKey(AsymmetricCipherKeyPair keyPair)
{
    var dhPublicKeyParameters = _generatedKey.Public as DHPublicKeyParameters;
    if (dhPublicKeyParameters != null)
    {
        return dhPublicKeyParameters.Y.ToString();
    }
    throw new NullReferenceException("The key pair provided is not a valid DH keypair.");
}

// This returns a
public string GetPrivateKey(AsymmetricCipherKeyPair keyPair)
{
    var dhPrivateKeyParameters = _generatedKey.Private as DHPrivateKeyParameters;
    if (dhPrivateKeyParameters != null)
    {
        return dhPrivateKeyParameters.X.ToString();
    }
    throw new NullReferenceException("The key pair provided is not a valid DH keypair.");
}

要导入从字符串参数只是做:

To import the parameters from strings just do:

var importedParameters = new DHParameters(p, g);

要生成B和B只是使用 GenerateKeys() importedParameters ,而不是生成的参数

To generate b and B just use GenerateKeys() with importedParameters instead of the generated parameters.

比方说,你产生的b和b,并且已经得到了p,G和A.要计算共享的秘密:

Let's say you generated b and B and already got p, g and A. To compute the shared secret:

public BigInteger ComputeSharedSecret(string A, AsymmetricKeyParameter bPrivateKey, DHParameters internalParameters)
{
    var importedKey = new DHPublicKeyParameters(new BigInteger(A), internalParameters);
    var internalKeyAgree = AgreementUtilities.GetBasicAgreement("DH");
    internalKeyAgree.Init(bPrivateKey);
    return internalKeyAgree.CalculateAgreement(importedKey);
}



重复A和现在你有2个客户端之间共享的秘密,随时准备用来加密通信。

Repeat for A and now you have a shared secret between 2 clients, ready to be used to encrypt communications.

希望这是有益的。

这篇关于如何使用BouncyCastle的的的Diffie-Hellman在C#中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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