DHM Key Exchange program has something weird going on

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!!!!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top