-3

I'm writing a simple program in which I take a .txt file read it and encrypt or decrypt it with the Playfair algorithm using a 16x16 ASCII cipher. When I decypher a file I get it mostly correct the only issue is that the char 'o' is replaced with '_' and random sections of the text are not deciphered right.

Example:
"as she swam l z}"d~o*dd/y``6� {;/~v_(tb_)uesp4�prxnc(zicehr,P`(yc'eezm"-ickin_)"

This is the section of the code that I use to decipher it. At this point I already generated the matrix and read the input file. In the encryption code if a and b are equal b is set to NULL, the last if statement checks that. The variable besedilo is the whole text of the file and M is the 16x16 matrix.

for(int t = 0; t < besedilo.size()-2;t+=2){
        char a = besedilo[t];
        char b = besedilo[t+1];
        int it, jt, k = 0,f = 0, i=0,j=0;
        for(it = 0; it < 16; it++){
            for(jt = 0; jt < 16; jt++){
                if(a == M[it][jt]){
                    i = it;
                    j = jt;
                }
                if(b == M[it][jt]){
                    k = it;
                    f = jt;
                }
            }
        }
        if((i != k)&&(j != f)){
            a = M[i][f];
            b = M[k][j];
        }else if(i == k){
            a = M[i][(j-1)%16];
            b = M[k][(f-1)%16];
        }else if(j == f){
            a = M[(i-1)%16][j];
            b = M[(k-1)%16][f];
        }
        if(b == NULL){
            b = a;
        }
JuanKek
  • 75
  • 1
  • 5
  • Did you try to debug your code with a debugger to find the problem? – Thomas Sablik Apr 25 '20 at 12:49
  • Yes, and as far as I could tell the issue is in the else if statements. I tried a few things but nothing really changed that much. The code is more or else the same as for the normal 5x5 playfair I just upscaled it for a 16x16 matrix. I posted the whole section just in case if I missed anything. – JuanKek Apr 25 '20 at 12:57
  • Please describe the issues you found with the debugger. – Thomas Sablik Apr 25 '20 at 12:58
  • 1
    Is it possible that `i`, `j`, `k` or `f`are `0` in the `if`-`else` section? In that case the access to `M` is undefined behavior. – Thomas Sablik Apr 25 '20 at 13:07
  • No, that doesn't happen. Also I cannot find the problem anymore with the debugger. Is it maybe possible that it has problems because I read the whole text into a variable instead of char by char? – JuanKek Apr 25 '20 at 14:20
  • No, It should be ok to read the whole file. Did you try to step with your debugger to the position where the first error occurs? You should be able to see where the wrong values come from. – Thomas Sablik Apr 25 '20 at 16:34
  • 1
    I don't understand how you can be sure that `i` for example cannot be equal to 0... – Damien Apr 25 '20 at 16:47

1 Answers1

0

Your issue is that % does not wrap with negative numbers. Instead you are indexing negatively when any of your positions are in the 0th column or index as then you are doing (0 - 1) % 16 which is -1. See this response here: Modulo operator with negative values for the issue.

You can easily solve this with a small helper function that handles the wrapping for you, see How to make the mod of a negative number to be positive? for a possible solution.

DClyde
  • 96
  • 2