cmatrixencryption

Issue with Encryption Algorithm in C


I'm currently developing a Zigzag encryption algorithm in C, and I've encountered an issue with the row indexing when the algorithm is moving upwards. The row indices seem to go into negative values, resulting in unexpected behavior.

Here's my code:

#include <stdio.h>

int main() {
    int size = 10; // Size of the input phrase
    char phrase[] = "ciaomestai"; // Input phrase to be encrypted
    char zzMatrix[3][10]; // Destination matrix
    
    int r = 0; 
    int isInvert = 0;
    int cont = 0;
    int limit = 2;

    for(int c = 0; c < size; c++) {
        if(!isInvert){
            zzMatrix[r++][c] = phrase[c];
        } else {
            zzMatrix[r--][c] = phrase[c];
        }

        printf("[%c", phrase[c]);
        printf("isInvert? %d, R:%d --- C:%d]///////", isInvert, r, c);

        if(cont == limit) { 
            isInvert = !isInvert;
            limit = (limit == 2) ? 0 : 2;
            cont = (limit == 2) ? 0:2;
        }

        if(!isInvert){
            cont++;
        } else {
            cont--;
        }
    }

    return 0;
}

And here are the generated logs:

> [cisInvert? 0, R:0 --- C:0]///////[iisInvert? 0, R:1 ---
> C:1]///////[aisInvert? 0, R:2 --- C:2]///////[oisInvert? 1, R:1 ---
> C:3]///////[cisInvert? 1, R:0 --- C:4]///////[oisInvert? 1, R:-1 ---
> C:5]///////[misInvert? 1, R:-2 --- C:6]///////[eisInvert? 1, R:-3 ---
> C:7]///////[visInvert? 1, R:-4 --- C:8]///////[aisInvert? 1, R:-5 ---
> C:9]///////

The problem arises when the algorithm is moving upwards (isInvert is true), and the row index r goes into negative values. I'm seeking insights into why this is happening and how I can rectify it.

This is in fact how it should be if you put a string like "hihowareu":

> [hisInvert? 0, R:0 --- C:0]///////[iisInvert? 0, R:1 ---
> C:1]///////[hisInvert? 0, R:2 --- C:2]///////[oisInvert? 1, R:1 ---
> C:3]///////[wisInvert? 1, R:0 --- C:4]///////[aisInvert? 0, R:1 ---
> C:5]///////[risInvert? 0, R:2 --- C:6]///////[eisInvert? 0, R:1 ---
> C:7]///////[uisInvert? 1, R:0 --- C:8]///////

Solution

  • When cont == limit, resetting cont = 0; means you lose track of your rail position, since cont--; expects to start traversing "up" from the lowest logical position (2, not zero).

    isInvert and cont seemingly complicate this. A single integer that is either 1 or -1, and is added to r each iteration should suffice.

    #include <stdio.h>
    
    int main(void)
    {
        const int size = 10;
        char phrase[] = "ciaomestai";
        char zzMatrix[3][10];
    
        int r = 0;
        int dir = 1;
        int limit = 2;
    
        for(int c = 0; c < size; c++) {
            zzMatrix[r][c] = phrase[c];
    
            printf("<%c> [%d, %d]\n", phrase[c], r, c);
    
            if (r == limit) {
                limit = (limit == 2) ? 0 : 2;
                dir = (dir == 1) ? -1 : 1;
            }
    
            r += dir;
        }
    }
    
    <c> [0, 0]
    <i> [1, 1]
    <a> [2, 2]
    <o> [1, 3]
    <m> [0, 4]
    <e> [1, 5]
    <s> [2, 6]
    <t> [1, 7]
    <a> [0, 8]
    <i> [1, 9]