簡體   English   中英

需要幫助在Java中實現“矩陣旋轉”以實現密碼

[英]Need help implementing “matrix rotation” in java for cipher

我正在嘗試制作一個簡單的密碼程序,該程序或多或少將ASCII文本轉換為二進制矩陣,因此“ AAAA”將變為:

01000001  
01000001  
01000001  
01000001

然后旋轉它來制作:

1111  
0000  
0000  
0000  
0000  
0000  
1111  
0000

我面臨的問題是,每當出現“ 0”時,什么都不會返回,所以我最終得到的只是:

1111  
1111

這是我的代碼:

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) {
        }
    }
}

這是我希望它如何工作的示例:

輸入的字符串:

ABCD

第一步:

01000001  
01000010  
01000011  
01000100

“逆時針旋轉”:

1010  
0110  
0001  
0000  
0000  
0000  
1111  
0000

編輯:

該過程如下所示:

  1. 一串ASCII字符將轉換為等效的二進制ASCII

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

    等等

在此示例中,我將使用字符串“ ABABAC”轉換為:

A -> 01000001
B -> 01000010
A -> 01000001
B -> 01000010
A -> 01000001
C -> 01000010
  1. 由字符串組成一個矩陣。 字符串“ ABABAC”變成矩陣:

     01000001 01000010 01000001 01000010 01000001 01000100 

注意:在我的代碼中,這是部分:

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

text是用戶輸入的原始字符串,對於第一個循環,j將為0或字符串文本中的第一個字符為A,它將緩沖區定義為01000001。對於第二個循環,字符串文本的第二個字符為B將緩沖區定義為01000010。此過程將循環直到整個字符串文本都轉換為二進制。

  1. 該矩陣逆時針旋轉90度成為

     101010 010100 000001 000000 000000 000000 111111 000000 

在我的代碼中,這部分是:

                    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;

在這里,對於第一次迭代,情況為n為0,j為0,因此選擇了第一種情況,並且數組a []的0位置被設置為1,因為“ 01000001”的最后一個字符為1。第二次迭代,處理字符B。 B是“ 01000010”,因此當讀取最后一個字符時,將返回0。 N為0,因此選擇情況0,但j為1,因此數組A的第二個位置設置為“ 0”,因為“ 01000010”的最后一個字符為“ 0”。

換句話說,每個數組的最后一個字符依次成為第一個數組,每個數組的第二個到最后一個字符依次成為第二個字符串等。

從本質上講,我正在執行的過程是要求用戶輸入字符串,將字符串轉換為ASCII等效字符串,並運行一些嵌套的FOR循環以使數組從最后開始,然后到最后,再到最后,等等。串。 我面臨的問題是,每個0似乎都作為null發送,而不是作為實際0發送到數組,從而導致輸出均為“ 1”。

我如何可視化通過數組的二維循環 矩陣排序

密碼類

要進行加密,只需按照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;
    }
}

跑步課

/**
 * 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());
    }
}

輸出量

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

數學中的矩陣與計算機科學中的數組 只是一個提示,它的含義是array[1]后的空白array[1]

矩陣與數組

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM