So I have a "Caesar Cipher" written for a java assignment but I'm missing 2 required components that I can't figure out with the limited knowledge of Java I have thus far.
所以我有一个为使用java编写的“Caesar Cipher”,但是我缺少2个必需的组件,这些组件是我迄今为止对Java知识有限所无法理解的。
I need to further encrypt my output by splitting the the outputted string into chunks of 4 chars. ie AFSD GRTH WRGD
我需要通过将输出的字符串分成4个字符块来进一步加密输出。即AFSD GRTH WRGD
I tried looking up java methods from the standard library but I haven't learned regex yet and I can't use guava since I'm submitting this via email. This hasn't been covered in class yet either.
我尝试从标准库中查找java方法,但我还没有学习正则表达式,因为我通过电子邮件提交,所以我不能使用guava。这还没有在课堂上讨论过。
I also need to include a bruteForce method that encrypts my plainText, but outputs cipherText for every possible key (0-25). I've tried using a while loop that sets the shiftKey at 0 and increments the key by 1 at the end of the loop but that outputted a crazy mess.
我还需要包含一个加密我的plainText的bruteForce方法,但是为每个可能的键输出cipherText(0-25)。我已经尝试使用while循环将shiftKey设置为0并在循环结束时将键递增1但是输出了一个疯狂的混乱。
public String bruteForce(String plainText) {
plainText = plainText.replaceAll("[^A-Za-z0-9]", "");
String cipherText = "";
int shiftKey = 0;
while (shiftKey <= 26) {
for (int i = 0; i < plainText.length(); i++) {
int charPosition = alphabet.indexOf(plainText.charAt(i));
int keyVal = (shiftKey + charPosition) % 26;
char replaceVal = this.alphabet.charAt(keyVal);
cipherText += replaceVal;
shiftKey++;
}
}
return cipherText.toUpperCase();
}
this is my code so far
到目前为止这是我的代码
class CaesarCipher {
private final String alphabet = "abcdefghijklmnopqrstuvwxyz";
private final String ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
public String bruteForce(String plainText, int shiftKey) {
plainText = plainText.replaceAll("[^A-Za-z0-9]", "");
String cipherText = "";
for (int i = 0; i < plainText.length(); i++) {
int charPosition = alphabet.indexOf(plainText.charAt(i));
int keyVal = (shiftKey + charPosition) % 26;
char replaceVal = this.alphabet.charAt(keyVal);
cipherText += replaceVal;
}
return cipherText.toUpperCase();
}
public String encrypt(String plainText, int shiftKey) {
plainText = plainText.replaceAll("[^A-Za-z0-9]", "");
String cipherText = "";
for (int i = 0; i < plainText.length(); i++) {
int charPosition = alphabet.indexOf(plainText.charAt(i));
int keyVal = (shiftKey + charPosition) % 26;
char replaceVal = this.alphabet.charAt(keyVal);
cipherText += replaceVal;
}
return cipherText.toUpperCase();
}
public String decrypt(String cipherText, int shiftKey) {
String plainText = "";
for (int i = 0; i < cipherText.length(); i++) {
int charPosition = this.ALPHABET.indexOf(cipherText.charAt(i));
int keyVal = (charPosition - shiftKey) % 26;
if (keyVal < 0) {
keyVal = this.ALPHABET.length() + keyVal;
}
char replaceVal = this.ALPHABET.charAt(keyVal);
plainText += replaceVal;
}
return plainText.toUpperCase();
}
public static void main(String args[]) {
String plainText = "this; is s'ome te.xt with punct";
int shiftKey = 4;
CaesarCipher cc = new CaesarCipher();
String cipherText = cc.encrypt(plainText, shiftKey);
System.out.println("Plain Text :" + plainText);
System.out.println("Cipher Text :" + cipherText);
String PlainText = cc.decrypt(cipherText, shiftKey);
System.out.println("Plain Text :" + PlainText);
}
}
1 个解决方案
#1
1
Four Chunks
Here's what I've been able to come up with for splitting the string into chunks of four:
以下是我能够将字符串分成四个块的方法:
public String[] splitIntoFour(String input){
int chunkNum = 1+(input.length() / 4);
String[] chunks = new String[chunkNum];
for(int i = 0; i < chunkNum; i++){
int startIndex = i*4;
int endIndex = (i+1)*4;
if(i == (chunkNum - 1))
endIndex = input.length();
chunks[i] = input.substring(startIndex, endIndex);
}
return chunks;
}
This method takes your string, divides it's length by four and adds one, in case it's not divisible by four. It creates an array of Strings, where each element is then going to be a chunk of four letters. Then, for each chunk, you take a substring of the input, the beginning index is calculated as a multiple of four and the end index as well. Except if the loop is on it's final iteration, in which case the end index is calculated as the length of the input. This is done to account for any input which is not divisible by four.
这个方法接受你的字符串,将它的长度除以4并加一,以防它不能被4整除。它创建了一个字符串数组,其中每个元素将成为一个包含四个字母的元素。然后,对于每个块,您获取输入的子字符串,开始索引计算为四的倍数和结束索引。除非循环在最后一次迭代中,否则结束索引计算为输入的长度。这样做是为了解释任何不能被4整除的输入。
Brute Force Method
蛮力方法
As for your brute force method, you probably want to give the output as an array as well, considering you output is 26 different strings, then it's just a matter of running a loop which encrypts the input with each key by using your other encryption method, considering you implemented that correctly.
至于你的暴力方法,你可能也希望将输出作为一个数组给出,考虑到输出是26个不同的字符串,那么只需要运行一个循环,用你的其他加密方法用每个密钥加密输入。 ,考虑到你正确实现了。
public String[] bruteForce(String plainText) {
String[] cipherText = new String[26];
for(int i = 0; i < cipherText.length; i++)
cipherText[i] = encrypt(plainText, i);
return cipherText;
}
Just as a side note, brute force decryption is actually just as easy, just change the encrypt(String)
method inside the loop into your decrypt(String)
method:
正如旁注,蛮力解密实际上同样容易,只需将循环内的encrypt(String)方法更改为您的decrypt(String)方法:
public String[] bruteForceDecrypt(String cipherText) {
String[] plainText = new String[26];
for(int i = 0; i < plainText.length; i++)
plainText[i] = decrypt(cipherText, i);
return plainText;
}
Printing Arrays
Here's are two examples, using the brute force method, they will both print out twenty six different strings:
这是两个例子,使用强力方法,它们将打印出二十六个不同的字符串:
public static void main(String[] args){
CaesarCipher cipher = new CeasarCipher();
//This variable contains 26 strings, returned from the bruteForce() method
String[] cipherText = cc.BruteForce("This is an example text");
//For each string in cipherText, print it out
for(String string : cipherText)
System.out.println(string);
}
public static void main(String[] args){
CaesarCipher cipher = new CeasarCipher();
//This variable contains 26 strings, returned from the bruteForce() method
String[] cipherText = cc.BruteForce("This is an example text");
//Access each string in cipherText through it's index
for(int i = 0; i < cipherText.length; i++)
System.out.println(cipherText[i]);
}
I hope this helps.
我希望这有帮助。
#1
1
Four Chunks
Here's what I've been able to come up with for splitting the string into chunks of four:
以下是我能够将字符串分成四个块的方法:
public String[] splitIntoFour(String input){
int chunkNum = 1+(input.length() / 4);
String[] chunks = new String[chunkNum];
for(int i = 0; i < chunkNum; i++){
int startIndex = i*4;
int endIndex = (i+1)*4;
if(i == (chunkNum - 1))
endIndex = input.length();
chunks[i] = input.substring(startIndex, endIndex);
}
return chunks;
}
This method takes your string, divides it's length by four and adds one, in case it's not divisible by four. It creates an array of Strings, where each element is then going to be a chunk of four letters. Then, for each chunk, you take a substring of the input, the beginning index is calculated as a multiple of four and the end index as well. Except if the loop is on it's final iteration, in which case the end index is calculated as the length of the input. This is done to account for any input which is not divisible by four.
这个方法接受你的字符串,将它的长度除以4并加一,以防它不能被4整除。它创建了一个字符串数组,其中每个元素将成为一个包含四个字母的元素。然后,对于每个块,您获取输入的子字符串,开始索引计算为四的倍数和结束索引。除非循环在最后一次迭代中,否则结束索引计算为输入的长度。这样做是为了解释任何不能被4整除的输入。
Brute Force Method
蛮力方法
As for your brute force method, you probably want to give the output as an array as well, considering you output is 26 different strings, then it's just a matter of running a loop which encrypts the input with each key by using your other encryption method, considering you implemented that correctly.
至于你的暴力方法,你可能也希望将输出作为一个数组给出,考虑到输出是26个不同的字符串,那么只需要运行一个循环,用你的其他加密方法用每个密钥加密输入。 ,考虑到你正确实现了。
public String[] bruteForce(String plainText) {
String[] cipherText = new String[26];
for(int i = 0; i < cipherText.length; i++)
cipherText[i] = encrypt(plainText, i);
return cipherText;
}
Just as a side note, brute force decryption is actually just as easy, just change the encrypt(String)
method inside the loop into your decrypt(String)
method:
正如旁注,蛮力解密实际上同样容易,只需将循环内的encrypt(String)方法更改为您的decrypt(String)方法:
public String[] bruteForceDecrypt(String cipherText) {
String[] plainText = new String[26];
for(int i = 0; i < plainText.length; i++)
plainText[i] = decrypt(cipherText, i);
return plainText;
}
Printing Arrays
Here's are two examples, using the brute force method, they will both print out twenty six different strings:
这是两个例子,使用强力方法,它们将打印出二十六个不同的字符串:
public static void main(String[] args){
CaesarCipher cipher = new CeasarCipher();
//This variable contains 26 strings, returned from the bruteForce() method
String[] cipherText = cc.BruteForce("This is an example text");
//For each string in cipherText, print it out
for(String string : cipherText)
System.out.println(string);
}
public static void main(String[] args){
CaesarCipher cipher = new CeasarCipher();
//This variable contains 26 strings, returned from the bruteForce() method
String[] cipherText = cc.BruteForce("This is an example text");
//Access each string in cipherText through it's index
for(int i = 0; i < cipherText.length; i++)
System.out.println(cipherText[i]);
}
I hope this helps.
我希望这有帮助。