Skip to content

Commit eb76c18

Browse files
authored
Added in AlphaNumeric Encryption "AlphaNum"
I added in the AlphaNumeric encryption which is inthe Encrypt.java class. Hopefully I will be able to get an Java GUI for at least the ceaser cypher released soon. This update is for the first initial release. I know I jumped through a lot of numbers but considering where this program is at now, it feels like an full release for an console program.
1 parent bfa319e commit eb76c18

File tree

3 files changed

+140
-11
lines changed

3 files changed

+140
-11
lines changed

Encrypt.java

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package base;
2+
3+
import java.util.Arrays;
4+
5+
public class Encrypt {
6+
7+
public void encryptM (String message, int num) {
8+
9+
String t = "";
10+
for (int i = 0; i < message.length(); ++i) {
11+
char ch = message.charAt(i);
12+
if (!t.isEmpty()) {
13+
t += " ";
14+
}
15+
int n = (int)ch - (int)'a' + 1;
16+
t += String.valueOf(n);
17+
}
18+
System.out.println(t);
19+
20+
// The string you want to be an integer array.
21+
String[] integerStrings = t.split(" ");
22+
// Splits each spaced integer into a String array.
23+
int[] integers = new int[integerStrings.length];
24+
// Creates the integer array.
25+
for (int i = 0; i < integers.length; i++){
26+
integers[i] = Integer.parseInt(integerStrings[i]);
27+
//Parses the integer for each string.
28+
}
29+
30+
31+
for (int i = 0; i < integers.length; i++)
32+
integers[i] = integers[i] * num;
33+
34+
35+
System.out.println("Encrypted Message: " + Arrays.toString(integers));
36+
37+
38+
}
39+
40+
public void decryptM (int integers[], int num) {
41+
42+
43+
for (int i = 0; i < integers.length; i++)
44+
integers[i] = integers[i] / num;
45+
46+
String[] decoded = new String[integers.length];
47+
48+
for (int i = 0; i < integers.length; i++) {
49+
50+
decoded[i] = String.valueOf(Character.toChars(integers[i] + 64));
51+
52+
}
53+
54+
String decodedE = String.join("", decoded);
55+
56+
System.out.println("Decoded: " + decodedE);
57+
}
58+
59+
public int parseD (String i) throws NumberFormatException {
60+
int num = 0;
61+
try {
62+
num = Integer.parseInt(i);
63+
} catch (NumberFormatException e1) {
64+
System.out.println("Please enter a number");
65+
}
66+
return num;
67+
68+
69+
}
70+
71+
}

Main.java

Lines changed: 53 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package base;
22
import java.util.Scanner;
3-
import java.util.Random;
43

54
public class Main {
65

76

87
public static void main(String[] args) {
8+
9+
//Start the object e for AlphaNum encryptions
10+
Encrypt e = new Encrypt();
911

1012
// create a new scanner with the specified String Object
1113
Scanner scanner = new Scanner(System.in);
@@ -24,8 +26,8 @@ public static void main(String[] args) {
2426
Scanner scanner1 = new Scanner(System.in);
2527

2628
//Check how the user wants to encrypt
27-
System.out.println("How Would You Like to Encrypt? - Available Currently: Ceaser Cypher");
28-
System.out.println("For Ceaser Cypher, enter Ceaser"); //More would be added in as more encryption ways are added into the program
29+
System.out.println("How Would You Like to Encrypt? - Available Currently: Ceaser Cypher, Alpha Number Encryption");
30+
System.out.println("For Ceaser Cypher, enter Ceaser, For Alpha Number, enter AlphaNum"); //More would be added in as more encryption ways are added into the program
2931

3032
String howE = scanner.next();
3133

@@ -34,29 +36,49 @@ public static void main(String[] args) {
3436
System.out.println("What is the text you would like to encrypt?");
3537
String textE = scanner1.next();
3638

37-
System.out.println("What is the offset for the Ceaser Cypher?");
39+
System.out.println("What is the offset for the Ceaser Cypher? Enter 0 for an random offset");
3840
String numE = scanner1.next();
3941

4042
int numEi = Integer.parseInt(numE);
4143

44+
if (numEi == 0) {
45+
Ran r = new Ran();
46+
numEi = r.ran();
47+
}
48+
4249
String encrypted = Encrypter.encode(textE, numEi);
4350

44-
System.out.println("Below is encrypted Text with Ceaser Cypher, offset of " + numE);
51+
System.out.println("Below is encrypted Text with Ceaser Cypher, offset of " + numEi);
4552
System.out.println(encrypted);
4653

54+
System.out.println("please reload the application again if you want to do another calculation");
55+
4756
// close the scanner
4857
scanner1.close();
4958

50-
scanner.close();
5159

60+
} else if (howE.equals("AlphaNum")) {
61+
62+
63+
System.out.println("Please enter the message to encrypt with NO SYMBOLS(symbols will look weird afterwards) :");
64+
String s = scanner1.nextLine();
65+
66+
System.out.println("How much do you want to multiply the encryption (The bigger the better but slower) NO DECIMALS");
67+
String numS = scanner1.nextLine();
68+
int num = Integer.parseInt(numS);
69+
70+
e.encryptM(s, num);
71+
72+
5273
}
74+
5375
} else if (EorD.equals("Decrypt")){
5476

5577
System.out.println("Decrypting message");
5678

5779
//Asks the user how they want to decrypt
58-
System.out.println("How Would You Like to Decrypt? - Available Currently: Ceaser Cypher");
59-
System.out.println("For Ceaser Cypher, enter Ceaser"); //More would be added in as more encryption ways are added into the program
80+
System.out.println("How Would You Like to Decrypt? - Available Currently: Ceaser Cypher, Alpha Numeric");
81+
System.out.println("For Ceaser Cypher, enter Ceaser. For Alpha Numeric, enter AlphaNum"); //More would be added in as more encryption ways are added into the program
6082
String howD = scanner.next();
6183

6284
if(howD.equals("Ceaser")) {
@@ -74,15 +96,35 @@ public static void main(String[] args) {
7496

7597
String decrypted = Encrypter.encode(textD, newNumDi);
7698

77-
System.out.println(decrypted);
99+
System.out.println("Here is the Decrypted message with an offset of " + numDi + ": " + decrypted);
100+
101+
System.out.println("please reload the application again if you want to do another calculation");
78102

79-
System.out.println("please close and open the application again if you want to do another calculation");
103+
} else if (howD.equals("AlphaNum")){
80104

105+
System.out.println("Please enter the numbers WITHOUT THE BRACKETS");
106+
String sampleString = scanner.next();
107+
108+
String[] stringArray = sampleString.split(",");
109+
int[] intArray = new int[stringArray.length];
110+
for (int i = 0; i < stringArray.length; i++) {
111+
String numberAsString = stringArray[i];
112+
intArray[i] = Integer.parseInt(numberAsString);
113+
}
114+
System.out.println("Number of letters/integers: " + intArray.length);
115+
116+
System.out.println("Please enter the amount you multiplied:");
117+
String numS = scanner.next();
118+
int num = e.parseD(numS);
119+
e.decryptM(intArray, num);
81120

82121
}
83122

84123

85124
}
125+
//Closes everything that hasn't been previusly closed to prevent an memory leak
126+
scanner.close();
127+
86128
}
87129

88-
}
130+
}

Ran.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package base;
2+
3+
import java.util.Random;
4+
5+
public class Ran {
6+
7+
public int ran() {
8+
9+
int numEi;
10+
Random rand = new Random();
11+
numEi = rand.nextInt(24) + 1;
12+
return numEi;
13+
14+
}
15+
16+
}

0 commit comments

Comments
 (0)