Repository navigation
Expand file tree
/
Copy pathFileHandler.java
More file actions
142 lines (124 loc) · 4.35 KB
/
Copy pathFileHandler.java
File metadata and controls
142 lines (124 loc) · 4.35 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
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.swing.*;
import java.io.*;
public class FileHandler
{
File file;
String path;
//-----------------------------------------------------------------------------------------------------------
/*
This is the handler constructor
Input: File object
Output: None
*/
public FileHandler(File newFile)
{
this.file = newFile;
this.path = newFile.getPath();
}
//-----------------------------------------------------------------------------------------------------------
/*
This function opens the file and converts it to binary
Input: None
Output: file bytes
*/
public byte[] loadFile()
{
byte[] data = null;
try
{
FileInputStream fileInputStream = new FileInputStream(file);
data = new byte[(int) file.length()];
BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
bufferedInputStream.read(data,0,data.length);
fileInputStream.close();
bufferedInputStream.close();
}
catch (IOException ex)
{
JOptionPane.showMessageDialog(null, "SuperCryptor encountered critical error while trying to open the file", "Error!", JOptionPane.ERROR_MESSAGE);
}
return data;
}
//-----------------------------------------------------------------------------------------------------------
/*
This function converts byte array to File and saves it
Input: binary
Output: None
*/
public void saveFile(byte[] binary)
{
try
{
File outFile = new File(this.file.getParentFile(), "SuperCryptor" + this.file.getName());
FileOutputStream out= new FileOutputStream(outFile);
out.write(binary);
out.close();
}
catch (IOException ex)
{
JOptionPane.showMessageDialog(null, "SuperCryptor encountered critical error while trying to save the file", "Error!", JOptionPane.ERROR_MESSAGE);
}
}
//-----------------------------------------------------------------------------------------------------------
/*
This function saves encrypted file with decryption info
Input: binary, key and encryption type
Output: None
*/
public void saveFile(byte[] binary, int encryptionType, List<Integer> keys)
{
List<Integer> key = new ArrayList<>();
key.add(encryptionType);
key.addAll(keys);
String tmp = Globals.DELIMITER + key.toString().replaceAll(Globals.REGEX_FILTER, "") + Globals.DELIMITER;
byte[] output = new byte[tmp.getBytes().length + binary.length];
System.arraycopy(tmp.getBytes(), 0, output, 0, tmp.getBytes().length);
System.arraycopy(binary, 0, output, tmp.getBytes().length, binary.length);
saveFile(output);
}
//-----------------------------------------------------------------------------------------------------------
/*
This function loads key and encryption num from file
Input: file binary
Output: key series and encryption type (Head of list is encryption type)
*/
public List<Integer> getKeyFromFile()
{
List<Integer> res = new ArrayList<>();
byte[] binary = loadFile();
if (binary.length == 0)
{
return null;
}
if(binary[0] != Globals.DELIMITER)
{
return null;
}
for(int i = 1; i < binary.length && binary[i] != Globals.DELIMITER; i++)
{
if ((int)binary[i] != Globals.COMMA)
{
res.add((int)binary[i] - '0');
}
}
return res;
}
//-----------------------------------------------------------------------------------------------------------
/*
This function removes key from file
Input: binary data from file
Output: Cleaned file
*/
public byte[] removeKeyFromFile(byte[] binary)
{
int idx = 1;
while(idx < binary.length && binary[idx] != Globals.DELIMITER)
{
idx++;
}
return Arrays.copyOfRange(binary, idx + 1, binary.length);
}
}