Java 行换位密码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19894395/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 21:06:51  来源:igfitidea点击:

Row Transposition Cipher

javaencryption

提问by zohaibbadarpura

I am trying to write a program to implement a transposition cipher. When the user provides the key 'TAPE' and the message "HelloworldMessage" my program is only showing only Hell, owor, ldMeand ssagand skipping the last "e" from "message," which is wrong. The output should be:

我正在尝试编写一个程序来实现换位密码。当用户提供了关键的“TAPE”和信息“HelloworldMessage”我的计划是只只显示HelloworldMessag以及跳过从“信息”,这是不对的最后一个“E”。输出应该是:

Hell
owor
ldMe
ssag
exxx

and xxxshould complete the message before encrypting. What am I doing wrong?

并且xxx应该在加密之前完成消息。我究竟做错了什么?

import java.util.*;
import java.util.Scanner;  // needed for Scanner

public class transpositionCipher
{

public static void main(String args[]) 
{
String key;
String message;
String encryptedMessage; 
// Letters in the x-axis
int x=0; 
// Letters in the y-axis
int y=0; 


// Prompt the user
System.out.print( "Type your Key : " );


// Read a line of text from the user.

Scanner scan = new Scanner(System.in);
key = scan.nextLine();

// Display the input back to the user.
System.out.println( "Your Key is " + key );


//Prompt the user
System.out.print( "Type your Message : " );


//Read a line of text from the user.
message = scan.nextLine();

//Display the input back to the user.
System.out.println( "Your Message is " + message );



encryptedMessage = "";

// To set the temp as [x][y]
char temp[][]=new char [key.length()][message.length()];
char msg[] = message.toCharArray();
// To populate the array
x=0;
y=0;
// To convert the message into an array of char
for (int i=0; i< msg.length;i++)
{
temp[x][y]=msg[i];
if (x==(key.length()-1)) 
{
x=0;
y=y+1;
} // Close if 
else 
{
x++;
}
} // Close for loop

// To sort the key
char t[]=new char [key.length()];
t=key.toCharArray();
Arrays.sort(t);

for (int j=0;j<y;j++)
{
for (int i=0;i<key.length();i++)
{
System.out.print(temp[i][j]);
}
System.out.println();
}

System.out.println();

// To print out row by row (i.e. y)
for (int j=0;j<y;j++){ 
// To compare the the sorted Key with the key
// For char in the key
for (int i=0;i<key.length();i++){ 
int pos=0;
// To get the position of key.charAt(i) from sorted key
for (pos=0;pos<t.length;pos++){ 
if (key.charAt(i)==t[pos]){ 
// To break the for loop once the key is found
break;
}
}
System.out.print(temp[pos][j]);
encryptedMessage+=temp[pos][j];
}
System.out.println();
}

System.out.println(encryptedMessage);
System.exit(0);
}
}

OUTPUT

输出

Type your Key : tape
Your Key is tape
Type your Message : HelloworldMessage
Your Message is HelloworldMessage
Hell
owor
ldMe
ssag

lHle
roow
elMd
gsas
lHleroowelMdgsas

采纳答案by zohaibbadarpura

Finally sort this problem on my own.....

终于自己解决了这个问题......

import java.util.*;
import java.util.Scanner;  // needed for Scanner

public class transpositionCipher
{

public static void main(String args[]) 
{
String key;
String message;
String encryptedMessage; 
// Letters in the x-axis
int x=0; 
// Letters in the y-axis
int y=0; 


// Prompt the user
System.out.print( "Type your Key : " );


// Read a line of text from the user.

Scanner scan = new Scanner(System.in);
key = scan.nextLine();

// Display the input back to the user.
System.out.println( "Your Key is " + key );


//Prompt the user
System.out.print( "Type your Message : " );


//Read a line of text from the user.
message = scan.nextLine();

//Display the input back to the user.
System.out.println( "Your Message is " + message );


int msgchar = message.length();
int keycahr = key.length();

if (!((msgchar % keycahr) == 0)){

do{
message = message + "x";
msgchar = message.length();
}while(!((msgchar % keycahr) == 0));

}


encryptedMessage = "";

// To set the temp as [x][y]
char temp[][]=new char [key.length()][message.length()];
char msg[] = message.toCharArray();
// To populate the array
x=0;
y=0;
// To convert the message into an array of char
for (int i=0; i< msg.length;i++)
{
temp[x][y]=msg[i];
if (x==(key.length()-1)) 
{
x=0;
y=y+1;
} // Close if 
else 
{
x++;
}
} // Close for loop

// To sort the key
char t[]=new char [key.length()];
t=key.toCharArray();
Arrays.sort(t);

for (int j=0;j<y;j++)
{
for (int i=0;i<key.length();i++)
{
System.out.print(temp[i][j]);
}
System.out.println();
}

System.out.println();

// To print out row by row (i.e. y)
for (int j=0;j<y;j++){ 
// To compare the the sorted Key with the key
// For char in the key
for (int i=0;i<key.length();i++){ 
int pos=0;
// To get the position of key.charAt(i) from sorted key
for (pos=0;pos<t.length;pos++){ 
if (key.charAt(i)==t[pos]){ 
// To break the for loop once the key is found
break;
}
}
System.out.print(temp[pos][j]);
encryptedMessage+=temp[pos][j];
}
System.out.println();
}

System.out.println(encryptedMessage);
System.exit(0);
}
}