-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileProcessor.java
More file actions
104 lines (87 loc) · 3.68 KB
/
Copy pathFileProcessor.java
File metadata and controls
104 lines (87 loc) · 3.68 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
import java.io.*;
public class FileProcessor {
private DES des;
public FileProcessor() {
this.des = new DES();
}
public void encryptFile(String inputPath, String outputPath, int[] key) throws IOException {
// read file as bytes (this allows us encrypt images and other files
byte[] fileData = readAllBytes(inputPath);
// pad file bytes so we can use it in our DES
// give us a bit array of our bytes
int[] paddedBits = DataUtils.padBytes(fileData);
// count DES blocks (64 bits each)
int numBlocks = paddedBits.length / 64;
// each block is 8 bytes
byte[] encrypted = new byte[numBlocks * 8];
for (int i = 0; i < numBlocks; i++) {
int[] block = new int[64];
DES.copy(paddedBits, i * 64, block, 0, 64);
int[] encBlock = des.encrypt(block, key);
byte[] encBytes = DataUtils.bitsToBytes(encBlock);
for (int j = 0; j < 8; j++) encrypted[i * 8 + j] = encBytes[j];
}
try (FileOutputStream fos = new FileOutputStream(outputPath)) {
// write to encryption file
fos.write(encrypted);
}
}
public void decryptFile(String inputPath, String outputPath, int[] key) throws IOException {
// file as bytes
byte[] fileData = readAllBytes(inputPath);
// not encrypted by us
if (fileData.length % 8 != 0) {
throw new IOException("Invalid encrypted file: size not multiple of 8 bytes");
}
int numBlocks = fileData.length / 8;
byte[] decrypted = new byte[fileData.length];
for (int i = 0; i < numBlocks; i++) {
byte[] blockBytes = new byte[8];
// bytes (we have no copy method for bytes
for (int j = 0; j < 8; j++) blockBytes[j] = fileData[i * 8 + j];
// no padding so we need separately to convert to bits
int[] block = DataUtils.bytesToBits(blockBytes);
int[] decBlock = des.decrypt(block, key);
byte[] decBytes = DataUtils.bitsToBytes(decBlock);
for (int j = 0; j < 8; j++) decrypted[i * 8 + j] = decBytes[j];
}
// remove padding if there is
byte[] unpadded = DataUtils.unpadBytes(decrypted);
// write to decryption file
try (FileOutputStream fos = new FileOutputStream(outputPath)) {
fos.write(unpadded);
}
}
// read as bytes
public static byte[] readAllBytes(String path) throws IOException {
File file = new File(path);
byte[] data = new byte[(int) file.length()];
try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file))) {
int offset = 0;
while (offset < data.length) {
int bytesRead = bis.read(data, offset, data.length - offset);
if (bytesRead == -1) break;
// move offset forward so we don't return to the first bit
offset += bytesRead;
}
}
return data;
}
public static String readString(String path) throws IOException {
return new String(readAllBytes(path)).trim();
}
public static void writeString(String path, String content) throws IOException {
try (FileWriter fw = new FileWriter(path)) {
fw.write(content);
}
}
public String getEncryptedOutputPath(String inputPath) {
return inputPath + ".des";
}
public String getDecryptedOutputPath(String encryptedPath) {
if (encryptedPath.endsWith(".des")) {
return encryptedPath.substring(0, encryptedPath.length() - 4) + ".dec";
}
return encryptedPath + ".dec";
}
}