-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEncryptor.java
More file actions
169 lines (131 loc) · 4.78 KB
/
Copy pathEncryptor.java
File metadata and controls
169 lines (131 loc) · 4.78 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import java.lang.reflect.Array;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;
public class Encryptor {
public static ArrayList<Integer> ids;
public static ArrayList<Node> nodes;
public static ArrayList<Node> actualOrder;
public static SecureRandom randGenerator;
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
ids=new ArrayList<Integer>();
nodes=new ArrayList<Node>();
actualOrder=new ArrayList<Node>();
String inputString = scan.next(); //Will delete
int[] myKey = new int[100];
//String inputString = "aaaabbb"; //For testing
randGenerator = new SecureRandom();
Node lastNode;
Node temp = new Node(uniqueRandomId(), false);
lastNode=temp;
nodes.add(temp);
for (int i = 0; i < 98; i++){
//create new ID and check if it's taken already
temp = new Node(uniqueRandomId(), false);
nodes.add(temp);
lastNode.addEdge(temp);
lastNode=temp;
}
temp = new Node(uniqueRandomId(), true);
nodes.add(temp);
//insecurely save key for proof of concept and then randomize order of other array
//actualOrder=nodes; //doesn't work because java
for(int i=0;i<nodes.size();i++){
actualOrder.add(nodes.get(i));
//System.out.println(actualOrder.get(i).getShift());
myKey[i] = actualOrder.get(i).getShift();
}
//sort nodes to be random
Collections.sort(nodes);
//NOW DO TESTING/OUTPUT
//System.out.println(nodes);
//System.out.println(actualOrder);
//System.out.println(nodes==actualOrder);
//System.out.println("Testing Encrypt: ");
//System.out.println(encrypt(inputString));
String encrypted = encrypt(inputString.toLowerCase());
Node tempNode;
for(int i = 0; i<100; i++)
{
tempNode = nodes.get(i);
for(int j = 0; j < 20; j++)
tempNode.addEdge(nodes.get(randGenerator.nextInt(100)));
}
System.out.println(inputString);
System.out.println("Encrypt: " + encrypted);
System.out.println("Decrypt: " + decrypt(actualOrder.get(0), myKey, encrypted));
}
public static String encrypt(String inputString){
char[] encryptString = new char[100];
int[] shiftArray = new int[100]; // Holding shift values
char[] alphabet = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
//System.out.println(inputString.length());
int numRandLetters = (100-inputString.length()); // finding out how many random chars to add to array
//System.out.println(numRandLetters);
char[] StringAndExtraSpace = new char[100];
// Filling the array with shifts
for(int j = 0; j < 100; j++){
if (j <= (inputString.length()-1)){
StringAndExtraSpace[j] = inputString.charAt(j);
}
else{
StringAndExtraSpace[j] = alphabet[randGenerator.nextInt(alphabet.length)]; //length of accepted alphabet
}
//System.out.print(StringAndExtraSpace[j]);
}
//System.out.println();
for(int i = 0; i < 100; i++){
//System.out.println(new String(alphabet).indexOf(StringAndExtraSpace[i]));
int shift = actualOrder.get(i).getShift();
encryptString[i] = alphabet[(new String(alphabet).indexOf(StringAndExtraSpace[i])+shift)%alphabet.length];
shiftArray[i] = shift;
}
//System.out.println(encryptString);
return (new String(encryptString));
}
public static int uniqueRandomId() {
int newId;
if(nodes.isEmpty()){
newId= randGenerator.nextInt(10000);
}else{
newId= randGenerator.nextInt(10000);
while(ids.contains((Integer) newId)){
newId= randGenerator.nextInt(10000);
}
}
return newId;
}
public static String decrypt(Node start, int[] key, String toBeEncrypted){
//storage for the iterator
Node temp;
temp = start;
char[] encrypted = new char[100];
// Filling the array with shifts
for(int j = 0; j < 99; j++){
encrypted[j] = toBeEncrypted.charAt(j);
}
//System.out.print(StringAndExtraSpace[j]);
//for the entire key, get the next node and encrypt
for(int i = 0; i<100; i++)
{
//shift the ith element by the node's shift value
encrypted[i] = shift(encrypted[i], actualOrder.get(i).getShift());
//temp = temp.nextNode(key[i]);
}
String b = new String(encrypted);
return b;
}
public static char shift(char tobeShifted, int shiftNum)
{
char[] alphabet = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
//System.out.println(shiftNum);
int desiredIndex = (new String(alphabet)).indexOf(tobeShifted)-shiftNum;
if(desiredIndex <0)
desiredIndex += 26 ;
return alphabet[desiredIndex];
}
}