高精度类实现 Diffie Hellman Algorithm
2014-03-22 14:17
399 查看
信息安全原理
作业2 第2题
高精度类写好了还不行
还要实现一下DH算法
那把表达式和DH算法写一个main()里吧
按照维基百科上的小数据试了一下,没有问题
把DH写成函数了,再发一遍好了
作业2 第2题
高精度类写好了还不行
还要实现一下DH算法
那把表达式和DH算法写一个main()里吧
// name: main.cpp // author: amrzs // date: 2014/03/22 #include <string> #include <iostream> #include "bigint.h" using namespace std; Bigint calc(Bigint a, Bigint b, char c){ Bigint result; switch(c){ case '+': result = a + b; break; case '-': result = a - b; break; case '*': result = a * b; break; case '/': result = a / b; break; case '%': result = a % b; break; default: cout << "You input a wrong operator" << endl; break; } return result; } int main(){ cout <<"If you want to calculate an expression then input \"EX 123456789 * 987654321\"" << endl <<"if DH algorithm then input \"DH\" and something as follow" << endl; string sFlag, s1, s2; char ch; while(1){ // don't like for(;;) cin >> sFlag; if("EX" == sFlag){ cin >> s1 >> ch >> s2; Bigint a(s1), b(s2); Bigint result = calc(a, b, ch); cout << "The result of expression is: "; result.printNum(); }else if("DH" == sFlag){ cout << "Please input the shared message g and p" << endl; cin >> s1 >> s2; Bigint g(s1), p(s2); cout << "Please input your secret key a" << endl; cin >> s1; Bigint a(s1), A; A = g.getPow(a, p); cout << "Your public key A is: "; A.printNum(); cout << "You send g, p and A to another person" << endl; cout << "He received your shared message p, g and public key A" << endl; cout << "Please input his secret key b" << endl; cin >> s1; Bigint b(s1), B; B = g.getPow(b, p); cout << "His public key is: "; B.printNum(); cout << "He sends the public key B to you" << endl; cout << "K = B^a mod p and you calculate the K is: " << endl; Bigint K = B.getPow(a, p); K.printNum(); cout << "K = A^b mod p and he calculates the K is: " << endl; K = A.getPow(b, p); K.printNum(); }else{ cout << "Your input is wrong, please try again!" << endl; } cout <<"If you want to calculate an expression then input EX 123456789 * 987654321" << endl <<"if DH algorithm then input DH and something as follow" << endl; } return 0; }
按照维基百科上的小数据试了一下,没有问题
把DH写成函数了,再发一遍好了
// name: main.cpp // author: amrzs // date: 2014/03/22 #include <string> #include <iostream> #include "bigint.h" using namespace std; Bigint calc(Bigint a, Bigint b, char c){ Bigint result; switch(c){ case '+': result = a + b; break; case '-': result = a - b; break; case '*': result = a * b; break; case '/': result = a / b; break; case '%': result = a % b; break; default: cout << "You input a wrong operator" << endl; break; } return result; } void DiffieHellman(){ string s1, s2; cout << "Please input the shared message g and p" << endl; cin >> s1 >> s2; Bigint g(s1), p(s2); cout << "Please input your secret key a" << endl; cin >> s1; Bigint a(s1), A; A = g.getPow(a, p); cout << "Your public key A is: " << endl; A.printNum(); cout << "You send g, p and A to another person" << endl; cout << "He received your shared message p, g and public key A" << endl; cout << "Please input his secret key b" << endl; cin >> s1; Bigint b(s1), B; B = g.getPow(b, p); cout << "His public key is: " << endl; B.printNum(); cout << "He sends the public key B to you" << endl; cout << "K = B^a mod p and you calculate the K is: " << endl; Bigint K = B.getPow(a, p); K.printNum(); cout << "K = A^b mod p and he calculates the K is: " << endl; K = A.getPow(b, p); K.printNum(); } int main(){ cout <<"If you want to calculate an expression then input \"EX 123456789 * 987654321\"" << endl <<"if DH algorithm then input \"DH\" and something as follow" << endl; string sFlag, s1, s2; char ch; while(1){ // don't like for(;;) cin >> sFlag; if("EX" == sFlag){ cin >> s1 >> ch >> s2; Bigint a(s1), b(s2); Bigint result = calc(a, b, ch); cout << "The result of expression is: "; result.printNum(); }else if("DH" == sFlag){ DiffieHellman(); }else{ cout << "Your input is wrong, please try again!" << endl; } cout <<"If you want to calculate an expression then input EX 123456789 * 987654321" << endl <<"if DH algorithm then input DH and something as follow" << endl; } return 0; }
相关文章推荐
- JAVA上加密算法的实现用例MD5/SHA1,DSA,DESede/DES,Diffie-Hellman的使用
- JAVA上加密算法的实现用例MD5/SHA1,DSA,DESede/DES,Diffie-Hellman的使用
- JAVA 上加密算法的实现用例 MD5/SHA1,DSA,DESede/DES,Diffie-Hellman 的使用
- 客户端网络pomelo学习笔记 (3) node.js 与 c 客户端 Diffie-Hellman 密钥交换算法的实现客户端网络
- 迪菲-赫尔曼密钥交换(Diffie–Hellman)算法原理和PHP实现版
- Diffie-Hellman密钥交换算法的JavaScript实现
- JAVA上加密算法的实现用例MD5/SHA1,DSA,DESede/DES,Diffie-Hellman的使用(转)
- JAVA上加密算法的实现用例MD5/SHA1,DSA,DESede/DES,Diffie-Hellman的使用
- ssh秘钥交换详解与实现 diffie-hellman-group-exchange-sha
- JAVA上加密算法的实现用例MD5/SHA1,DSA,DESede/DES,Diffie-Hellman的使用
- 用java编程实现Diffie-Hellman算法
- 迪菲-赫尔曼密钥交换(Diffie–Hellman)算法原理和PHP实现版
- JAVA上加密算法的实现用例MD5/SHA1,DSA,DESede/DES,Diffie-Hellman的使用(转)
- Python实现Diffie-Hellman密钥交换协议
- 对Diffie-Hellman密钥交换算法实现的一点改进
- C#实现Diffie-Hellman(DH)算法可结合DES用于通信信息加密
- pomelo学习笔记 (3) node.js 与 c 客户端 Diffie-Hellman 密钥交换算法的实现
- ssh秘钥交换详解与实现 diffie-hellman-group-exchange-sha
- ssh秘钥交换详解与实现 diffie-hellman-group-exchange-sha
- JAVA上加密算法的实现用例MD5/SHA1,DSA,DESede/DES,Diffie-Hellman的使用