需要帮助实现"矩阵轮换"在java中用于密码

时间:2016-01-31 04:59:32

标签: java arrays

我试图制作一个简单的密码程序,或多或少地将ASCII文本转换为二进制矩阵,以便" AAAA" 将成为:

compile "com.android.support:recyclerview-v7:23.1.1"

然后将其旋转以生成:

01000001  
01000001  
01000001  
01000001

我面临的问题是,似乎只要有" 0" 就不会返回任何内容,所以我最终只会:

1111  
0000  
0000  
0000  
0000  
0000  
1111  
0000

这是我的代码:

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

&#34;逆时针旋转&#34;:

01000001  
01000010  
01000011  
01000100

编辑:

这个过程是这样的:

  1. 一串ASCII字符转换为二进制ASCII等效字符

    1010  
    0110  
    0001  
    0000  
    0000  
    0000  
    1111  
    0000
    

  2. 对于这个例子,我将使用字符串&#34; ABABAC&#34;变成了:

    A - 01000001
    B - 01000010
    C - 01000011
    D - 01000100
    E - 01000101
    F - 01000110
    G - 01000111
    
    1. 矩阵由字符串组成。字符串&#34; ABABAC&#34;变成矩阵:

      A -> 01000001
      B -> 01000010
      A -> 01000001
      B -> 01000010
      A -> 01000001
      C -> 01000010
      
    2. 注意:在我的代码中,这是部分:

      01000001
      01000010
      01000001
      01000010
      01000001
      01000100
      

      text是用户输入的原始字符串,因为第一个循环j将是0或字符串文本中的第一个字符,即A,它将缓冲区定义为01000001.对于第二个循环,字符串的第二个字符文本,B将缓冲区定义为01000010.此过程循环,直到整个字符串文本转换为二进制文件。

      1. 此矩阵逆时针旋转90度,变为

        String buffer = Integer.toBinaryString(text.charAt(j));
        
      2. 在我的代码中,这部分是:

        101010
        010100
        000001
        000000
        000000
        000000
        111111
        000000
        

        这里,对于第一次迭代,情况是n是0并且j是0因此选择第一种情况并且数组a []的0位置被设置为1,因为&#34; 01000001&#的最后一个字符34;对于第二次迭代,处理字符B. B是&#34; 01000010&#34;因此,当读取最后一个字符时,返回0。 N为0,因此选择情况0但j为1,因此数组A的第二个位置设置为&#34; 0&#34;因为&#34; 01000010&#34;的最后一个字符是&#34; 0&#34;。

        换句话说,每个数组的最后一个字符依次成为第一个数组,每个数组的倒数第二个字符依次成为第二个字符串等。

        基本上我正在做的过程是要求用户输入一个字符串,将字符串转换为ASCII等价物,并运行一些嵌套的FOR循环以使数组从最后一个,然后倒数第二个,然后是第三个到最后一个等等。每个字符串的字符。我面临的问题是,每个0似乎都被发送为null而不是作为实际的0发送到数组,导致输出全部为&#34; 1&#34; s。

1 个答案:

答案 0 :(得分:1)

我如何看待通过数组的2-d循环 Matrix sorting

密码类

要制作密码,只需执行variable.getId().equals(myStudentID)

的操作即可
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

之后的空白区域

matrix vs array