这是我第一次在这里发帖。我一直在使用其他的论坛帖子来寻找答案,但我似乎无法弄清楚如何处理这个问题:
分配是实现一个简单的密码。 (注意,这种方法不使用java中构建的任何实际密码方法。老实说,我自己从未使用过这些方法。)我们的想法是使用Ciphers来教授多个数组的概念。以下是我理解的步骤:
EX:
纯文本争夺密码
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);
}
}
这是与老师的解决方案相同的课程。它太复杂了,我甚至不确定如何达到这个解决方案。人们可以推荐一些资源或解释她是如何到达这里的吗?以下解决方案有效,除了现在它抛出一个数组超出范围
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(" ");
}
}
}