这是我第一次在这里发帖。我一直在使用其他论坛主题的答案,但我似乎无法弄清楚如何解决这个问题:
任务是实现一个简单的密码。(请注意,此方法不使用 java 中内置的任何实际密码方法。老实说,我自己从未使用过这些方法。)这个想法是使用密码来教授多个数组的概念。以下是我理解的步骤:
- 用户输入文本
- 给定一个关键字,将明文放入一个模块化表(多维数组)中
- 关键字确定数组中的列数和行数。出于本示例的目的:NEIUP2
- 在单独的加密方法中,使用随机方法拼写关键字
- 将明文分配给这个新的数组表,使用相同的位置创建密文
例如:
纯文本加扰密码 NEIUP2 NUP2IE hello1 hlo1le 234567 256743
- 最后一步是取关键字(自己的数组)新加扰的关键字(另一个新数组),将密文传回类方法解密
这是我有点困惑的步骤
据我从代码中可以看出,我们应该获取关键字和密文的值,然后对其进行解读。为此,我应该将关键字与 scramble 关键字进行比较,并将密文放入一个新的、整洁的表中。
这是她给我们的代码。我试图自己解决这个问题,但我似乎无法弄清楚如何将这些步骤转换为代码。
我认为我的第一步是查找/比较两个数组(原始关键字)和(运行时新的混乱关键字)
无解的代码
import java.util.Scanner;
import java.util.Random;
/**
* Class Cipher:
*
* Template for Cipher
*/
class Cipher
{
/**
* Attributes
*/
private char[][] Table = new char[5][6];
//added an attribute to decrypt message
private char[] shuffleKey =new char[6];
/*
* Constructor (could be one of more)
*/
Cipher()
{
int i,j;
for (i=0; i<5 ;i=i+1)
{
for (j=0; j<6 ;j=j+1)
{
Table[i][j]='x';
}
}
}
/**
* Compute encryption
*/
public char[] encryption(char[] key, Cipher newTable)
{
char[] emessage = new char[5*6];
int[] newkeyIndex = new int[6];
int i,j,k;
// set message to table
setAttribute();
// shuffle keyword
newkeyIndex = shuffle(key);
// change columns according to shuffle keyword
for (i=0; i<6 ;i=i+1)
{
for (j=0; j<5 ;j=j+1)
{
newTable.setLetter(j,i,Table[j][newkeyIndex[i]]);
}
}
//check encryption process
displayTable();
System.out.println("new table ");
displayShuffleKeyword(6,key,newkeyIndex);
newTable.displayTable();
//copy table to encrypted message
k=0;
for (i=0; i<5 ;i=i+1)
{
for (j=0; j<6 ;j=j+1)
{
emessage [k]=newTable.getLetter(i,j);
k = k + 1;
}
}
return(emessage);
}
/**
* Compute decryption
*/
public char[] decryption(char[] key, char[] ciphertext)
{
char[] shufflekeyword = new char[6];
char[] plaintext = new char[5*6];
int i,j,k;
return(plaintext);
}
/**
* Set attributes
*/
public int setAttribute()
{
int i,j,k;
Scanner input = new Scanner(System.in);
char[] message;
System.out.println("type message");
String inputMessage = input.next(); //The user defined message to scramble
message = inputMessage.toCharArray(); //put into the character array 'message'
k = 0;
Boolean temp = (k!=inputMessage.length());
for (i=0; (i<5 && temp==true) ;i=i+1)
{
for (j=0; (j<6 && temp==true) ;j=j+1)
{
if (message[k]!='\0')
{
Table[i][j]= message[k];
k = k+ 1;
temp = (k!=inputMessage.length());
}
}
}
return(k);
}
/**
* Get a single value
*/
public char getLetter(int indexI, int indexJ)
{
return(Table[indexI][indexJ]);
}
/**
* Set a single value
*/
public void setLetter(int indexI, int indexJ, char letter)
{
Table[indexI][indexJ] = letter;
}
/**
* Display Shuffle keyword private
*/
private void displayShuffleKeyword(int size, char[] key,int[] indexArray)
{
int i;
for (i=0; i<size ;i=i+1)
{
System.out.print(key[indexArray[i]]);
}
System.out.println();
}
/**
* Display array of char
*/
public static void displayArrayChar(int size, char[] message)
{
int i;
for (i=0; i<size ;i=i+1)
{
System.out.print(message[i]);
}
System.out.println();
}
/**
* Display array of int
*/
public static void displayArrayInt(int size, int[] message)
{
int i;
for (i=0; i<size; i=i+1)
{
System.out.print(message[i]);
}
System.out.println();
}
/**
* Compute Shuffle Index of Keyword
*/
private int[] shuffle(char[] key)
{
int n = 6; // keyword length
int[] shuffleIndex = new int[n];
Random random = new Random();
int shift = random.nextInt();
for (int i = 0; i < n; i=i+1)
{
shuffleIndex[i] = ( Math.abs((n - shift + i)) % n);
}
return(shuffleIndex);
}
/**
* Method that prints out all attributes by row
*/
private void displayTable()
{
int i,j;
for (i=0; i<5 ;i=i+1)
{
for (j=0; j<6 ;j=j+1)
{
System.out.print(Table[i][j]);
}
System.out.println(" ");
}
}
/**
* Method that prints out all attributes by row
*/
private void displayTable2()
{
int i,j;
for (i=0; i<6 ;i=i+1)
{
for (j=0; j<5 ;j=j+1)
{
System.out.print(Table[j][i]);
}
System.out.println(" ");
}
}
}
这是主要的方法解决方案:
import java.util.Scanner;
import java.util.Random;
/**
* Class CipherMain:
*
* Program to test class CipherMain
*/
class CipherMain
{
/**
* main method for testing
*/
public static void main(String[] args)
{
//Declarations of variables
char [] keyword = new char[7];
//Declarations of objects
Cipher matrix = new Cipher();
Cipher newmatrix = new Cipher();
//Create keyword students can research about Class Console
//Console allows to type passwords and returns char[]
keyword = "NEIUP2".toCharArray();
Cipher.displayArrayChar(6,keyword);
//Call encryption
char[] ciphertext = matrix.encryption(keyword, newmatrix);
Cipher.displayArrayChar((6*6),ciphertext);
//Call decryption
char[] plaintext = matrix. decryption(keyword, ciphertext);
Cipher.displayArrayChar(6*6,plaintext);
}
}
这是与老师的解决方案相同的课程。它是如此复杂,我什至不知道如何得到这个解决方案。人们可以推荐一些资源或解释她是如何来到这里的吗?以下解决方案有效,但现在它会抛出一个 Array Out of bounds
import java.util.Scanner;
import java.util.Random;
/**
* Class Cipher:
*
* Template for Cipher
*/
class Cipher
{
/**
* Attributes
*/
private char[][] Table = new char[5][6];
//added an attribute to decrypt message
private char[] shuffleKey =new char[6];
/**
* Constructor (could be one of more)
*/
Cipher()
{
int i,j;
for (i=0; i<5 ;i=i+1)
{
for (j=0; j<6 ;j=j+1)
{
Table[i][j]='x';
}
}
}
/**
* Compute decryption
*/
public char[] decryption(char[] key, char[] ciphertext)
{
// Need extra variables and objects
Cipher cipherTable = new Cipher();
Cipher ordercipherTable = new Cipher();
char[] plaintext = new char[5*6];
Boolean temp= true;
int[] keyIndex = new int[6];
int i,j,k;
// You need to obtain the shuffle keyword from the ciphertext
// We assume the shuffle keyword is at the end of the ciphertext
// grab shuffle keyword
for (j=0; j<6; j=j+1)
{
shuffleKey[j] = ciphertext[30+j];
}
// Once you have shuffle keyword you need to create from the
// ciphertext a table that will be use to decrypt message
// transform ciphertext to a table form
k=0;
for (i=0; i<6; i=i+1)
{
for (j=0; j<5 ;j=j+1)
{
cipherTable.setLetter(i,j, ciphertext[k]);
k= k+1;
}
}
// find order to correctly decrypt message by creating
// an index array that has order of columns
for (i=0; i<6; i=i+1)
{
temp = true;
for (j=0; (j<6 && temp==true); j=j+1)
{
if (key[j] == shuffleKey[i])
{
keyIndex[i] = j;
temp = false;
}
}
}
//reorder ciphertext according to keyIndex
for (i=0; i<6; i=i+1)
{
for (j=0; j<5; j=j+1)
{
ordercipherTable.setLetter(j,keyIndex[i],cipherTable.getLetter(j,i));
}
}
//copy plaintext from order ciphertext
k=0;
for (i=0; i<6 ;i=i+1)
{
for (j=0; j<5 ;j=j+1)
{
plaintext [k]=ordercipherTable.getLetter(i,j);
k = k + 1;
}
}
return(plaintext);
}
/**
* Compute encryption
*/
public char[] encryption(char[] key, Cipher newTable)
{
char[] emessage = new char[6*6];
int[] newkeyIndex = new int[6];
int i,j,k;
// set message to table
setAttribute();
// shuffle keyword
newkeyIndex = shuffle(key);
// change columns according to shuffle keyword
for (i=0; i<6 ;i=i+1)
{
for (j=0; j<6 ;j=j+1)
{
newTable.setLetter(j,i,Table[j][newkeyIndex[i]]);
}
}
//check encryption process
//displayTable();
//System.out.println("new table ");
//displayShuffleKeyword(6,key,newkeyIndex);
//newTable.displayTable();
//copy table to encrypted message
k=0;
for (i=0; i<6 ;i=i+1)
{
for (j=0; j<5 ;j=j+1)
{
emessage [k]=newTable.getLetter(i,j);
k = k + 1;
}
}
// need to add shuffle key at the end of cipher text
for (j=0; j<6; j=j+1)
{
emessage[30+j] = key[newkeyIndex[j]];
}
return(emessage);
}
/**
* Set attributes
*/
public int setAttribute()
{
int i,j,k;
Scanner input = new Scanner(System.in);
char[] message;
System.out.println("type message");
String inputMessage = input.next();
message = inputMessage.toCharArray();
k = 0;
Boolean temp = (k!=inputMessage.length());
for (i=0; (i<5 && temp==true) ;i=i+1)
{
for (j=0; (j<6 && temp==true) ;j=j+1)
{
if (message[k]!='\0')
{
Table[i][j]= message[k];
k = k+ 1;
temp = (k!=inputMessage.length());
}
}
}
return(k);
}
/**
* Get a single value
*/
public char getLetter(int indexI, int indexJ)
{
return(Table[indexI][indexJ]);
}
/**
* Set a single value
*/
public void setLetter(int indexI, int indexJ, char letter)
{
Table[indexI][indexJ] = letter;
}
/**
* Display Shuffle keyword private
*/
private void displayShuffleKeyword(int size, char[] key,int[] indexArray)
{
int i;
for (i=0; i<size ;i=i+1)
{
System.out.print(key[indexArray[i]]);
}
System.out.println();
}
/**
* Display array of char
*/
public static void displayArrayChar(int size, char[] message)
{
int i;
for (i=0; i<size ;i=i+1)
{
System.out.print(message[i]);
}
System.out.println();
}
/**
* Display array of int
*/
public static void displayArrayInt(int size, int[] message)
{
int i;
for (i=0; i<size; i=i+1)
{
System.out.print(message[i]);
}
System.out.println();
}
/**
* Compute Shuffle Index of Keyword
*/
private int[] shuffle(char[] key)
{
int n = 6; // keyword length
int[] shuffleIndex = new int[n];
Random random = new Random();
int shift = random.nextInt();
for (int i = 0; i < n; i=i+1)
{
shuffleIndex[i] = ( Math.abs((n - shift + i)) % n);
}
return(shuffleIndex);
}
/**
* Method that prints out all attributes by row
*/
private void displayTable()
{
int i,j;
for (i=0; i<5 ;i=i+1)
{
for (j=0; j<6 ;j=j+1)
{
System.out.print(Table[i][j]);
}
System.out.println(" ");
}
}
/**
* Method that prints out all attributes by row
*/
private void displayTable2()
{
int i,j;
for (i=0; i<6 ;i=i+1)
{
for (j=0; j<5 ;j=j+1)
{
System.out.print(Table[j][i]);
}
System.out.println(" ");
}
}
}