I'm trying to practice my java skills and i'm writing a program that does the following:
- Implement a symmetric cryptosystem.
- Generate a 16 bit value key
- Messages are randomly generated string with an even number of characters.
- Encrypt and Decrypt a method.
- Implement a brute force method decryption of attack for this cryptosystem using the randomly generated String.
- Also I can only call methods in Main
So in my code I'm pretty much done. The problem that I'm having is when I generate the random String. The String is not going through the Encryption and Decryption methods in my code. The methods I have for encryption and Decryption would be perfect if it passed the String along the way.
The method I have in this code is completely diffrent then other encryption methods. This encryption method works but I just need to pass the String.
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 empty="";
int xor;
char temp;
for(int i=0;i<msg.length();i++) {
xor=msg.charAt(i)^key;
temp=(char)xor;
empty=empty+temp;
}
// This will return a encrypted message.
msg=empty;
return msg;
}
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 empty="";
int xor;
char temp;
for(int i=0;i<msgE.length();i++) {
xor=msgE.charAt(i)^key;
temp=(char)xor;
empty=empty+temp;
}
msgE=empty;
}
private static void bruteForce(String msgE) {
//TODO: implement bruteForce algorithm, you may need the above decryption(key,msgE) method
decryption(key, msgE);
boolean isEnglish=msgE.matches("[a-zA-Z]");
if(isEnglish) {
System.out.println("This string is English: "+msgE);
}else {
System.out.println("This String is not English at all: "+msgE);
}
}}
Aucun commentaire:
Enregistrer un commentaire