简体   繁体   中英

Need help implementing “matrix rotation” in java for cipher

I'm trying to make a simple cipher program that more or less converts ASCII text into a matrix of binary so "AAAA" will become:

01000001  
01000001  
01000001  
01000001

and then it rotates it to make:

1111  
0000  
0000  
0000  
0000  
0000  
1111  
0000

The problem I am facing is that it seems like whenever there is a "0" nothing is returned so I end up with just:

1111  
1111

Here is my code:

public class Cipher {
    public static void main(String[] args) {
        Scanner i = new Scanner(System.in);
        System.out.print("1. Cipher text\n2. Decipher Text\n>");
        int choice = i.nextInt();
        if (choice == 1) {
            System.out.println("Enter text to be ciphered: ");
            String text = i.next();
            String[] a = new String[text.length()];
            String[] b = new String[text.length()];
            String[] c = new String[text.length()];
            String[] d = new String[text.length()];
            String[] e = new String[text.length()];
            String[] f = new String[text.length()];
            String[] g = new String[text.length()];
            String aa = "0";
            String bb = "0";
            String cc = "0";
            String dd = "0";
            String ee = "0";
            String ff = "0";
            String gg = "0";
            for (int n = 0; n < 7; n++) {
                for (int k = 6; k >= 0; k--) {
                    for (int j = 0; j < text.length(); j++) {
                        String buffer = Integer.toBinaryString(text.charAt(j));
                        switch (n) {
                            case 0: a[j] = buffer.substring(k, k + 1); break;
                            case 1: b[j] = buffer.substring(k, k + 1); break;
                            case 2: c[j] = buffer.substring(k, k + 1); break;
                            case 3: d[j] = buffer.substring(k, k + 1); break;
                            case 4: e[j] = buffer.substring(k, k + 1); break;
                            case 5: f[j] = buffer.substring(k, k + 1); break;
                            case 6: g[j] = buffer.substring(k, k + 1); break;
                            default: break;
                        }
                    }
                }
            }
            for (int n = 0; n < 7; n++) {
                for (int m = 0; m < text.length(); m++) {
                    System.out.println(a[m]);
                }
            }
        } else if (choice == 2) {
        }
    }
}

Here's an example of how I want it to work:

Inputted String:

ABCD

First step:

01000001  
01000010  
01000011  
01000100

"rotated counter-clockwise":

1010  
0110  
0001  
0000  
0000  
0000  
1111  
0000

EDIT:

The process works like this:

  1. A string of ASCII characters is converted to the Binary ASCII equivalent

     A - 01000001 B - 01000010 C - 01000011 D - 01000100 E - 01000101 F - 01000110 G - 01000111 

    etc.

For this example I will be using the string "ABABAC" which turns into :

A -> 01000001
B -> 01000010
A -> 01000001
B -> 01000010
A -> 01000001
C -> 01000010
  1. A matrix is made out of the string. The string "ABABAC" turns into the matrix :

     01000001 01000010 01000001 01000010 01000001 01000100 

note: In my code this is the portion :

String buffer = Integer.toBinaryString(text.charAt(j));

text is the raw string of the user input, for the first cycle j would be 0 or the first character in the string text, being A and it would define buffer as 01000001. For the second cycle the second character of the string text, being B would define buffer as 01000010. This process loops until the entire string text has been converted into binary.

  1. This matrix is rotated 90 degrees counter-clockwise to become

     101010 010100 000001 000000 000000 000000 111111 000000 

In my code this portion is :

                    switch (n) {
                        case 0: a[j] = buffer.substring(k, k + 1); break;
                        case 1: b[j] = buffer.substring(k, k + 1); break;
                        case 2: c[j] = buffer.substring(k, k + 1); break;
                        case 3: d[j] = buffer.substring(k, k + 1); break;
                        case 4: e[j] = buffer.substring(k, k + 1); break;
                        case 5: f[j] = buffer.substring(k, k + 1); break;
                        case 6: g[j] = buffer.substring(k, k + 1); break;
                        default: break;

Here, for the first iteration, the case is n is 0 and j is 0 so the first case is selected and the 0 position of array a[] is set to 1 because the last character of "01000001" is a 1. For the second iteration, the character B is processed. B is "01000010" so when the last character is read, a 0 is returned. N is 0 so case 0 is selected but j is 1 so the second position of the array A is set to be a "0" because the last character of "01000010" is a "0".

In other words, the last character of each array in order becomes the first array in order, and the second to last character of each array becomes the second string in order etc.

Essentially the process I am doing is asking the user for a string, converting the string to the ASCII equivalent, and running some nested FOR loops to make arrays out of the last, then second to last, then third to last etc. characters of each string. The problem I am facing is that every 0 seems to be sent as null instead of being sent to the array as an actual 0 resulting in the output being all "1"s.

How I visualized the 2-d loop going through the array 矩阵排序

Cipher class

To make a cipher, simply do something along the lines of Cipher hiddenText = new Cipher(userInput);

/**
 * For the thread: http://stackoverflow.com/questions/35110522/need-help-implementing-matrix-rotation-in-java-for-cipher
 * 
 * @author Riley C
 * @version 1/30/16
 */
public class Cipher
{
    // instance variables
    private String text;
    private String[] matrix;
    private String[] encodedMatrix;

    /**
     * Constructor for objects of class Cipher
     */
    public Cipher(String text)
    {
        this.text = text;
        matrix = new String[text.length()];
        for (int i = 0; i < matrix.length; i++) // Sets each point of the matrix array to binary string
        {
            matrix[i] = "0" + Integer.toBinaryString(text.charAt(i));
            System.out.println(matrix[i]);
        }

        encodedMatrix = new String[8]; // Needs to hold 8 slots
        for (int i = 0; i < 8; i++) // Will reverse the matrix counter clockwise...
        {
            encodedMatrix[i] = "";
            for (int j = 0; j < matrix.length; j++)
                encodedMatrix[i] += matrix[j].charAt(7 - i);
            System.out.println(encodedMatrix[i]);
        }
    }

    // getters and setters
    public String getText() {return text;}
    public String[] getMatrix() // Making dat deep copy
    {
        String[] returnMatrix = new String[this.matrix.length];
        for (int i = 0; i < returnMatrix.length; i++)
            returnMatrix[i] = this.matrix[i];
        return returnMatrix;
    }
    public String[] getEncodedMatrix() // ...
    {
        String[] returnMatrix = new String[this.encodedMatrix.length];
        for (int i = 0; i < returnMatrix.length; i++)
            returnMatrix[i] = this.encodedMatrix[i];
        return returnMatrix;
    }
}

Run class

/**
 * Used to test the cipher class
 */
public class CipherTest
{
    public static void main(String[] args)
    {
        Cipher hello = new Cipher("hello");
        System.out.println(hello.getText() + hello.getMatrix() + hello.getEncodedMatrix());
    }
}

Output

01101000
01100101
01101100
01101100
01101111
01001
00001
01111
10111
00000
11111
11111
00000
hello[Ljava.lang.String;@16dfc0a[Ljava.lang.String;@1bafa2b

Matrix in Mathematics vs. Array in Computer Science Just a heads up, it is meant to be blank space after array[1]

矩阵与数组

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM