gg003
gg003

Reputation: 17

Implementing columnar encryption technique?

I am working on another encryption technique now, the columnar transposition cipher technique. So far I have only tried to make columns and what I am trying to do is to view the matrix. But with the code that I have written it is only showing one letter in the matrix:

import java.io.*;
import java.lang.reflect.Array;

public class transCip {
    public static void main(String args[]) {
        String keys;
        String message;
        String encrypt;
        String decrypt;
        message = "encryptiontextbe";
        keys = "work";
        encrypt = "";
        decrypt = "";
        char msg[] = message.toCharArray();
        char key[] = keys.toCharArray();
        int x = msg.length;
        int y = key.length;
        char temp[][] = new char[y][x];

        if (x % y != 0) {
            System.out.println("Cannot encrypt string");
        }
        for (int i = 0; i < (x/y); i++) 
        {
            for (int j = 0; j < y; j++) 
            {
                int k=0;
                temp[i][j] = msg[k];
                k++;
            }
        }
        System.out.println("Matrix");
        for (int i = 0; i < (x/y); i++)
        {
            for (int j = 0; j < y; j++) 
            {
                System.out.print(temp[i][j]);
            }
            System.out.println("");
        }
    }
}

And my current output is as follows:

Matrix
eeee
eeee
eeee
eeee

I can't seem to figure out why this is happening;
I tried solving the run on paper also.

Upvotes: 0

Views: 368

Answers (1)

mcgyver5
mcgyver5

Reputation: 1163

for (int i = 0; i < (x/y); i++) 
    {
            for (int j = 0; j < y; j++) 
        {
        int k=0;
                temp[i][j] = msg[k];
        k++;
        }
        }

You reset k to zero in each iteration of the loop, guaranteeing you always get the first letter 'e' of your message string. Try initializing k before the inner loop. This would have been easier to spot with better formatting:

for (int i = 0; i < (x/y); i++) {
    for (int j = 0; j < y; j++) {
        int k=0;
        temp[i][j] = msg[k];
        k++;
    }
}

Upvotes: 2

Related Questions