Java 将输出拆分为4个字符的块和一个bruteForce方法
因此,我有一个为java任务编写的“Caesar密码”,但我缺少了2个必需的组件,而我目前掌握的java知识有限,无法理解这些组件 我需要通过将输出的字符串拆分为4个字符的块来进一步加密我的输出。 ie AFSD GRTH WRGD 我试着从标准库中查找java方法,但我还没有学习regex,我不能使用guava,因为我是通过电子邮件提交的。 这门课也还没有讲过 我还需要包括一个bruteForce方法,该方法加密我的明文,但为每个可能的密钥(0-25)输出密文。 我尝试过使用while循环,它将shiftKey设置为0,并在循环结束时将key增加1,但输出的结果非常混乱Java 将输出拆分为4个字符的块和一个bruteForce方法,java,regex,split,Java,Regex,Split,因此,我有一个为java任务编写的“Caesar密码”,但我缺少了2个必需的组件,而我目前掌握的java知识有限,无法理解这些组件 我需要通过将输出的字符串拆分为4个字符的块来进一步加密我的输出。 ie AFSD GRTH WRGD 我试着从标准库中查找java方法,但我还没有学习regex,我不能使用guava,因为我是通过电子邮件提交的。 这门课也还没有讲过 我还需要包括一个bruteForce方法,该方法加密我的明文,但为每个可能的密钥(0-25)输出密文。 我尝试过使用while循环,它
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();
}
公共字符串暴力(字符串明文){
纯文本=纯文本.replaceAll(“^A-Za-z0-9]”,“”);
字符串密文=”;
int-shiftKey=0;
while(shiftKey四块
下面是我将字符串分成四块的方法:
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;
}
打印阵列
下面是两个示例,使用暴力方法,它们都将打印出26个不同的字符串:
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]);
}
publicstaticvoidmain(字符串[]args){
CaesarCipher cipher=新的CeasarCipher();
//此变量包含26个字符串,从bruteForce()方法返回
String[]cipherText=cc.BruteForce(“这是一个示例文本”);
//对于密文中的每个字符串,打印出来
for(字符串:密文)
System.out.println(字符串);
}
公共静态void main(字符串[]args){
CaesarCipher cipher=新的CeasarCipher();
//此变量包含26个字符串,从bruteForce()方法返回
String[]cipherText=cc.BruteForce(“这是一个示例文本”);
//通过密文的索引访问密文中的每个字符串
for(int i=0;i
我希望这能有所帮助。我该如何调用/实现这些方法?我不太熟悉使用数组的方法。当我尝试用字符串[]bplainText=cc.bruteForce(纯文本)调用蛮力方法时,它输出“[Ljava.lang.String;@3b7a687b”这里有一个关于数组是什么的基本解释:我还添加了一些代码来在我的回答中打印数组顺便说一句,您得到的输出是从调用对象的toString()
方法返回的,您需要访问数组中的每个字符串
对象来单独打印它,这就是我在回答中添加的内容。
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;
}
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;
}
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]);
}