P
Protoman
I wrote a DHM key exchange program that has something funky going on:
the number that you have to send to your friend is completely wrong,
but the session key it generates is correct. Wha?
Code:
DHMAlg.hpp
------------------------
#pragma once
const long long& DHMAlg(long long& base, long long& mod, long long&
num);
--------------------------
DHMAlg.cpp
------------------------
#pragma once
#include "DHMAlg.hpp"
const long long& DHMAlg(long long& base, long long& mod, long long&
num)
{
const long long& Exp(const long long& base,long long exp);
long long ret=(Exp(base,num)%mod);
return ret;
}
const long long& Exp(const long long& base,long long exp)
{
long long ret=1;
for(;exp--
ret=ret*base;
return ret;
}
----------------------------------------------
DHMKeyGenMain.cpp
----------------------------------------------
#include <iostream>
#include <cstdlib>
#include "DHMAlg.hpp"
using namespace std;
int main()
{
long long base, mod, num, num2;
cout << "Enter the base, modulus, and your secret number: " << endl;
cin >> base >> mod >> num;
cout << "The number to send to your friend is: " <<
DHMAlg(base,mod,num) << endl;
cout << "Enter the number you got from your friend: " << endl;
cin >> num2;
long long key=DHMAlg(num2,mod,num);
cout << "Here's your session key: " << key << endl;
system("PAUSE");
return EXIT_SUCCESS;
}
---------------------------------------------------------
This sounds like one of those "I used the wrong method but I got the
right answer" things.
Can anyone here help me out?
Thanks!!!!
the number that you have to send to your friend is completely wrong,
but the session key it generates is correct. Wha?
Code:
DHMAlg.hpp
------------------------
#pragma once
const long long& DHMAlg(long long& base, long long& mod, long long&
num);
--------------------------
DHMAlg.cpp
------------------------
#pragma once
#include "DHMAlg.hpp"
const long long& DHMAlg(long long& base, long long& mod, long long&
num)
{
const long long& Exp(const long long& base,long long exp);
long long ret=(Exp(base,num)%mod);
return ret;
}
const long long& Exp(const long long& base,long long exp)
{
long long ret=1;
for(;exp--
ret=ret*base;
return ret;
}
----------------------------------------------
DHMKeyGenMain.cpp
----------------------------------------------
#include <iostream>
#include <cstdlib>
#include "DHMAlg.hpp"
using namespace std;
int main()
{
long long base, mod, num, num2;
cout << "Enter the base, modulus, and your secret number: " << endl;
cin >> base >> mod >> num;
cout << "The number to send to your friend is: " <<
DHMAlg(base,mod,num) << endl;
cout << "Enter the number you got from your friend: " << endl;
cin >> num2;
long long key=DHMAlg(num2,mod,num);
cout << "Here's your session key: " << key << endl;
system("PAUSE");
return EXIT_SUCCESS;
}
---------------------------------------------------------
This sounds like one of those "I used the wrong method but I got the
right answer" things.
Can anyone here help me out?
Thanks!!!!