前言
网络安全中Diffie-Hellman的C++语言描述简单实现。
代码仓库
代码特点
纯C++语言:
- 相对规范和整洁
- 一定程度地面向对象
- 使用一部分高级特性
- 考虑优化性能
详细注释:
- 提示规范和整洁
- 提示面向对象
- 提示高级特性
- 提示优化性能
- 解析Diffie-Hellman步骤(网络上大部分实现代码的含义不明确,本代码相对明确)
- 注意易错点
代码
dh.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| #ifndef DH_DH_H_ #define DH_DH_H_
#include <iostream>
using std::cout; using std::endl;
class DH { public: DH(); unsigned int GetPrivateKey(); unsigned int GetPublicKey(const unsigned int &x); unsigned int GetKey(const unsigned int &y, const unsigned int &x);
private: unsigned int GetPrimeNum(); bool PrimalityTest(const unsigned int &n, const unsigned int &a); unsigned int QuickPowMod(const unsigned int &a, const unsigned int &q, const unsigned int &n); unsigned int QuickMulMod(const unsigned int &a, const unsigned int &b, const unsigned int &c); unsigned int GetPrimitiveRoot();
unsigned int p_arg_; unsigned int a_arg_; };
#endif
|
dh.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
| #include <ctime> #include <cstdlib>
#include "dh.h"
DH::DH() { unsigned int seed = time(nullptr); srand(seed);
this->p_arg_ = this->GetPrimeNum();
this->a_arg_ = this->GetPrimitiveRoot();
cout << "全局公开量:" << endl; cout << "参数p:\t" << this->p_arg_ << endl; cout << "参数a:\t" << this->a_arg_ << endl; cout << endl; }
unsigned int DH::GetPrivateKey() { unsigned int random = 0;
while (1) { random = rand();
if (random < this->p_arg_) { break; } }
return random; }
unsigned int DH::GetPublicKey(const unsigned int &x) { unsigned int y = this->QuickPowMod(this->a_arg_, x, this->p_arg_);
return y; }
unsigned int DH::GetKey(const unsigned int &y, const unsigned int &x) { unsigned int k = this->QuickPowMod(y, x, this->p_arg_);
return k; }
unsigned int DH::GetPrimeNum() { unsigned int random = 0; unsigned int random_odd = 0;
unsigned int n = 0; unsigned int a = 0; bool primality_test_res = false; bool prime_flag = false;
while (1) { random = rand();
if (random % 2 == 0) { random_odd = random + 1; } else { random_odd = random; }
n = random_odd;
for (int i = 0; i < 128; ++i) { a = rand() % (n - 1); if (a == 0) { a += 2; } if (a == 1) { ++a; }
primality_test_res = PrimalityTest(random_odd, a);
if (primality_test_res == true) { prime_flag = true; } else if (primality_test_res == false) { prime_flag = false;
break; } }
if (prime_flag == true) { break; } }
return random_odd; }
bool DH::PrimalityTest(const unsigned int &n, const unsigned int &a) { unsigned int k = 0; unsigned int q = n - 1;
while ((q & 1) == 0) { ++k; q >>= 1; }
unsigned int aq_mod_n = this->QuickPowMod(a, q, n);
if (aq_mod_n == 1) { return true; }
for (int j = 0; j < k; ++j) { aq_mod_n = this->QuickPowMod(aq_mod_n, 2, n);
if (aq_mod_n != 1 && aq_mod_n != n - 1) { return false; } }
return true; }
unsigned int DH::QuickPowMod(const unsigned int &a, const unsigned int &q, const unsigned int &n) { unsigned int res = 1; unsigned int a_temp = a; unsigned int q_temp = q;
while (q_temp > 0) { if ((q_temp & 1) == 1) { res = this->QuickMulMod(res, a_temp, n); }
a_temp = this->QuickMulMod(a_temp, a_temp, n);
q_temp >>= 1; }
return res; }
unsigned int DH::QuickMulMod(const unsigned int &a, const unsigned int &b, const unsigned int &c) { unsigned int res = 0; unsigned int a_temp = a; unsigned int b_temp = b;
while (b_temp > 0) { if (b_temp & 1) { res = (res + a_temp) % c; }
a_temp = (a_temp + a_temp) % c;
b_temp >>= 1; }
return res; }
unsigned int DH::GetPrimitiveRoot() {
unsigned int calcul_res = 0;
for (unsigned int i = 2; i < this->p_arg_; ++i) { for (unsigned int j = 2; j < this->p_arg_; ++j) { calcul_res = this->QuickPowMod(i, j, this->p_arg_);
if ((j != this->p_arg_ - 1) && (calcul_res == 1)) { break; }
if ((j == this->p_arg_ - 1) && (calcul_res == 1)) { return i; } } }
return 0; }
|
main.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| #include "dh.h"
int main(int argc, char **argv) { DH *dh = new DH();
unsigned int xa = dh->GetPrivateKey(); cout << "用户A的密钥生成:\t" << endl; cout << "私钥xa:\t" << xa << endl;
unsigned int ya = dh->GetPublicKey(xa); cout << "公钥ya:\t" << ya << endl; cout << endl;
unsigned int xb = dh->GetPrivateKey(); cout << "用户B的密钥生成:\t" << endl; cout << "私钥xb:\t" << xb << endl;
unsigned int yb = dh->GetPublicKey(xb); cout << "公钥yb:\t" << yb << endl; cout << endl;
unsigned int ka = dh->GetKey(yb, xa); cout << "用户A计算产生密钥:\t" << endl; cout << "密钥ka:\t" << ka << endl; cout << endl;
unsigned int kb = dh->GetKey(ya, xb); cout << "用户B计算产生密钥:\t" << endl; cout << "密钥kb:\t" << kb << endl;
delete dh;
return 0; }
|
结果
总结
网络安全中Diffie-Hellman的C++语言描述简单实现。
参考资料
作者的话
- 感谢参考资料的作者/博主
- 作者:夜悊
- 版权所有,转载请注明出处,谢谢~
- 如果文章对你有帮助,请点个赞或加个粉丝吧,你的支持就是作者的动力~
- 文章在描述时有疑惑的地方,请留言,定会一一耐心讨论、解答
- 文章在认识上有错误的地方, 敬请批评指正
- 望读者们都能有所收获