-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsender.java
More file actions
206 lines (173 loc) · 8.11 KB
/
Copy pathsender.java
File metadata and controls
206 lines (173 loc) · 8.11 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.security.DigestInputStream;
import java.security.MessageDigest;
import java.util.Scanner;
import java.security.*;
import java.io.*;
import java.util.Collections;
import java.util.Arrays;
import javax.crypto.spec.SecretKeySpec;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.Cipher;
import java.nio.file.Files;
import java.nio.file.Path;
import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import java.math.BigInteger;
import java.security.spec.RSAPublicKeySpec;
import java.security.spec.InvalidKeySpecException;
public class sender{
private static int buffer_size = 16 * 1024;
private static byte[] hash;
private static String file;
private static Scanner input = new Scanner(System.in);
private final String initVector = "ASDFGHJKLZXCVBNM";
private static String symmetricKey;
private static PublicKey rKey;
public sender() throws IOException, NoSuchPaddingException,NoSuchProviderException, InvalidAlgorithmParameterException,
InvalidKeyException, IllegalBlockSizeException, BadPaddingException, NoSuchAlgorithmException {
try{
System.out.println("Enter in the file name you want encrypted ");
file = input.nextLine();
hashedMessage(file);
initSymKey();
aesEncryption();
rsaEncryption();
}catch(IOException | InvalidKeySpecException | NoSuchAlgorithmException e){System.out.print(e);}
}
public String initSymKey()throws IOException{
try{
Path keyFile = Path.of("/home/ckugler1/Prj01/KeyGen/Keys/symmetric.key");
symmetricKey = Files.readString(keyFile);
}
catch(IOException e){System.out.print(e);}
return symmetricKey;
}
public void aesEncryption() throws NoSuchPaddingException, NoSuchAlgorithmException, NoSuchProviderException, IOException,
InvalidAlgorithmParameterException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
System.out.println("Starting AES Encryption...");
try{
Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding", "SunJCE");
SecretKeySpec key = new SecretKeySpec(symmetricKey.getBytes("UTF-8"), "AES");
cipher.init(Cipher.ENCRYPT_MODE, key,new IvParameterSpec(initVector.getBytes("UTF-8")));
BufferedInputStream inFile = new BufferedInputStream(new FileInputStream("message.dd"));
BufferedOutputStream outFile = new BufferedOutputStream(new FileOutputStream("message.add-msg"));
byte[] buffer = new byte[buffer_size];
int bytesRead;
while((bytesRead = inFile.read(buffer)) != -1){
byte[] output = cipher.update(buffer, 0, bytesRead);
if(output != null){
outFile.write(output);
}
}
byte[] outputBytes = cipher.doFinal();
if(outputBytes != null){
outFile.write(outputBytes);
}
inFile.close();
outFile.close();
appendMessage();
System.out.println("Finished AES Encryption...");
}catch(NoSuchAlgorithmException | NoSuchProviderException | NoSuchPaddingException | InvalidKeyException |
InvalidAlgorithmParameterException | IOException | IllegalBlockSizeException | BadPaddingException e){System.out.println(e);}
}
public void rsaEncryption() throws IOException, NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeySpecException,
IllegalBlockSizeException, BadPaddingException {
// Grab Receiver's Public key, store it in rKey.
System.out.println("gathering receiver's public key");
BufferedInputStream is = new BufferedInputStream(new FileInputStream("/home/ckugler1/Prj01/KeyGen/Keys/ReceiverPublicKey.key"));
ObjectInputStream ois = new ObjectInputStream(new BufferedInputStream(is));
try {
BigInteger mod = (BigInteger) ois.readObject();
BigInteger exp = (BigInteger) ois.readObject();
RSAPublicKeySpec keySpec = new RSAPublicKeySpec(mod,exp);
KeyFactory fact = KeyFactory.getInstance("RSA");
rKey = fact.generatePublic(keySpec);
//ois.close();
// RSA Encryption of "message.add-msg" with Receiver's public key
System.out.println("RSA encryption beginning");
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("message.add-msg"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("message.rsacipher"));
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.ENCRYPT_MODE, rKey);
byte[] buffer = new byte[117];
int index;
do{
index = bis.read(buffer,0,buffer.length);
System.out.println(index);
bos.write(cipher.doFinal(buffer,0,buffer.length));
System.out.println(Arrays.toString(buffer));
if(index < 117){
byte[] last = new byte[index];
index = bis.read(buffer,0,last.length);
bos.write(cipher.doFinal(buffer,0,last.length));
}
}
while(index > -1);
ois.close();
bis.close();
bos.close();
System.out.println("Finished RSA encryption of Message...");
}catch (IOException | ClassNotFoundException | NoSuchPaddingException
| InvalidKeyException | NoSuchAlgorithmException e){e.printStackTrace();}
}
public void appendMessage() throws IOException{
System.out.println("Appending Message...");
try {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("message.add-msg",true));
byte[] buffer = new byte[buffer_size];
int index;
bos.write('\n');
while((index = bis.read(buffer)) != -1) {
bos.write(buffer, 0, index);
}
bis.close();
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public void hashedMessage(String fileName) throws IOException, NoSuchAlgorithmException {
try {
BufferedInputStream file = new BufferedInputStream(new FileInputStream(fileName));
MessageDigest md = MessageDigest.getInstance("SHA-256");
DigestInputStream in = new DigestInputStream(file, md);
int index;
byte[] buffer = new byte[buffer_size];
do {
index = in.read(buffer, 0, buffer_size);
} while (index == buffer_size);
md = in.getMessageDigest();
in.close();
hash = md.digest();
//System.out.println("DO you want to invert the 1st byte in SHA256(M)? (Y or N)");
//String answer = input.nextLine();
System.out.println("Hexadecimal value of hash value of the message: " + byteToHex(hash));
saveHash(hash);
}catch(IOException e){System.out.print(e);}
}
public void saveHash(byte[] value) throws IOException {
try {
BufferedOutputStream messageDigestFile = new BufferedOutputStream(new FileOutputStream("message.dd"));
messageDigestFile.write(value, 0, value.length);
messageDigestFile.close();
}catch(IOException e){System.out.print(e);}
}
private String byteToHex(byte[] bytes){
StringBuilder string = new StringBuilder();
for (byte value: bytes){
string.append(String.format("%02x", value));
}
return string.toString();
}
public static void main(String[] args) throws IOException, NoSuchProviderException, InvalidAlgorithmParameterException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException
, NoSuchPaddingException, NoSuchAlgorithmException{
new sender();
System.out.println(symmetricKey);
}
}