【问题标题】:How to decrypt this message using reverse encryption?如何使用反向加密解密此消息?
【发布时间】:2023-03-05 04:21:01
【问题描述】:

我本质上想反转加密来解密消息。谁能帮我创建代码来做到这一点?谢谢!!

/** * 加密和解密字符串 * @作者(安德鲁·格鲁伯) * @版本(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(){

【问题讨论】:

  • “帮助你”我假设你的意思是“我们为你做”?
  • 好吧,我真正需要的只是如何将其放回数组中并相应地将其重新排列回字符串中。我知道该怎么做,但我只需要一些帮助来编码。不,我不要求你做所有这一切。当然这会有所帮助,但我不会那样学得最好。谢谢! +redFive

标签: java arrays encryption


【解决方案1】:

这是一个转置密码,类似于transposition of a matrix. 您将消息写入一个网格,然后交换行和列,这样您就有了一个 c x r 网格,而不是一个 r x c 网格。

|wt rtb| |在哪里 | |hhtyao| |驼鹿| |伊霍卢| |那晚| --> |r ankt| -->“wt rtbhhtyaoeeholur anktemtei on n ioeig ssvs ee a” |ryone 是 | |emtei | |谈一谈| |开 | |回合 | |ioeig | |ssvs | |我一个 |

您可以看到,如果再次转置,您将恢复原始网格,从而有效地解密消息。

您已经有了填充网格、转置网格以及从其内容创建字符串的代码。只需使用正确的网格尺寸再次调用它。与其剪切'n'粘贴所有代码,不如尝试创建另一个包含该代码的函数,并使用不同的参数从您的 main 函数中调用该函数两次。

【讨论】:

    猜你喜欢
    • 2020-11-01
    • 2021-07-04
    • 1970-01-01
    • 2021-04-21
    • 2019-01-07
    • 1970-01-01
    • 1970-01-01
    • 2019-01-14
    • 1970-01-01
    相关资源
    最近更新 更多