2016-04-28 53 views
0

在我当前的程序中,我尝试使用非常基本的密码来解密字符串。为此,我将输入的字符串转换为二维字符数组,并向下填充。通过这样做,可以通过读取跨越行来看到实际消息。我的问题是我可以正确地填充数组,但不知道如何跨行读取并输出这些值以获取未加密的消息。如何以不同的方式读取数组,而不是填充数组

相关的代码如下所示

char[][] charArray = new char[column][row]; 
    for (int j = 0; j < row; j++) { 
     for (int i = 0; i < column; i++) {    
      if (x < input.length()) { 
       charArray[i][j] = input.charAt(x); 
       outputArea.append("[" + i + "]" + "[" + j + "]" + charArray[i][j]); 
       x++; 
      } 
      if (x >= input.length()) { 
       charArray[i][j] = ' '; 
       outputArea.append("[" + i + "]" + "[" + j + "]" + charArray[i][j]); 
       x++; 
      } 
     } 
    } 

由于我输出我读以同样的方式,我只是得到了串了一遍。

例如我想字符串PNTSLTMAAEEGIXSE显示PLAINTEXTMESSAGE

网格将如下这样

P L A我
ÑT E X
Tm值(E S)
S A G ^é

+2

从java教程开始[这里](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html) – randominstanceOfLivingThing

+0

有点像(i = 0; I <行*柱; i ++){print(char [floor(i/row)] [i%column])}我认为floor是java.math。 %是模运算符 –

回答

0

你会希望这样的事情来获得原始字符串返回:

for (int i = 0; i < row; i++) 
{ 
    for (int j = 0; j < column; j++) 
    { 
     char c = charArray[j][i]; 
     System.out.print(c); 
    } 
} 

此外,你应该有x++;做一次;否则你会用空格覆盖最后一个字符。

当x = input.length - 1;你x ++;那么如果在同一个循环迭代中,并且覆盖刚存储的字符,就会进入下一个循环。

有这样说:

for (int i = 0; i < column; i++) {    
      if (x < input.length()) { 
       charArray[i][j] = input.charAt(x); 
       outputArea.append("[" + i + "]" + "[" + j + "]" + charArray[i][j]); 
      } 
      if (x >= input.length()) { 
       charArray[i][j] = ' '; 
       outputArea.append("[" + i + "]" + "[" + j + "]" + charArray[i][j]); 
      } 

      x++; // have it here outside both if statments 
     } 
0

下面是一个完整的代码片段:

String str = "PLAINTEXTMESSAGE"; 

    // To get the row and column 
    double sqrt = Math.sqrt(str.length()); 
    int row = (int)sqrt; // be careful 
    int column = row + (sqrt % 1d > 0 ? 1 : 0); 

    // To encrypt 
    char[][] array = new char[row][column]; 

    for (int i = 0, cursor = 0; i < row * column; i++) { 
     array[i % row][i/row] = cursor < str.length() ? str.charAt(cursor++) : 0; 
    } 

    //// Just a equivalent to above 
    //for (int i = 0, cursor = 0; i < column; i++) { 
    // for (int j = 0; j < row; j++) { 
    //  array[j][i] = cursor < str.length() ? str.charAt(cursor++) : 0; 
    // } 
    //} 

    // Print the encrypted array 
    StringBuilder encrypted = new StringBuilder(); 
    for (int i = 0; i < row * column; i++) { 
     char c = array[i/column][i % column]; 
     System.out.print(c + ((i + 1) % column == 0 ? "\n" : " ")); 
     encrypted.append(c); 
    } 
    System.out.println("The encrypted string: " + encrypted); 


    // To decrypt 
    System.out.println(); 

    int decryptRow = array.length; 
    if (decryptRow < 1) { 
     throw new RuntimeException("Empty array"); 
    } 

    StringBuilder decrypted = new StringBuilder(); 
    for (int i = 0; i < row * column; i++) { 
     char c = array[i % row][i/row]; 
     System.out.print(c + ((i + 1) % row == 0 ? "\n" : " ")); 
     decrypted.append(c); 
    } 
    System.out.println("The decrypted string: " + decrypted); 

输出:

P N T S 
L T M A 
A E E G 
I X S E 
The encrypted string: PNTSLTMAAEEGIXSE 

P L A I 
N T E X 
T M E S 
S A G E 
The decrypted string: PLAINTEXTMESSAGE 
0

假设你知道numberOfColumnsnumberOfRows下面的方法将解密消息:

public String decrypt(int numberOfRows, int numberOfColumns, String encrypted) { 
    String decrypted = ""; 
    for(int c = 0; c < numberOfColumns; c++) { 
     for(int r = 0; r < numberOfRows; r++) { 
      decrypted += encrypted.charAt(numberOfColumns * (r % numberOfRows) + (c % numberOfColumns)); 
     } 
    } 
    return decrypted; 
}