2015-09-28 94 views
-1

我輸入值,在下面的代碼p=3q=11e=7m=2C++配方麻煩

#include <cstdlib> 
#include <iostream> 

using namespace std; 

class Calculate{ 
    int q, p, n, d, e, c, zeta , m, encryption; 
public: 
    Calculate(){ 
     cout << "Enter P" << endl; 
     cin >> p; 

     cout << "Enter Q" << endl; 
     cin >> q; 

     cout << "Enter E" << endl; 
     cin >> e; 

     cout << "Enter M" << endl; 
     cin >> m; 

     assign(); 
    } 

    void test(){ 
     while (e >= zeta || e <=1){ 
      cout << "Enter a correct value for E" << endl; 
      cin >> e; 
     } 
     encrypt(); 
    }; 

    void assign(){ 
     n = p*q; 
     zeta = (p-1)*(q-1); 

     for (int j = 2; j < n; j++){ 
      if ((j*e) % zeta == 1){  
       d = j; 
       j = n; 
      } 
     } 
     test(); 
    }; 

    void encrypt(){ 
     cout << m << endl; 
     cout << e << endl; 
     cout << n << endl; 
     encryption = (m^e) % n; 
     cout << "The encryption Is: " << encryption << endl; 
    }; 


}; 

//------------------------------------------ 
int main(int argc, char *argv[]){ 
    Calculate calc; 

    system("PAUSE"); 
    return EXIT_SUCCESS; 
} 

出於某種原因encryption總是等於5,這是沒有意義的我,因爲2^7 % 33 == 29

我的代碼在某處有錯誤嗎?

回答

3

這裏的問題最有可能是^算子不是指數,它是按位排他或算子。 2 XOR 7確實是5.

您可能想要std::pow

+0

另外請注意,有計算模冪運算的方法更有效率(特別是當您像往常一樣使用RSA時,不在「常規」整數可以執行的範圍之外)。 –