0

I am trying to simulate a brute force attack using an additive cipher and i need to use modulus on some numbers but when i try to use the modulus operator "%" it never seems work here is my code

#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;
    }    
}

the output stays as the array of numbers minus i. i dont know why this isnt working any help would be appriciated.

Scheff's Cat
  • 19,528
  • 6
  • 28
  • 56
  • Did you intend `cout< – 1201ProgramAlarm Oct 04 '18 at 03:27
  • That is what is intended. I made an error in that code but above that line it should read modArray[x] = y; – Joe O'Brien Oct 04 '18 at 03:54
  • A hint: Concerning `//Get Number for each letter`: To convert a capital character to a value in range [0, 26), you may use e.g. `int num = chr - 'A';`. (This will work with ASCII, and I doubt you're platform is using something else like EBCDIC.) – Scheff's Cat Oct 04 '18 at 05:58
  • Another hint: You have correctly allocated 15 characters to store `"UOISCXEWLOBDOX"` (incl. the silent `'\0'` terminator) in `cipherText`. `sizeof(cipherText)` will hence return 15, and this involves the `'\0'` terminator in the loops. (It has index 14 - the highest valid index in `char cipherText[15]`.) I hardly believe this is intended. May be, it's better to introduce a length variable which is one less than size. – Scheff's Cat Oct 04 '18 at 06:07
  • Another hint: There are a lot of [magic numbers](https://stackoverflow.com/q/47882/7478597) in your code. I would initially introduce variables for these values. It makes the code better maintainable. (It prevents unintended side effects when a certain detail is changed somewhere.) E.g. `char cipherText [] = "UOISCXEWLOBDOX";` <- let the compiler count the number of elements; `const size_t size = sizeof cipherText;` <- this is a constant which can be used for dimension of related arrays. `const size_t len = size - 1;` <- for loops consider "pay load" only. – Scheff's Cat Oct 04 '18 at 06:13
  • Concerning your title: _Modulus not returning remainder of a number_ I simply don't believe this. I really (really, really) cannot imagine that something simple like the modulo operator is broken on your system. What I could imagine is that it does not yield the expected values. (Because your expectation is wrong?) I believe you urgently should go step by step through your code with a debugger to see what _really_ happens. (I must admit that even after 20 ... 30 years developing, I'm still sometimes surprised how values in debugger differ from my expectation and why.) ;-) – Scheff's Cat Oct 04 '18 at 06:24

2 Answers2

1

Your solution with the intermediate arrays is vastly overcomplicated, and you're unnecessarily using low-level primitives. The code can be made quite simpler:

#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;
    }
}

There is the assumption made about the linearity of the 'A' - 'Z' range, but you'd be hard-pressed to find a machine that doesn't do that.

This assumption allows for the logic of the code to essentially condense to this one line:

return ((c + i - 'A') % alphabetSize) + 'A';

It might seem a bit weird at first, but it gets easier once you realize that the -'A' and +'A' parts are there just to map the character to the 0-26 range, so that the modulo operation could be utilized. You could obviously split this up into multiple transformations, doing it in more steps (e.g. subtract 'A', add i, do the modulo, add 'A').

Bartek Banachewicz
  • 38,596
  • 7
  • 91
  • 135
0

The modulo operator is working fine.

You are getting negative values because of

j = numArray[x]-i.

I just printed your numArray and it comes out to be 20 14 8 18 2 23 4 22 11 14 1 3 14 23 26 for one instance. Now you are subtracting i from each element in your numArray because of which your resulting array have negative values.

Ishpreet
  • 5,230
  • 2
  • 19
  • 35