mercredi 14 mai 2014

Can I have a member function of a class called by another member function and how to do so?


Vote count:

0




To check the logic of the provided code, I will put cout statements in key() to print the bitsets and variables inside it. I don't know how to have key() called by another member function, I don't know if it can be done in the first place.


Here is the code:



#include <iostream>
#include <math.h>
#include <algorithm>
#include <vector>
#include <boost/dynamic_bitset.hpp>
#include <string>

using namespace std;
class rsa {
protected:
int polyLoc, x, y, p, q, d, m, n, f, e, c, g, xn, c_, end, k;
boost::dynamic_bitset<> inpSeq;
boost::dynamic_bitset<> operSeq;
boost::dynamic_bitset<> bit;
vector <int> xorArray;
vector <int> keyReg;
public:
rsa () : polyLoc(210), inpSeq(4), operSeq(4), bit(4), x(0), y(0), n(0), e(0), c(0), g(0), k(0), end(0), f(0), xn(0), c_(1) {};
void set (int , int , int, const boost::dynamic_bitset <>& );
int key () {
while(polyLoc>0)
{
xorArray.push_back(polyLoc%10);
polyLoc/=10;
}
sort(xorArray.rbegin(), xorArray.rend());
operSeq = inpSeq;
keyReg.push_back(inpSeq[0]);
x = xorArray[0];
do {
for (unsigned int r = 1; r < xorArray.size(); r++)
{
bit[3] = operSeq[x];
y = xorArray[r];
bit[3] = bit[3] ^ operSeq[y];
}
operSeq >>= 1;
operSeq[3] = bit[3];
keyReg.push_back(operSeq[0]);
}
while ((operSeq != inpSeq));
for ( unsigned int i = 0; i < keyReg.size(); i++)
{
if (keyReg[i]==1)
m = m + int(pow(2,i));
}
n = p*q;
f = (p-1)*(q-1);
for (k ; end < 1; k++)
{
if ((1+k*f)%d==0)
{
end = 2;
e = (1+k*f)/d;
}
}
g = int(pow(m,e));
if (g < 0)
{
if ((e%2)==0)
{
c_=(int(pow(m,2))%n)*(e/2);
c_ = c_%n;
c = c_;
}
else
{
c_ = (int(pow(m,2))%n)*(e/2);
c_ = c_*(m%n);
c_ = c_%n;
c = c_;
}
}
else
{
c = g%n;
}
return m;}
};
void rsa::set(int p_, int q_, int d_, const boost::dynamic_bitset <>& m_ )
{
p = p_;
q = q_;
d = d_;
inpSeq = m_;
}

class public_key : public rsa {
public:
public_key () : rsa () {} ;
};

class private_key : public rsa {
public:
private_key () : rsa () {} ;
};

int main()
{
public_key usr1, usr2, usr3;
private_key usr1r, usr2r, usr3r;
string user_sel;
usr1.set (11, 5, 23, boost::dynamic_bitset<>(std::string("1100")));
usr2.set (13, 7, 97, boost::dynamic_bitset<>(std::string("0010")));
usr3.set (11, 17, 997, boost::dynamic_bitset<>(std::string("0011")));
usr1r.set (17, 7, 1999, boost::dynamic_bitset<>(std::string("0111")));
usr2r.set (11, 17, 51, boost::dynamic_bitset<>(std::string("0110")));
usr3r.set (43, 17, 997, boost::dynamic_bitset<>(std::string("1010")));
int usr1_pu = usr1.key();
int usr2_pu = usr2.key();
int usr3_pu = usr3.key();
int usr1r_pr = usr1r.key();
int usr2r_pr = usr2r.key();
int usr3r_pr = usr3r.key();
//User selection
asker:
cout << "Enter user: (0 will exit)" << endl;
cin >> user_sel;
if (user_sel=="Mohamed1")
{
cout << "Public key: " << usr1_pu << endl;
cout << "Private key: " << usr1r_pr << endl;
goto asker;
}
else if (user_sel=="Mohamed2")
{
cout << "Public key: " << usr2_pu << endl;
cout << "Private key: " << usr2r_pr << endl;
goto asker;
}
else if (user_sel=="Mohamed3")
{
cout << "Public key: " << usr3_pu << endl;
cout << "Private key: " << usr3r_pr << endl;
goto asker;
}
else if (user_sel=="0")
goto end;
else
{
cout << "You entered the wrong username." << endl;
goto asker;
}
end:
cin.get();
return 0;
}


asked 1 min ago






Aucun commentaire:

Enregistrer un commentaire