2016-01-13 83 views
-4

我基本上想要反轉加密來解密messgae。任何人都可以幫助我創建代碼來做到這一點?謝謝!!如何使用反向加密解密此消息?

/** * 加密和解密string * @author(安德魯·格魯伯) * @version(2016年1月11日) */

公共類邏輯

{

public static void main(String[] args){ 
    //Encryption 
    String mm= "where is the moose that everyone is talking about"; 
    final int nr=6; //number or rows 
    int nc; //number of columns 

    nc=mm.length()/nr; 
    if(mm.length()% nr !=0){ 
     nc++; 
    } 

    while(mm.length()<nr*nc){ //insert spaces 
     mm+=" "; 
    } 

    String[][] ar=new String [nr][nc]; 

    for(int i=0; i<mm.length(); i++){ 
     //eaach character in the string is stored 
     ar[i%nr][i/nr]=mm.substring(i,i+1); 

    } 

    String outStr=""; //output string 
    for(int r=0; r<nr; r++){ 
     for(int c=0; c<nc; c++){ 
      outStr+=ar[r][c]; 
     } 
    } 
    System.out.println(outStr); 
    //Decryption 

    String test=outStr; 
    Decyrption(); 


} 
static void Decyrption(){ 
+0

通過「幫助你」我假設你的意思是「我們爲你做」嗎? – redFIVE

+0

好吧,我真正需要的是如何將它放回數組並將其歸類到字符串中的想法。我知道該怎麼做,但我只需要一些編碼幫助。沒有我不要求你做這一切。當然這會有所幫助,但我不會學習最好的方式。謝謝! + redFive –

回答

0

這是一個轉置密碼,類似於transposition of a matrix.您將消息寫入網格,然後交換行和列,以便代替rxc網格,您可以c x r網格。

 
        |wt rtb| 
|where is |  |hhtyao| 
|the moose|  |eeholu| 
| that eve| --> |r ankt| --> "wt rtbhhtyaoeeholur anktemtei o n ioeig ssvs ee a " 
|ryone is |  |emtei | 
|talking a|  | o n | 
|bout  |  |ioeig | 
        |ssvs | 
        | ee a | 

您可以看到,如果您再次轉置,您將返回原始網格,有效地解密該消息。

您已經擁有填充網格,轉置它以及從其內容創建字符串的代碼。只需使用正確的網格維度再次調用它。嘗試創建另一個包含代碼的函數,而不是使用所有代碼的「n」代碼,並使用不同的參數從函數中調用該函數兩次。

+0

非常感謝!非常感激 –