c++公式问题

C++ Formula trouble

本文关键字:问题 c++      更新时间:2023-10-16

我在以下代码中输入p=3, q=11, e=7m=2的值:

#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

我有一个错误在我的代码某处?

这里的问题很可能是^运算符不是指数运算符,它是位排他运算符或运算符。2 XOR 7确实是5。

你可能想要std::pow