C++ 不返回数字余数的模

C++ 不返回数字余数的模,c++,C++,我尝试使用加法密码模拟暴力攻击,我需要对一些数字使用模,但当我尝试使用模运算符“%”时,它似乎永远不起作用这里是我的代码 #include <cstdlib> #include <iostream> using namespace std; int main() { char cipherText [15] = "UOISCXEWLOBDOX"; char guess [] = " "; int numArray [15]; int

我尝试使用加法密码模拟暴力攻击,我需要对一些数字使用模,但当我尝试使用模运算符“%”时,它似乎永远不起作用这里是我的代码

#include <cstdlib>
#include <iostream>
using namespace std;



int main() 
{
    char cipherText [15] = "UOISCXEWLOBDOX";
    char guess [] = " ";
    int numArray [15];
    int modArray [15];
    int finalArray[15];
    char alpha [26] = {'A','B','C','D','E','F','G','H','I','J','K',
                'L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};

    //Get Number for each letter
    for(int x = 0; x < sizeof(cipherText); x++)
    {    
        char c = cipherText[x];
        int num = 0;
        for(int i = 0; i<sizeof(alpha);i++)
        {
            if(c == alpha[i])
            {
                num = num + 0;
                break;
            } 
            else
            {
                num = num + 1;
            }    
        }
        numArray[x] = num;
        //cout<<" "<<numArray[x];
    }


    for(int i = 0; i < 26; i++)
    {  

        cout<<endl;

        for(int x = 0; x < 15; x++)
        {
            int j;
            if(i == 0)
            {
                j = numArray[x];
            }
            else
            {    
                j = numArray[x]-i;
            } 
            modArray[x] = j;
            //cout<<modArray[x]<<" ";
        }

        for(int x = 0; x < 15; x++)
        {    

            int y = (modArray[x])%26;
            cout<<modArray[x]<<" ";
        }




        cout<<endl;
    }    
}
#包括
#包括
使用名称空间std;
int main()
{
字符密文[15]=“UOISCXEWLOBDOX”;
字符猜测[]=“”;
努马拉伊国际酒店[15];
int modArray[15];
int finalArray[15];
字符alpha[26]={'A','B','C','D','E','F','G','H','I','J','K',
‘L’、‘M’、‘N’、‘O’、‘P’、‘Q’、‘R’、‘S’、‘T’、‘U’、‘V’、‘W’、‘X’、‘Y’、‘Z’;
//获取每个字母的编号
for(int x=0;x对于(int i=0;i,使用中间数组的解决方案过于复杂,并且不必要地使用低级原语。代码可以变得非常简单:

#include <iostream>
#include <string>
#include <algorithm>

int main() {
    const std::string cipherText = "UOISCXEWLOBDOX";
    constexpr int alphabetSize = 'Z' - 'A' + 1;

    for(int i = 0; i < alphabetSize; i++) {  
        std::string encryptedText(cipherText.size(), ' ');
        std::transform(cipherText.begin(), cipherText.end(), encryptedText.begin(),
            [=](char c){
                return ((c + i - 'A') % alphabetSize) + 'A';
            }
        );
        std::cout << encryptedText << std::endl;
    }
}

一开始可能有点奇怪,但一旦你意识到
-'a'
+'a'
部分只是用来将字符映射到0-26范围,这样就可以利用模运算了。很明显,你可以将其分解成多个转换,用更多的步骤来完成(例如,减去
'A'
,添加
i
,进行模运算,添加
'A'
)。

模运算器工作正常

你得到的是负值,因为

j=numaray[x]-i


我刚刚打印了你的numArray,结果显示它是
201488182234211141314142326
。现在你从numArray中的每个元素中减去
I
,因为你得到的数组有负值。

你打算
这样做吗在该代码中,但在该行之上,它应该是modArray[x]=y;提示:关于
//获取每个字母的数字
:要将大写字符转换为[0,26]范围内的值,您可以使用例如
int num=chr-'A';
(这将与ASCII一起工作,我怀疑您的平台是否使用了其他类似EBCDIC的东西。)另一个提示:您已经正确分配了15个字符来存储
密文中的
“UOISCXEWLOBDOX”
(包括静默的
'\0'
终止符)
sizeof(cipherText)
将因此返回15,这涉及循环中的
'\0'
终止符。(它有索引14-在
字符密文[15]
中的最高有效索引)我几乎不相信这是故意的。可能是,最好引入一个比大小小一个的长度变量。另一个提示:代码中有很多。我最初会为这些值引入变量。这样可以更好地维护代码。(当某个细节在某处发生更改时,它可以防止意外的副作用。)例如,
char-cipherText[]=“UOISCXEWLOBDOX”;
return ((c + i - 'A') % alphabetSize) + 'A';