-1
我輸入值,在下面的代碼p=3
,q=11
,e=7
和m=2
:C++配方麻煩
#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
。
我的代碼在某處有錯誤嗎?
另外請注意,有計算模冪運算的方法更有效率(特別是當您像往常一樣使用RSA時,不在「常規」整數可以執行的範圍之外)。 –