diffi-hellma(密钥交换算法)
2010-12-26 17:16
141 查看
一、原理(待续)
![](http://hi.csdn.net/attachment/201012/27/0_1293414903zXqW.gif)
二、过程
![](http://hi.csdn.net/attachment/201012/27/0_1293414887YVHc.gif)
![](http://hi.csdn.net/attachment/201012/27/0_12934148792S2O.gif)
注意这里我们并没有对其中的任何元素加以限制。所以这里我不是很理解。
为什么p要为素数?a为什么要为p的本原根?xA和xB为什么要小于p?
期待大牛的解答。
三、代码
求素数的本原根这里我不知道怎么实现。
之前取a,p在[INTAX,LLMAX]之间,
在求p的本原根a时碰到了难题,穷举判断复杂度太高了,n^2*log(n)
改成了[SHORTMAX,upperBound]也还是很慢。目前还未实现!
期待大牛的代码。
1.variable.h
2.main.cpp
3.get_a_p.h
4.millerRabin.h
原创转载请注明!
![](http://hi.csdn.net/attachment/201012/27/0_1293414903zXqW.gif)
二、过程
![](http://hi.csdn.net/attachment/201012/27/0_1293414887YVHc.gif)
![](http://hi.csdn.net/attachment/201012/27/0_12934148792S2O.gif)
注意这里我们并没有对其中的任何元素加以限制。所以这里我不是很理解。
为什么p要为素数?a为什么要为p的本原根?xA和xB为什么要小于p?
期待大牛的解答。
三、代码
求素数的本原根这里我不知道怎么实现。
之前取a,p在[INTAX,LLMAX]之间,
在求p的本原根a时碰到了难题,穷举判断复杂度太高了,n^2*log(n)
改成了[SHORTMAX,upperBound]也还是很慢。目前还未实现!
期待大牛的代码。
1.variable.h
#include < iostream > #include <cmath> #include <cstdlib> #include <ctime> using namespace std ; typedef unsigned __int64 ull; typedef unsigned int uint ; const uint upperBound = 1000000 ; //上界的设置真纠结!!! const unsigned short SHORTMAX = 0Xffff ; bool flag [ 10000000000 ] ;//10^9
2.main.cpp
#include "variable.h" #include "millerRabin.h" #include "get_a_p.h" #define getxA() ( rand ( ) % ( upperBound + 1 ) ) #define getxB() ( rand ( ) % ( upperBound + 1 ) ) #define getyA powerMod #define getyB powerMod #define getKeyA powerMod #define getKeyB powerMod int main ( ) { srand ( ( unsigned ) time ( NULL ) ) ; uint a , p , xA , yA , xB , yB , keyA , keyB ; consult_a_p ( a , p ) ;//a,p 属于[ushort ,upperBound ] //xA,xB[0,upperBound] xA = getxA ( ) ; xB = getxB ( ) ; //yA,yB[1,p-1] yA = getyA ( a , xA , p ) ; yB = getyB ( a , xB , p ) ; //kyeA,keyB[1,p-1] keyA = getKeyA ( yB , xA , p ) ; keyB = getKeyB ( yA , xB , p ) ; cout << "the diffiHellma algorithm is " ;//认证diffiHellma算法的正确性 if ( keyA == keyB ) cout << "right !" ; else cout << "wrong!" ; cout << endl ; return 0 ; }
3.get_a_p.h
bool isOk ( uint a , uint p ) { uint i ; for ( i = 1 ; i < p ; i ++ )//[1,p-1] flag [ i ] = false ; uint p0 ; for ( i = 1 ; i < p ; i ++ ) { p0 = powerMod ( a , i , p ) ; if ( flag [ p0 ] ) return false ; else flag [ p0 ] = true ; } return true ; } uint power ( uint a , uint p ) { if ( p == 0 ) return 1 ; uint k = 1 ; while ( p != 1 ) { if ( p & 1 ) k *= a ; a *= a ; p >>= 1 ; } return a * k ; } uint get_A_Possible_Value ( ) { uint p , a , mi ; do { //get a do { a = rand ( ) % ( upperBound -2 ) + 3 ;//[3,upperBound] } while ( ! millerRabin ( a ) ) ; //get mi const uint MAXOFMI = log ( upperBound ) / log ( a ) ;//p^mi<=upperBound mi = rand ( ) % MAXOFMI + 1 ;//[1,upperBound] //get p p = power ( a , mi ) ; } while ( p < SHORTMAX ) ; return p ; } ull gcd( ull a, ull b ){ ull t; while ( b ) { t = b ; b = a % b ; a = t ; } return a ; } void consult_a_p ( uint & a , uint & p ) { //2 , 4 , p^mi , 2*p^mi p为奇素数 ,p,a尽量取大一点;possible_Value ,使得它的每一个值都位于[ushort ,uint] uint possible_Value [ 2 ] ; possible_Value [ 0 ] = get_A_Possible_Value ( ) ; do { possible_Value [ 1 ] = get_A_Possible_Value () ; } while ( possible_Value [ 1 ] > upperBound / 2 ) ; possible_Value [ 1 ] *= 2 ; p = possible_Value [ rand ( ) % 2 ] ; //get p // get a //求出一个数的本原根求得我想死!有什么好的算法没啊?? a = p - 1 ; while ( gcd ( a , p ) != 1 || ! isOk ( a , p ) ) { cout << a << endl ; a -- ; } }
4.millerRabin.h
ull multiMod(ull a,ull b,ull m){//s=(a*b)(mod m) (n<2^62) // return ( (a%m) * (b%m) ) % m; // this wiull overflow! a %= m; b %= m; ull s = 0; while( b ) { if(b & 1) { s += a; if(s >= m) s -= m; } a <<= 1; b >>= 1;//a*b=(a*2)*(b/2) if(a >= m) a -= m; } return s; } ull powerMod ( ull a , ull p , ull n ) { ull k = 1 ; while ( p != 1 ) { if ( p & 1 ) { k = multiMod ( k , a , n ) ; } p >>= 1 ;//a^p=(a^2)^(p/2) a = multiMod ( a , a , n ) ; } return multiMod ( a , k , n ) ; } bool witness(ull a, ull n) { //test wether a^(n-1)%n=1 or not //n-1=2^k*q ull q = n - 1; int k = 0; while( ( q &1 ) == 0 ) { k ++ ; q >>= 1 ; } //a^(2^i*q)%n=1 or n-1 (-1<i<=k) ull y , x = powerMod(a, q, n); if (x == 1 || x == n-1) return false; /* while(k --) { x = x * x % n; if (x == n-1) return false;//素数的个数少数合数,先判断素数效率低一些! } return true; */ while( k -- ){ y = multiMod ( x , x , n ) ; if(y == 1 && x != 1 && x != n-1 ) return true;//素数的个数少数合数,先判断合数效率高一些! x = y; } if ( y != 1 ) return true; // composite return false; //may be prime } bool millerRabin(ull n, int T = 10) { // if (n == 2) return true; 取奇素数,因此n==2不要 if ((n&1) == 0 || n < 2) return false; while(T --) { ull a = rand() % (n-1) + 1;// a>=1 && a<=n-1 if (witness(a, n)) return false; } return true; }
原创转载请注明!
相关文章推荐
- DH密钥交换算法
- 扫盲 HTTPS 和 SSL/TLS 协议[3]:密钥交换(密钥协商)算法及其原理
- Diffie-Hellman密钥交换算法及其优化
- Nodejs进阶:使用DiffieHellman密钥交换算法
- 迪菲.赫尔曼(Diffie–Hellman)密钥交换算法
- [转载]非对称加密(RSA、DH密钥交换算法、数字签名)
- Diffie-Hellman 密钥交换算法
- 非对称加密(RSA、DH密钥交换算法、数字签名)
- Diffie-Hellman密钥交换算法及其优化
- pomelo学习笔记 (3) node.js 与 c 客户端 Diffie-Hellman 密钥交换算法的实现
- DH密钥交换算法
- DH算法(密钥交换算法)
- Diffie-Hellman密钥交换算法
- Diffle-Hellman密钥交换算法
- 非对称加密(RSA、DH密钥交换算法、数字签名)
- DH密钥交换(Diffie–Hellman key exchange)算法笔记
- Node.js如何使用Diffie-Hellman密钥交换算法详解
- 密钥交换算法DH(Java实现)
- DiffieHillman密钥交换算法 C/C++实现
- 非对称加密--密钥交换算法DHCoder