我正在练习我的Java技能,我几乎完成了该程序,但是我对代码遇到了一些问题。因此,我所拥有的代码的问题在于我要获得nullpointExceptions,但无法弄清楚那是哪里。
其次,我正在尝试在代码中实现一种蛮力方法,但是我在有效地实施它方面遇到问题。对于该方法,我必须查看字符串是否为英语,但基于其他使用Brute Force方法的代码。我可以在许多方面使用该方法,但不知道在我的代码中有效地实现它。
所以我的程序执行以下操作:
- 实现对称密码系统。
- 生成16位值键
- 消息是随机生成的字符串,偶数字符。
- 加密并解密方法。
- 为此实施蛮力方法解密使用随机生成的字符串的密码系统。
-
我也只能在main
中调用方法错误:线程" main" java.lang.nullpointerexception中的异常 在sudendyStem.bruteforce(sudendyStem.java:112( 在sudendyystem.main(sudendyStem.java:38(
代码:
private static String msg;
private static String msgE;
private static String msgD;
private static int key;
//This is the String for the program. So Upper is the chracters in upper letters
private static String upper="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
//This is for the chracters in the lower case
private static String lower=upper.toLowerCase();
private static String space=" ";
//Alphanum is upper and lower and space combined and this is what I'll be using for this project.
private static String alphanum=upper+lower+space;
public static void main(String[] args){
//TODO: You can only call methods in main method
key = generateKey();
msg = generateMsg();
msgE = encryption(key,msg);
bruteForce(msgE);
}
private static int generateKey() {
//TODO: implement step a (randomly generate 16-bit key)
//So in Decimal a key would be 2^16-1
//So i'm using random rand to generate a key which is going to be a int and in power of 2^16 -1
Random rand=new Random();
return rand.nextInt((int) (Math.pow(2, 16)-1));
}
private static String generateMsg() {
//TODO: implement step b (randonly generate a string with an even number of characters)
//For this method i'm going to generate a string which is random
//This string is going to use the above alphanum.
StringBuilder builder =new StringBuilder();
//The while loop states while the builder length is divisible by 2 then you can print the random string
while(builder.length()%2!=0) {
//The chracter is using the random rand and alphanum length to generate the String
int character=(int)(Math.random()*alphanum.length());
//Builder append is shortering the string into something more simple.
builder.append(alphanum);
}
return builder.toString() ;
}
private static String encryption(int key, String msg) {
//TODO: implement step c (encrypt the message)
//To encrypt the string we're going to use the key we generated before
/*The String Key is going to take the key and put that into a string*/
//then the loop is going to go through and put the String Key to generate a key
String Key=Integer.toString(key);
for(int i=0;i<=msg.length()/2;i++) {
Key+=Key;
}
// This will return a mesage and a key at the same time
return (msg+key);
}
private static void decryption(int key, String msgE) {
//TODO: implement step d (decryption)
//For decryption we're going to use the key we got before and go through the loop
//We're going to go through the loop and put the String into String Key
//Then we're going to return the String with the key.
String Key=Integer.toString(key);
for(int i=0;i<msgE.length()/2;i++) {
Key+=Key;
}
String msgD;
msgD=msgE+key;
}
private static void bruteForce(String msgE) {
//TODO: implement bruteForce algorithm, you may need the above decryption(key,msgE) method
boolean isEnglish=msgE.matches("[a-zA-Z]+");
if(isEnglish) {
System.out.println("This string is English");
}else {
System.out.println("This String is not English at all");
}
decryption(key,msgE);
boolean isEnglish2=msgD.matches("[a-zA-Z]");
if(isEnglish2) {
System.out.println("Encrypted Message is English: "+msgD);
}
else {
System.out.println("The message is not english at all: "+msgD);
}
}
}
您通过给出相同的名称,将类字段与方法变量混淆。NullPoInterException是因为您从未初始化msgD
,类字段。
所以在这里,您声明一个字段msgD
:
private static String msgD;
但是在decryption
方法中,您声明了一个具有相同名称的新的本地变量:
String msgD;
msgD=msgE+key;
任何时候将变量的类放在其名称之前时,您都会声明一个新变量。我重复:这是a new 本地变量,它与您以前已宣布的字段无关,即使它们具有相同的名称。
因此,在运行解密方法后,您期望msgD
具有值,但是当您尝试在此行上引用它时,它不会抛出NPE:
boolean isEnglish2=msgD.matches("[a-zA-Z]");
如果您删除该行
String msgD;
从decryption
方法中,NPE应该消失,但我怀疑可能有其他问题。