迪菲-赫尔曼密钥交换(Diffie–Hellmankey exchange,简称“D–H”) 是一种安全协议。
它可以让双方在完全没有对方任何预先信息的条件下通过不安全信道建立起一个密钥。这个密钥可以在后续的通讯中作为对称密钥来加密通讯内容。

(1)、算法描述

离散对数的概念:

原根:如果a是素数p的一个原根,那么数值:

amod pa^2 modp,…,a^(p-1) modp

是各不相同的整数,且以某种排列方式组成了从1p-1 的所有整数。

离散对数:如果对于一个整数b和素数p的一个原根a,可以找到一个唯一的指数 i,使得:

b =(a的i次方) modp                其中0≦ i ≦p-1

那么指数i 称为b的以a为基数的模p的离散对数。

Diffie-Hellman 算法的有效性依赖于计算离散对数的难度,其含义是:当已知大素数p和它的一个原根a后,对给定的 b,要计算 i ,被认为是很困难的,而给定 i 计算b 却相对容易。

Diffie-Hellman算法:

假如用户A和用户B希望交换一个密钥。

取素数p和整数aap的一个原根,公开a和p。

A选择随机数XA<p,并计算YA=a^XA modp。

B选择随机数XB<p,并计算YB=a^XB modp。

每一方都将X保密而将Y公开让另一方得到。

A计算密钥的方式是:K=(YB)^XA modp

B计算密钥的方式是:K=(YA)^XB modp

证明:

            (YB)^ XA mod p = (a^XB modp)^XA mod p

                    = (a^XB)^XA mod p = (a^XA)^XB mod p   (<-- 密钥即为 a^(XA*XB) mod p)

=(a^XA modp)^XB mod p = (YA) ^XBmod p

由于XA和XB是保密的,而第三方只有pa、YB、YA可以利用,只有通过取离散对数来确定密钥,但对于大的素数p,计算离散对数是十分困难的。

例子:

假如用户Alice和用户Bob希望交换一个密钥。

取一个素数p =97和97的一个原根a=5。

Alice和Bob分别选择秘密密钥XA=36和XB=58,并计算各自的公开密钥:

YA=a^XAmod p=5^36 mod 97=50

YB=a^XBmod p=5^58 mod 97=44

Alice和Bob交换了公开密钥之后,计算共享密钥如下:

Alice:K=(YB) ^XA mod p=44^36 mod 97=75

Bob:K=(YA) ^XB mod p=50^58 mod 97=75 

(2)、安全性

当然,为了使这个例子变得安全,必须使用非常大的XA, XB以及p, 否则可以实验所有的可能取值。(总共有最多97个这样的值, 就算XA和XB很大也无济于事)。
如果 p 是一个至少 300 位的质数,并且XA和XB至少有100位长, 那么即使使用全人类所有的计算资源和当今最好的算法也不可能从a, p和a^(XA*XB) mod p 中计算出 XA*XB。
这个问题就是著名的离散对数问题。注意g则不需要很大, 并且在一般的实践中通常是2或者5。
在最初的描述中,迪菲-赫尔曼密钥交换本身并没有提供通讯双方的身份验证服务,因此它很容易受到中间人攻击。 
一个中间人在信道的中央进行两次迪菲-赫尔曼密钥交换,一次和Alice另一次和Bob,就能够成功的向Alice假装自己是Bob,反之亦然。
而攻击者可以解密(读取和存储)任何一个人的信息并重新加密信息,然后传递给另一个人。因此通常都需要一个能够验证通讯双方身份的机制来防止这类攻击。
有很多种安全身份验证解决方案使用到了迪菲-赫尔曼密钥交换。例如当Alice和Bob共有一个公钥基础设施时,他们可以将他们的返回密钥进行签名。

DH算法用到的基础知识

原根

设m是正整数,a是整数,若a模m的阶等于φ(m),则称a为模m的一个原根。(其中φ(m)表示m的欧拉函数)

假设一个数g对于P来说是原根,那么g^i mod P的结果两两不同,且有 1<g<P, 0<i<P,那么g可以称为是P的一个原根,归根到底就是g^(P-1) = 1 (mod P)当且仅当指数为P-1的时候成立.(这里P是素数).

简单来说,g^i mod p ≠ g^j mod p (p为素数)

其中i≠j且i, j介於1至(p-1)之间

则g为p的原根。

求原根目前的做法只能是从2开始枚举,然后暴力判断g^(P-1) = 1 (mod P)是否当且当指数为P-1的时候成立

而由于原根一般都不大,所以可以暴力得到.

欧拉函数

在数论,对正整数n,欧拉函数是少于或等于n的数中与n互质的数的数目。此函数以其首名研究者欧拉命名,它又称为Euler's totient function、φ函数、欧拉商数等。例如φ(8)=4,因为1,3,5,7均和8互质。从欧拉函数引伸出来在环论方面的事实和拉格朗日定理构成了欧拉定理的证明。

φ函数的值 通式:φ(x)=x(1-1/p1)(1-1/p2)(1-1/p3)(1-1/p4)…..(1-1/pn),其中p1,p2……pn为x的所有质因数,x是不为0的整数。φ(1)=1(唯一和1互质的数(小于等于1)就是1本身)。 (注意:每种质因数只一个。比如12=2*2*3那么φ(12)=12*(1-1/2)*(1-1/3)=4

若n是质数p的k次幂,φ(n)=p^k-p^(k-1)=(p-1)p^(k-1),因为除了p的倍数外,其他数都跟n互质。

设n为正整数,以 φ(n)表示不超过n且与n互

素的正整数的个数,称为n的欧拉函数值,这里函数

φ:N→N,n→φ(n)称为欧拉函数。

欧拉函数是积性函数——若m,n互质,φ(mn)=φ(m)φ(n)。

特殊性质:当n为奇数时,φ(2n)=φ(n),证明与上述类似。

Description( from wikipedia)

Illustration of the Diffie–Hellman Key Exchange

Diffie–Hellman establishes ashared secret that can be used for secret communications while exchanging dataover a public network. The following diagram illustrates the general idea ofthe key exchange by using colors instead of a very large number. The crucialpart of the process is that Alice andBob exchangetheir secret colors in a mix only. Finally this generates an identical key thatis computationally difficult (impossible for modernsupercomputers to do in a reasonable amount of time) toreverse for another party that might have been listening in on them. Alice andBob now use this common secret to encrypt and decrypt their sent and receiveddata. The starting color (yellow) is arbitrary, but is agreed on in advance byAlice and Bob, and does not need to be secret.

Cryptographic explanation

The simplest and the originalimplementation of the protocol uses the multiplicative group of integersmodulo p, where p is prime, and g is a primitive root modulo p. Here is an example of theprotocol, with non-secret values in blue, andsecret values in red.

1.  Alice andBob agree touse a prime number p = 23 and base g = 5 (which is a primitive root modulo 23).

2.  Alice chooses a secret integer a = 6, thensends Bob A = ga mod p

·        A = 56 mod 23 = 8

3.  Bob chooses a secret integer b = 15, thensends Alice B = gb mod p

·        B = 515 mod 23 = 19

4.  Alice computes s = Ba mod p

·        s = 196 mod 23 = 2

5.  Bob computes s = Ab mod p

·        s = 815 mod 23 = 2

6.  Alice and Bob now share a secret (the number 2).

Both Alice and Bob have arrivedat the same value, because (ga)b (for Bob, 815 mod 23 = (ga mod p)b mod p = (ga)b mod p)and (gb)a are equal mod p. Note that only ab, and (gab mod p = gba mod p) are kept secret. All the other values– pgga modp, and gb mod p – are sent in the clear. Once Aliceand Bob compute the shared secret they can use it as an encryption key, knownonly to them, for sending messages across the same open communications channel.

Of course, much larger values of ab, and p would be needed to make this examplesecure, since there are only 23 possible results of n mod 23. However, if p is a prime of at least 300 digits, and a and b are at least 100 digits long, theneven the fastest modern computers cannot find a given only gpgb mod p and ga mod p.The problem such a computer needs to solve is called the discrete logarithm problem. Thecomputation of ga mod p is known as modular exponentiation and can be done efficiently even for largenumbers. Note that g need not be large at all, and inpractice is usually a small prime (like 2, 3, 5...) because primitive rootsusually are quite numerous.

Generalization to finite cyclic groups

Here's a more general descriptionof the protocol,

1.  Alice and Bob agree on a finite cyclicgroup G and a generating element g in G.(This is usually done long before the rest of the protocol; g is assumed to be known by allattackers.) We will write the group G multiplicatively.

2.  Alice picks a random naturalnumber a and sends ga to Bob.

3.  Bob picks a random natural number b and sends gb to Alice.

4.  Alice computes (gb)a.

5.  Bob computes (ga)b.

Both Alice and Bob are now inpossession of the group element gab, whichcan serve as the shared secret key.

If m is a message, and an element of thegroup, then we can encrypt e = mgab. Then wecan decipher m from e as follows: We compute (gab)−1, using|G|:

Bob knows Gb, and ga. As g generates G it follows that g|G| = 1 (the group identity). Bob calculates (ga)|G|−b = ga(|G|−b) = ga|G|−ab = ga|G|gab = (g|G|)agab = 1agab = gab = (gab)−1.

When Alice sends Bob theencrypted message, e =mgab, Bobcomputes e(gab)−1 = mgab(gab)−1 = m(1)= m.

When the group is too large for amultiplication table, then a multiplication algorithm is needed, and Exponentiation for finite cyclic groups should be used.

Returning to the originalquestion of Diffie–Hellman key exchange, how can a key be extracted, ingeneral, from the shared group element gab? Onecould extract a set of AES keys from it. This would require a mappingfrom G to the set of n-bit binary strings,and the mapping would depend on G.For example, if G consisted of the powers of a fixednonsingular matrix A defined over a finite field, and if gab = {gi,j}, thenthe element g11 would be an element of the field and wouldhave a binary representation that could be divided up into AES keys.

Secrecy chart

The chart below depicts who knowswhat, again with non-secret values in blue, andsecret values in red. HereEve is aneavesdropper—she watches what is sent between Alice andBob, but she does not alter the contents of their communications.

·        g = public(prime) base, known to Alice, Bob, and Eve. g = 5

·        p = public(prime) number, known to Alice, Bob, and Eve. p = 23

·        a = Alice's private key, known only to Alice. a = 6

·        b = Bob's private key known only to Bob. b = 15

·        A = Alice'spublic key, known to Alice, Bob, and Eve. A = ga mod p = 8

·        B = Bob'spublic key, known to Alice, Bob, and Eve. B = gb mod p = 19

Alice

knows

doesn't know

p = 23

b = ?

base g = 5

a = 6

A = 5a mod 23

A = 56 mod 23 = 8

B = 19

s = Ba mod 23

s = 196 mod 23 = 2

s = 2

Bob

knows

doesn't know

p = 23

a = ?

base g = 5

b = 15

B = 5b mod 23

B = 515 mod 23 = 19

A = 8

s = Ab mod 23

s = 815 mod 23 = 2

s = 2

Eve

knows

doesn't know

p = 23

a = ?

base g = 5

b = ?

s = ?

A = 8

B = 19

s = 19a mod 23 = 8b mod 23

·        Now s = the shared secret key and it is known toboth Alice and Bob, but not to Eve. s = 2

Note: It should be difficult forAlice to solve for Bob's private key or for Bob to solve for Alice's privatekey. If it is not difficult for Alice to solve for Bob's private key (or viceversa), Eve may simply substitute her own private / public key pair, plug Bob'spublic key into her private key, produce a fake shared secret key, and solvefor Bob's private key (and use that to solve for the shared secret key. Eve mayattempt to choose a public / private key pair that will make it easy for her tosolve for Bob's private key). Another demonstration of Diffie-Hellman (alsousing numbers too small for practical use) is given here [7]

DH算法证明及相应的理论知识相关推荐

  1. dh算法 理论依据_DH算法原理

    DH 算法原理 DH 是 Diffie-Hellman 的首字母缩写, 是 Whitefield 与 Martin Hellman 在 1976 年提出了一个的密钥交换协议.我个人倾向于称 DH 算法 ...

  2. 北极熊优化算法PBO的理论知识以及python代码实现

    北极熊优化算法PBO的理论知识以及python代码实现 北极熊优化算法 1.条件假设 2.浮冰漂移 (全局搜索)策略 3.海豹捕捉 (局部搜索)策略 算法步骤 Python代码实现 参数设定 寻优函数 ...

  3. 灰狼优化算法GWO的理论知识以及python代码实现

    灰狼优化算法GWO的理论知识以及python代码实现 GWO 算法的等级结构 GWO 算法的数学模型 灰狼算法的python实现 寻优结果 下载链接 GWO 算法的等级结构 灰狼优化算法是模拟食物链顶 ...

  4. 【清风数模】 相关系数(理论知识+代码实操 保姆级笔记)

    相关系数 前言 我们将学习两种给最为常用的相关系数:pearson相关系数和spearman等级相关系数.它们可用来衡量两个变量之间的相关性的大小,根据数据满足的不同条件,我们要选择不同的相关系数进行 ...

  5. 用VC进行COM编程所必须掌握的理论知识

    用VC进行COM编程所必须掌握的理论知识 这篇文章是给初学者看的,尽量写得比较通俗易懂,并且尽量避免编程细节.完全是根据我自己的学习体会写的,其中若有技术上的错误之处,请大家多多指正. 一.为什么要用 ...

  6. oracle rac理论知识

    oracle数据库高可靠性高性能的特性是很多企业需要的,这些年一直给各大政府企业做oracle咨询与规划,实施安装以及维护,回头看看,自己已经忘记大部分oracle rac的整体具体架构理论知识,现在 ...

  7. 从理论知识到落地能力,你欠缺了什么?

    转载自   从理论知识到落地能力,你欠缺了什么? 本文是一个理论过度到实践的典型案例,借助程序员经常遇到的一个问题--网络为什么不通,来具体说明怎么将书本上的死知识真正变成我们解决问题的能力. 大学学 ...

  8. vrp 节约算法 c++_数据结构和算法(Golang实现)(8.1)基础知识-前言

    基础知识 学习数据结构和算法.我们要知道一些基础的知识. 一.什么是算法 算法(英文algorithm)这个词在中文里面博大精深,表示算账的方法,也可以表示运筹帷幄的计谋等.在计算机科技里,它表示什么 ...

  9. FFmpeg学习(音视频理论知识)

    文章目录 1. 音视频理论知识 1.1 基本概念 1.1.1 音视频必备的基本概念 常用的视频封装格式 常用的视频编码器 常用的音频编程器: 视频流 裸数据YUV 1.1.2 音视频常见处理 采集 处 ...

  10. 学习AlphaGo理论知识-----part two

    Mastering the game of Go without human knowledge 郑重声明:原文参见标题,文章来源转载,如有侵权,请联系CSDN发布者smartgridequation ...

最新文章

  1. 美权威报告:量子计算十年内无法落地
  2. [译]Stack View 自定义间隙
  3. linux 重定向 2gt;gt;,编写我自己的linux shell I / O重定向’gt;’函数
  4. birt脚本for循环语句_如何使用 for 循环语句嵌套方法!
  5. js 数字序号 转 字母序号
  6. 如何解决Xshell使用时中文字体是躺倒显示的问题
  7. Hibernate 教程(一)[Hibernate的基本配置]
  8. 设备指纹技术分析和应用分析
  9. Vivado 使用方法
  10. win7 计算机刷新dns,win7系统刷新DNS缓存的操作方法
  11. 单片机定时器一1ms12MHz_单片机原理及接口技术笔记1单片机概述
  12. 官宣:华为云学院带你看AI
  13. 硬盘内部传输速率和外部传输速率的区别
  14. Android第五次课→文件操作
  15. 解锁京东云底层技术密码:京东四大数智供应链技术亮相
  16. 青岛地铁不能让你的房子升值
  17. 【进程】进程间通信----消息队列
  18. 海思嵌入式开发-003-Hi3861烧录报错,找不到Hi3861_wifiiot_app_allinone.bin文件
  19. sed -i 命令使用
  20. 遥控51单片机控制42步进电机(TB6600驱动)控制方向、速度

热门文章

  1. 如何实现施耐德Twido系列PLC远程上下载
  2. VS2012配置WTL
  3. mysql进阶教程pdf_SQL进阶教程 (MICK著) 高清pdf完整版
  4. 教你怎么利用Matlab画散点图
  5. 3D视觉学习路线 + 路线规划
  6. 如何DIY一台属于你自己的电脑?
  7. Matlab遗传算法大纲
  8. Spring Cloud教程合集
  9. 内存的工作原理及时序介绍
  10. 2022软考中级软件设计师---易混淆知识点总结1