By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,128 Members | 926 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,128 IT Pros & Developers. It's quick & easy.

DHM Key Exchange program has something weird going on

P: n/a
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!!!!

Dec 27 '06 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.