对不起我的英语我尽我所能。
我有这个keyCode:“M O N C L E T A B D F G H I J K P Q R S U V W X Y Z”
这条消息:“鱼是没有翅膀的鸟,鸟是没有鱼鳍的鱼”
整体而言,我的程序将消息编码为两对,而新消息为“FISHXAREXBIRDSXWITHOUTXWINGSXANDXBIRDSXAREXFISHXWITHOUTXFINS”
所以基本上每两个字母都是一个数组,就像“FI”是消息[0];等等...
空格替换为“X”
我的代码中的问题是我试图找出消息中两对中每个字母的索引,然后从那里,每对二进制中的第二个字符总是转移到右边的配对和第一个角色总是转移到左边的队友所以,如果我有FI,我应该被翻译成它的右边配对(keyCode中的“I”),F应该被翻译成它的左边配合(keyCode中的“F”)“
总的来说,我应该从“FI”到“JD”结束,依此类推剩下的编码信息
这也是它的棘手部分,Z也有正确的伴侣,即“M和”M左边是“Z”
编辑:如果没有意义,基本上我想要的是根据keyCode规则将消息的字符向右或向左移动。
Edit2:发布了我的实际代码。
import java.util.Scanner;
class pairwiseCipher
{
public static void main(String[] args){
System.out.println("");
char[] keyCode;
keyCode = key();
System.out.println("");
String[] message;
message = message();
System.out.println(message);
String encodedMessage;
encodedMessage = encodedMessage(message,keyCode);
}
public static char[] key(){
Scanner sc = new Scanner(System.in);
System.out.println("Please enter a key in order to perform the opertation.");
char[] key = sc.nextLine().toUpperCase().toCharArray();
System.out.print("Keycode with alphabet: ");
for (int i = 0; i < key.length ; i++){
System.out.printf("%2c",key[i]);
}
char[] alphabet ="ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
for (int i = 0; i<alphabet.length ; i++){
System.out.printf("%2s",alphabet[i]);
}
System.out.println("");
char[] keyTotalLength = new char[key.length + alphabet.length];
int index = 0;
// Add all unique chars from firstString
for (char c : key) {
if (! contains(keyTotalLength, index, c)) {
keyTotalLength[index++] = c;
}
}
// Add all unique chars from secondString
for (char c : alphabet) {
if (! contains(keyTotalLength, index, c)) {
keyTotalLength[index++] = c;
}
}
char[] finalCode = new char[index];
for (int i = 0; i < index; i++) {
finalCode[i] = keyTotalLength[i];
}
System.out.println("");
System.out.print("Scrambled Keycode: ");
System.out.println(finalCode);
return finalCode; //This is the Scrambled keycode
}
public static boolean contains(char[] in, int index, char t) {
for (int i = 0; i < index; i++) {
if (in[i] == t) return true;
}
return false;
}
public static String[] message(){
Scanner sc = new Scanner(System.in);
System.out.println("Please enter a message in order to perform the opertation.");
String message = sc.nextLine().toUpperCase();
message = message.replaceAll("X","ks");
message = message.replaceAll(" ","X");
String[] fixedMessage = message.split("(?<=\\G.{2})");
System.out.print("The encoded Message:");
for (int i = 0 ; i < fixedMessage.length ; i++){
System.out.print(fixedMessage[i]);
System.out.print(" ");
}
System.out.println("------------");
System.out.println(fixedMessage[0]);
System.out.println(fixedMessage[0].charAt(1));
return fixedMessage;
}
// This method encodes the given text string using a Caesar
// cipher, shifting each letter by the rule "M O N C L E T A B D F G H I J K P Q R S U V WX Y Z"
public static void encode(String[] text, char[] shift) {
System.out.print("The encoded message: ");
for (int i = 0; i < text.length; i++) {
char letter = text.charAt(i);
// shift only letters (leave other characters alone)
if (letter >= 'a' && letter <= 'z') {
letter = (char) (letter + shift);
// may need to wrap around
if (letter > 'z') {
letter = (char) (letter - 26);
} else if (letter < 'a') {
letter = (char) (letter + 26);
}
}
System.out.print(letter);
}
System.out.println();
}
}
答案 0 :(得分:0)
尝试以下代码开始...... !!
public class Encrypter {
public static void main(String[] args) {
//you can take key as a space separaed string and split it into array of chars
char[] key= {'M', 'O', 'N', 'C', 'L', 'E', 'T', 'A', 'B', 'D', 'F', 'G', 'H', 'I', 'J', 'K', 'P', 'Q', 'R', 'S', 'U','V', 'W','X','Y','Z'};
char[] old = null;
char[][] neww = null;
String msg= "Fish are birds without wings and birds are fish without fins";
msg = msg.replace(' ', 'X');
System.out.println(msg);
old = msg.toCharArray();
neww = new char[msg.length()/2][2];
for(int i=0; i<msg.length()/2 ; i=i+2){
{
neww [i][0] = old[i];
neww [i][1] = old[i+1];
}
}
//this is just to display characters in pairs
for(int j=0; j<neww.length; j++){
for(int k=0; k<2; k++){
System.out.print(neww[j][k]+" ");
}
}
//Now you can write the code that replaces proper character in the message from key
//....
//....
}
}
答案 1 :(得分:0)
您可以使用java.util.HashMap构建左侧Map和右侧Map。然后,对于每个字符对,您只需输出rightMap.get(char2)+ leftMap.get(char1)。
答案 2 :(得分:0)
这是你可能想要做的事情!!
asFormula