-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpoc.java
More file actions
162 lines (127 loc) · 4.45 KB
/
poc.java
File metadata and controls
162 lines (127 loc) · 4.45 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
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
public class poc {
static String username = "kansas";
static String password = "kansas";
public static SecretKey calculateUserKey(String user, String pass)
throws NoSuchAlgorithmException {
String keyPhrase = username + password;
String keyHash = Utilities.sha1(keyPhrase);
System.out.println(keyHash);
byte[] keyHashBytes = keyHash.getBytes();
byte key[] = Arrays.copyOf(keyHashBytes, 16);
SecretKeySpec secretKey = new SecretKeySpec(key, "AES");
return secretKey;
}
public static void encrypt(String fname) throws Exception {
// KeyGenerator keyGen = KeyGenerator.getInstance("AES");
// keyGen.init(256); // AES-256
// SecretKey key = keyGen.generateKey();
SecretKey key = calculateUserKey(username, password);
Cipher aesCipher = Cipher.getInstance("AES");
aesCipher.init(Cipher.ENCRYPT_MODE, key);
// creating file output stream to write to file
try (FileOutputStream fos = new FileOutputStream(fname + ".enc")) {
// creating output stream to write to file
ObjectOutputStream oos = new ObjectOutputStream(fos);
// oos.writeObject(key);
// creating file input stream to read contents
try (FileInputStream fis = new FileInputStream(fname)) {
// creating cipher output stream to write encrypted contents
try (CipherOutputStream cos = new CipherOutputStream(fos,
aesCipher)) {
int read;
byte buf[] = new byte[4096];
while ((read = fis.read(buf)) != -1)
// Writing to file as cipher
cos.write(buf, 0, read);
}
}
}
}
public static void splitFile(String fileName, int parts) throws IOException {
try {
File f = new File(fileName);
BufferedInputStream bis = new BufferedInputStream(
new FileInputStream(f));
FileOutputStream out;
String name = f.getName();
int partCounter = 1;
int sizeOfFiles = (int) (f.length() / parts) + 1;
byte[] buffer = new byte[sizeOfFiles];
int tmp = 0;
while ((tmp = bis.read(buffer)) > 0) {
String path = f.getParent() + "//" + name + "."
+ String.format("%03d", partCounter++);
File newFile = new File(path);
newFile.createNewFile();
out = new FileOutputStream(newFile);
out.write(buffer, 0, tmp);
out.close();
}
} catch (Exception exp) {
exp.printStackTrace();
}
}
public static void join(String baseFilename, int numberParts,
String newFileName) throws IOException {
String path = "/home/mandeep/";
BufferedOutputStream out = new BufferedOutputStream(
new FileOutputStream(path + newFileName));
for (int part = 0; part < numberParts; part++) {
String partFile = String.format(path + baseFilename + "." + "%03d",
part + 1);
BufferedInputStream in = new BufferedInputStream(
new FileInputStream(partFile));
int b;
while ((b = in.read()) != -1)
out.write(b);
in.close();
}
out.close();
}
public static void decrypt(String fname) throws Exception {
SecretKey key = calculateUserKey(username, password);
// creating file input stream to read from file
try (FileInputStream fis = new FileInputStream(fname)) {
// creating object input stream to read objects from file
ObjectInputStream ois = new ObjectInputStream(fis);
// key = (SecretKey) ois.readObject(); // reading key used for
// encryption
//key = calculateUserKey(username, password);
Cipher aesCipher = Cipher.getInstance("AES");
aesCipher.init(Cipher.DECRYPT_MODE, key);
try (FileOutputStream fos = new FileOutputStream(
"/home/mandeep/pap" + ".pdf")) {
// creating cipher input stream to read encrypted contents
try (CipherInputStream cis = new CipherInputStream(fis,
aesCipher)) {
int read;
byte buf[] = new byte[4096];
while ((read = cis.read(buf)) != -1)
// Writing to file in Decrypt mode
fos.write(buf, 0, read);
}
}
}
}
public static void main(String[] args) throws Exception {
String path = "/home/mandeep/";
String fileName = "me.pdf";
//encrypt(path + "/" + fileName);
decrypt(path + fileName);
}
}