Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream> //Standard library.
- #include <boost/dynamic_bitset.hpp> //Library for 10 handling.
- #include <vector> //Variable size array.
- #include <algorithm> //We use sorting from it.
- #include <math.h>
- using namespace std;
- int main()
- {
- int y = 0;
- int turnCount = 0;
- int count1 = 0, count0 = 0;
- int xx = 0;
- int polyLoc;
- int p = 0;
- int q = 0;
- int d = 0;
- int n = 0;
- int end = 0;
- int f = 0;
- int e = 0;
- int m = 0;
- int c = 0;
- int l = 0, g = 0;
- boost::dynamic_bitset<> inpSeq(5);
- boost::dynamic_bitset<> operSeq(5);
- boost::dynamic_bitset<> bit(5);
- vector <int> xorArray;
- vector <int> keyReg;
- cout << "p is: ";
- cin >> p;
- cout << "q is: ";
- cin >> q;
- cout << "d is: ";
- cin >> d;
- cout << "What is the legnth of the sequence?";
- cin >> xx;
- inpSeq.resize(xx);
- operSeq.resize(xx);
- bit.resize(xx);
- cout << "Enter a bit sequence: \n";
- cin >> inpSeq;
- int seq_end = inpSeq.size() - 1;
- cout << "Enter polynomial:";
- cin >> polyLoc;
- while(polyLoc>0)
- {
- xorArray.push_back(polyLoc%10);
- polyLoc/=10;
- }
- sort(xorArray.rbegin(), xorArray.rend());
- cout << "\n";
- 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 ++;
- cout << operSeq << "\n";
- }
- while ((operSeq != inpSeq) && (turnCount < 1024));
- cout << "Generated key is: ";
- for (unsigned int k = 0; k < keyReg.size(); k++)
- {
- cout << keyReg[k];
- }
- cout << "\n";
- cout << "Bit 1 positions: ";
- for ( unsigned int g = 0; g < xorArray.size(); g++)
- {
- cout << xorArray[g];
- }
- cout << "\n";
- cout << "Key length is: " << keyReg.size();
- cout << "\n";
- for ( unsigned int i = 0; i < keyReg.size(); i++)
- {
- if (keyReg[i]==1)
- {
- count1++;
- m = m + int(pow(2,i));
- }
- else {
- count0++;
- }
- }
- cout << "Number of 0's: " << count0 << "\n";
- cout << "Number of 1's: " << count1 << "\n";
- if ( keyReg.size()%2 ==0)
- {
- cout << "key length is even. \n";
- if (count1==count0)
- {
- cout << "Key is perfect! \n";
- }
- else {
- cout << "Key is not perfect! \n";
- }
- }
- else
- {
- cout << "key length is odd. \n";
- if ((count1==count0+1) || (count0==count1+1))
- {
- cout << "Key is perfect! \n";
- }
- else {
- cout << "Key is not perfect! \n";
- }
- }
- cout << "M is: " << m << "\n";
- n = p*q;
- f = (p-1)*(q-1);
- for (int k = 0; end < 1; k++)
- {
- cout << "k is : " << k << "\n";
- if ( (1+k*f)%d == 0)
- {
- end = 2;
- e = (1+(k*f))/d;
- }
- }
- cout << "e is: " << e << "\n";
- cout << "m is: " << m << "\n";
- g = int(pow(m,e));
- cout << "m^e: " << g << "\n";
- c = g%n;
- cout << "C is: " << c << "\n";
- cin.get();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement