如何实现Brute Force算法并修复Nullpointerexception?
How to implement BruteForce algorithm and fix Nullpointexception?
我正在练习我的 java 技能,我几乎已经完成了程序,但我在代码方面遇到了一些问题。所以我遇到的代码问题是我遇到了 Nullpointexceptions 并且不知道它在哪里。
其次,我正在尝试在我的代码中实施一种强力方法,但我在有效实施它方面遇到了问题。对于该方法,我必须查看字符串是否为英文,我已经完成但基于使用暴力方法的其他代码。我可以通过多种方式使用该方法,但不知道如何在我的代码中有效地实现它。
所以我的程序执行以下操作:
- 实施对称密码系统。
- 生成一个 16 位值密钥
- 消息是随机生成的偶数个字符串
字符。
- 加密和解密方法。
- 为此实施暴力破解攻击
使用随机生成的字符串的密码系统。
而且我只能调用Main中的方法
错误:
线程异常 "main" java.lang.NullPointerException
在 StudentSystem.bruteForce(StudentSystem.java:112)
在 StudentSystem.main(StudentSystem.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);
}
}
}
您将 class 字段与方法变量赋予相同的名称,从而混淆了它们。 NullPointerException 是因为您从未初始化 msgD
,class 字段。
所以在这里,你声明一个字段msgD
:
private static String msgD;
但随后在 decryption
方法中,您声明了一个具有相同名称的新局部变量:
String msgD;
msgD=msgE+key;
任何时候你把一个变量的 class 放在它的名字之前,你就是在声明一个新变量。我再说一遍:这是一个 new 局部变量,它与您之前声明的字段无关,即使它们具有相同的名称。
所以,在你 运行 解密方法之后,你期望 msgD
有一个值,但它没有,当你试图在这一行引用它时抛出一个 NPE :
boolean isEnglish2=msgD.matches("[a-zA-Z]");
如果删除该行
String msgD;
从 decryption
方法来看,NPE 应该消失了,但我怀疑可能还有其他潜伏的问题。
我正在练习我的 java 技能,我几乎已经完成了程序,但我在代码方面遇到了一些问题。所以我遇到的代码问题是我遇到了 Nullpointexceptions 并且不知道它在哪里。
其次,我正在尝试在我的代码中实施一种强力方法,但我在有效实施它方面遇到了问题。对于该方法,我必须查看字符串是否为英文,我已经完成但基于使用暴力方法的其他代码。我可以通过多种方式使用该方法,但不知道如何在我的代码中有效地实现它。
所以我的程序执行以下操作:
- 实施对称密码系统。
- 生成一个 16 位值密钥
- 消息是随机生成的偶数个字符串 字符。
- 加密和解密方法。
- 为此实施暴力破解攻击 使用随机生成的字符串的密码系统。
而且我只能调用Main中的方法
错误: 线程异常 "main" java.lang.NullPointerException 在 StudentSystem.bruteForce(StudentSystem.java:112) 在 StudentSystem.main(StudentSystem.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);
}
}
}
您将 class 字段与方法变量赋予相同的名称,从而混淆了它们。 NullPointerException 是因为您从未初始化 msgD
,class 字段。
所以在这里,你声明一个字段msgD
:
private static String msgD;
但随后在 decryption
方法中,您声明了一个具有相同名称的新局部变量:
String msgD;
msgD=msgE+key;
任何时候你把一个变量的 class 放在它的名字之前,你就是在声明一个新变量。我再说一遍:这是一个 new 局部变量,它与您之前声明的字段无关,即使它们具有相同的名称。
所以,在你 运行 解密方法之后,你期望 msgD
有一个值,但它没有,当你试图在这一行引用它时抛出一个 NPE :
boolean isEnglish2=msgD.matches("[a-zA-Z]");
如果删除该行
String msgD;
从 decryption
方法来看,NPE 应该消失了,但我怀疑可能还有其他潜伏的问题。