0

I am a beginner at C++ coding, and coding in general. I have successfully wrote a code to calculate the RSA algorithm output C, when m is the decimal equivalent to the output of a linear feedback shift register. I have put the code in a class so that I am able to use it more than once. When compiling, I get bug notifications like: "‘keyReg’ does not name a type keyReg.push_back(inpSeq[0]);" When this code was in the main class, it didn't produce such errors when compiling. Here is the full code:

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

using namespace std;

class lfsr {
 int y, xx, polyLoc, n, end, f, e, m, c, l, g;
 boost::dynamic_bitset<> inpSeq(5);
 boost::dynamic_bitset<> operSeq(5);
 boost::dynamic_bitset<> bit(5);
 vector <int> xorArray;
 vector <int> keyReg;
 polyLoc = 320;
 while(polyLoc>0)
 {
  xorArray.push_back(polyLoc%10);
  polyLoc/=10;
 }
 sort(xorArray.rbegin(), xorArray.rend());
 operSeq = inpSeq;
 keyReg.push_back(inpSeq[0]);
  int x = xorArray[0];
  do {
  for (unsigned int r = 1; r < xorArray.size(); r++)
  {
  bit[seq_end] = operSeq[x];
  y = xorArray[r];
  bit[seq_end] = bit[seq_end] ^ operSeq[y];
  }
  operSeq >>= 1;
  operSeq[seq_end]  = bit[seq_end];
  keyReg.push_back(operSeq[0]);
  turnCount ++;
 }
 while ((operSeq != inpSeq) && (turnCount < 1024));
 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 (int k = 0; end < 1; k++)
 {
  if ( (1+k*f)%d == 0)
   {
    end = 2;
    e = (1+(k*f))/d;
   }
  }
g = int(pow(m,e));
c = g%n;
public:
 rsa (int, int, int, boost::dynamic_bitset);
 int key () { return (c); }
};
lfsr::lfsr() //Constructor
{
  y = 0;
  turnCount = 0;
  xx = 0;
  polyLoc = 0;
  n = 0;
  end = 0;
  f = 0;
  e = 0;
  m = 0;
  c = 0;
  l = 0, g = 0;
};
lfsr::rsa (int x, int y, int z, boost::dynamic_bitset <5> initSeq)
 {
  p = x;
  q = y;
  d = z;
  inpSeq = initSeq;
}
int main ()
{
lfsr public_key, private_key;
public_key.rsa (29, 41, 74, 00111);
private_key.rsa (43, 89, 73, 01011);
cout << "Public key is: " << public_key.key() << endl;
cout << "Private key is: " << private_key.key() << endl;
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
Mohamed Ahmed
  • 457
  • 1
  • 7
  • 25
  • You can't just put code into a class. Your class needs to have functions. – jliv902 May 09 '14 at 17:53
  • 3
    Please pick up a good C++ book and/or tutorial, your class definition makes no sense at all. You can't have code in the class body directly, only in member functions. – Mat May 09 '14 at 17:54
  • Ok, thanks, thes small tutorial I am following didn't mention that important note. – Mohamed Ahmed May 09 '14 at 17:56
  • @MohamedAhmed Did it say you COULd just put random bits of code inside a class? I guess not. – OMGtechy May 09 '14 at 17:57
  • @Mat is the definition of lsfr:rsa function correct in terms of syntax? I am not sure if the definition of initSeq is correct! – Mohamed Ahmed May 09 '14 at 17:58
  • 1
    No it's not. It's missing a return type. And it's not decleared in the class. You really really need to find a better tutorial or book if what you have above matches what you're reading in yours. – Mat May 09 '14 at 18:01
  • @Mat, Thanks. Sorry if I wasted your time. – Mohamed Ahmed May 09 '14 at 18:07
  • 1
    BTW, there's a list of books here, if you're looking for one: http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – Mat May 09 '14 at 18:09

1 Answers1

1

In the first half of your class, there appears to be statements placed directly under the class scope.

You need to define member functions to hold that code, and then invoke those member functions on the class.

For the variables, whether they are declared inside or outside the member functions will depend on whether they need to persist or are merely used for temporary storage (for the computation).

merlin2011
  • 71,677
  • 44
  • 195
  • 329