Advertisement
Mohammed_AhmedAF

Security Project

May 5th, 2014
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.89 KB | None | 0 0
  1. #include <iostream>  //Standard library.
  2. #include <boost/dynamic_bitset.hpp>    //Library for 10 handling.
  3. #include <vector>    //Variable size array.
  4. #include <algorithm> //We use sorting from it.
  5. #include <math.h>
  6.  
  7. using namespace std;
  8.  
  9. int main()
  10. {
  11.  int y = 0;
  12.  int turnCount = 0;
  13.  int count1 = 0, count0 = 0;
  14.  int xx = 0;
  15.  int polyLoc;
  16.  int p = 0;
  17.  int q = 0;
  18.  int d = 0;
  19.  int n = 0;
  20.  int end = 0;
  21.  int f = 0;
  22.  int e = 0;
  23.  int m = 0;
  24.  int c = 0;
  25.  int l = 0, g = 0;
  26.  boost::dynamic_bitset<> inpSeq(5);
  27.  boost::dynamic_bitset<> operSeq(5);
  28.  boost::dynamic_bitset<> bit(5);
  29.  vector <int> xorArray;
  30.  vector <int> keyReg;
  31.  cout << "p is: ";
  32.  cin >> p;
  33.  cout << "q is: ";
  34.  cin >> q;
  35.  cout << "d is: ";
  36.  cin >> d;
  37.  cout << "What is the legnth of the sequence?";
  38.  cin >> xx;
  39.  inpSeq.resize(xx);
  40.  operSeq.resize(xx);
  41.  bit.resize(xx);
  42.  cout << "Enter a bit sequence: \n";
  43.  cin >> inpSeq;
  44.  int seq_end = inpSeq.size() - 1;
  45.  cout << "Enter polynomial:";
  46.  cin >> polyLoc;
  47.  while(polyLoc>0)
  48.  {
  49.   xorArray.push_back(polyLoc%10);
  50.   polyLoc/=10;
  51.  }
  52.  sort(xorArray.rbegin(), xorArray.rend());
  53.  cout << "\n";
  54.  operSeq = inpSeq;
  55.  keyReg.push_back(inpSeq[0]);
  56.   int x = xorArray[0];
  57.   do {
  58.   for (unsigned int r = 1; r < xorArray.size(); r++)
  59.   {
  60.   bit[seq_end] = operSeq[x];
  61.   y = xorArray[r];
  62.   bit[seq_end] = bit[seq_end] ^ operSeq[y];
  63.   }
  64.   operSeq >>= 1;
  65.   operSeq[seq_end]  = bit[seq_end];
  66.   keyReg.push_back(operSeq[0]);
  67.   turnCount ++;
  68.   cout << operSeq << "\n";
  69.  }
  70.  while ((operSeq != inpSeq) && (turnCount < 1024));
  71.  cout << "Generated key is: ";
  72.  for (unsigned int k = 0; k < keyReg.size(); k++)
  73.   {
  74.   cout  <<  keyReg[k];
  75.   }
  76.  cout << "\n";
  77.  cout << "Bit 1 positions: ";
  78.  for ( unsigned int g = 0; g < xorArray.size(); g++)
  79.  {
  80.   cout << xorArray[g];
  81.  }
  82.  cout << "\n";
  83.  cout << "Key length is: " << keyReg.size();
  84.  cout << "\n";
  85.  for ( unsigned int i = 0; i < keyReg.size(); i++)
  86.  {
  87.   if (keyReg[i]==1)
  88.    {
  89.     count1++;
  90.     m = m + int(pow(2,i));
  91.    }
  92.   else {
  93.     count0++;
  94.   }
  95.  }
  96.  cout << "Number of 0's: " << count0 << "\n";
  97.  cout << "Number of 1's: " << count1 << "\n";
  98.  if ( keyReg.size()%2 ==0)
  99.   {
  100.    cout << "key length is even. \n";
  101.    if (count1==count0)
  102.     {
  103.    cout << "Key is perfect! \n";
  104.     }
  105.   else {
  106.    cout << "Key is not perfect! \n";
  107.     }
  108.  }
  109.   else
  110.    {
  111.   cout << "key length is odd. \n";
  112.    if  ((count1==count0+1) || (count0==count1+1))
  113.     {
  114.    cout << "Key is perfect! \n";
  115.     }
  116.   else {
  117.    cout << "Key is not perfect! \n";
  118.     }
  119.    }
  120. cout << "M is: " << m << "\n";
  121. n = p*q;
  122. f = (p-1)*(q-1);
  123. for (int k = 0; end < 1; k++)
  124.  {
  125.   cout << "k is : " << k << "\n";
  126.   if ( (1+k*f)%d == 0)
  127.    {
  128.     end = 2;
  129.     e = (1+(k*f))/d;
  130.    }
  131.   }
  132. cout << "e is: " << e << "\n";
  133. cout << "m is: " << m << "\n";
  134. g = int(pow(m,e));
  135. cout << "m^e: " << g << "\n";
  136. c = g%n;
  137. cout << "C is: " << c << "\n";
  138. cin.get();
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement