· 5 years ago · Mar 30, 2020, 10:44 PM
1import java.net.*;
2import java.io.*;
3import java.nio.file.*;
4import java.security.InvalidAlgorithmParameterException;
5import java.security.InvalidKeyException;
6import java.security.NoSuchAlgorithmException;
7import java.security.SecureRandom;
8import javax.crypto.*;
9import javax.crypto.spec.IvParameterSpec;
10import javax.crypto.spec.SecretKeySpec;
11
12public class prog{
13 public static void main(String args[]){
14
15 if (args[0].equals("-genKey")){
16 String keyFileName=args[1];
17 createKey(keyFileName);
18 }
19 else if (args[0].equals("-enc" )||args[0].equals("-dec")){
20
21 try {
22 Cipher c = Cipher.getInstance("AES/CTR/NoPadding");
23 SecretKey secretKey =readKey(args[1]);
24
25 if(args[0].equals("-enc")){
26
27
28 //generates a random IV
29 byte[] bytesForIV=new byte[16];
30 SecureRandom sr = new SecureRandom();
31 sr.nextBytes(bytesForIV);
32 IvParameterSpec iv = new IvParameterSpec(bytesForIV);
33
34
35 //runs cipher
36 c.init(Cipher.ENCRYPT_MODE, secretKey, iv);
37 runChiper(c, args[2], args[3],1,bytesForIV);
38
39
40 }else if(args[0].equals("-dec")){
41
42 // reads the first 16 bytes of the file as IV
43 IvParameterSpec iv =getIv(args[2]);
44
45
46 //runs cipher
47 c.init(Cipher.DECRYPT_MODE, secretKey, iv);
48 runChiper(c, args[2], args[3],2,null);
49
50 }
51
52
53 } catch (NoSuchAlgorithmException e) {
54 e.printStackTrace();
55 } catch (NoSuchPaddingException e) {
56 e.printStackTrace();
57 } catch (InvalidAlgorithmParameterException e) {
58 e.printStackTrace();
59 } catch (InvalidKeyException e) {
60 e.printStackTrace();
61 } catch (FileNotFoundException e) {
62 System.out.println("File does not exist");
63 }
64
65 }
66 else{
67 System.out.println("Invalid operation");
68 }
69
70
71 }
72 static private IvParameterSpec getIv(String inFile){
73
74 try {
75
76 //reads the first 16 bytes of the file (IV)
77 FileInputStream inputFile = new FileInputStream(inFile);
78 byte[] inputBuf = new byte[16];
79 inputFile.read(inputBuf);
80 return new IvParameterSpec(inputBuf);
81
82 } catch (IOException e) {
83 e.printStackTrace();
84 }
85 return null;
86 }
87
88 static private void runChiper(Cipher c,String inFile,String outFile, int mode, byte[] bytesForIV) throws FileNotFoundException {
89 try{
90 FileOutputStream outputFile = new FileOutputStream(outFile);
91 FileInputStream inputFile = new FileInputStream(inFile);
92 byte[] inputBuf = new byte[1024];
93 int len;
94
95 // IV handling
96 if(mode==1){
97 //If we are encrypting we write the the IV as the first 16 bytes of the file
98 outputFile.write(bytesForIV);
99
100 }
101 if(mode==2){
102 //If we are decrypting we read the first 16 bytes since they are the IV , but we do nothing with it because the cipher is already initialized
103 inputFile.read(new byte[16]);
104 }
105
106 while ((len = inputFile.read(inputBuf)) != -1) {
107 byte[] outputBuf = c.update(inputBuf, 0, len);
108 if ( outputBuf != null ) {
109 outputFile.write(outputBuf);
110 }
111 }
112
113 byte[] outputBuf = c.doFinal(); //calls for final
114 if ( outputBuf != null ) {
115 outputFile.write(outputBuf);
116 }
117 } catch (IOException e) {
118 e.printStackTrace();
119 } catch (BadPaddingException e) {
120 e.printStackTrace();
121 } catch (IllegalBlockSizeException e) {
122 e.printStackTrace();
123 }
124 }
125
126
127 protected static SecretKeySpec readKey(String keyFileName){
128 // reads a key stored in a file
129 try {
130 byte[] keyBytes = Files.readAllBytes(Paths.get(keyFileName));
131 SecretKeySpec secretKey = new SecretKeySpec(keyBytes, "AES");
132 return secretKey;
133 } catch (IOException e) {
134 e.printStackTrace();
135 }
136 return null;
137 }
138
139 protected static void createKey(String keyFileName){
140 // creates a key and saves it in a file
141 try {
142 KeyGenerator keyGen = KeyGenerator.getInstance("AES");
143 keyGen.init(128);
144 SecretKey secretKey = keyGen.generateKey();
145
146 File keyFile = new File(keyFileName);
147 FileOutputStream out=null;
148 try {
149 out = new FileOutputStream(keyFileName);
150 byte[] keyBytes = secretKey.getEncoded();
151 out.write(keyBytes);
152
153 } finally {
154
155 if (out != null) {
156 out.close();
157 System.out.println("Key created and saved on file: " +keyFileName);
158 }
159 }
160 } catch (NoSuchAlgorithmException e) {
161 System.out.println("Error creating key");
162 } catch (FileNotFoundException e) {
163 System.out.println("Problem creating file");
164 } catch (IOException e) {
165 e.printStackTrace();
166 }
167
168 }
169}