-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEncryption.java
More file actions
106 lines (106 loc) · 3.75 KB
/
Copy pathEncryption.java
File metadata and controls
106 lines (106 loc) · 3.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
//Made by Gregory Drapeau, began programing 5/4/2024 in NY, finished 7/13/2024 at YYC
//|E~[=Ej1l*l~k7mngo:[9o+~0$56~5E|P80r~:j~n5o]jlil7)0o1~,%75,=~V0/7.<1~=)99J.r,=~z%=/Z9 ,,~7<t~W:%QU=9|
import java.util.Scanner;
public class Encryption {
public static String encrypt(String string, int key){
string=crap(string, key);
string=charChange(string);
for(int i=0;i<3;i++){
string = groups(string);
string = rev(string);
}
string="|"+string+"|";
return string;
}
public static String decrypt(String string, int key){
string=string.substring(1,string.length()-1);
for(int i=0;i<3;i++) {
string = rev(string);
string = groups(string);
}
string=charChange(string);
string=deCrap(string, key);
return string;
}
public static String groups(String string){
String output="";
for(int i=0;i<(string.length())/2;i++){
output+=String.valueOf(string.charAt(i*2+1))+String.valueOf(string.charAt(i*2));
}
if(string.length()%2!=0){
output+=String.valueOf(string.charAt(string.length()-1));
}
return output;
}
public static String rev(String string){
String output="";
for(int i=string.length();i>0;i--){
output+=string.charAt(i-1);
}
return output;
}
public static String charChange(String string){
String newString="";
int high=0;
int low=65536;
boolean odd=false;
for(int i=0;i<string.length();i++){
high=Math.max(high, (int)(string.charAt(i)));
low=Math.min(low, (int)(string.charAt(i)));
}
if((high-low)%2!=0&&string.charAt(0)!='!'){
low++;
odd=true;
}
int middle=(high+low)/2;
if(string.charAt(0)=='!') {
high--;
string = string.substring(1);
}
for(int i=0;i<string.length();i++){
int ascii=(int)string.charAt(i);
char newChar=(char)(middle-(ascii-middle));
newString+=String.valueOf(newChar);
}
if(odd){
newString="!"+newString;
}
return newString;
}
public static String crap(String string, int key){
int stringLength=string.length()/key;
for(int i=1;i<=stringLength;i++){
char extra=(char)((int)(Math.random()*95+32));
string=string.substring(0,i*(key+1)-1)+String.valueOf(extra)+string.substring(i*(key+1)-1);
}
return string;
}
public static String deCrap(String string, int key){
int stringLength=string.length()/(key+1);
for(int i=1;i<=stringLength;i++){
string=string.substring(0,i*key)+string.substring(i*key+1);
}
return string;
}
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
System.out.println("Type \"en\" to encrypt a message or \"de\" to decrypt a message");
String mode = scanner.nextLine().toLowerCase();
while(mode.equals("de")||mode.equals("en")) {
System.out.print("Encryption key: ");
int key = scanner.nextInt();
scanner.nextLine();
System.out.print("Phrase: ");
String phrase = scanner.nextLine();
if(mode.equals("en")) {
String encrypt = encrypt(phrase,key);
System.out.println(encrypt);
}else {
String decrypt = decrypt(phrase,key);
System.out.println(decrypt);
}
System.out.println("\nType \"en\" to encrypt a message or \"de\" to decrypt a message");
mode=scanner.nextLine().toLowerCase();
}
}
}