Simple zip compressor to instead WinRAR.

I am gonna write blogs via English as current as I do. The first article is regarding to pack and encrypt files to instead WinRAR as a simple solution. java

The principle is just utilizing AES algorithm to pack designated folder into one file, or release the designated file to folder. Here is the code without notes, later I will add up them If possible. Hope you can understand.less

Note: file name maybe too short to safe, because lesser content will cause higher chance to be deciphered.dom

 

  1 package com.encrypt;
  2 
  3 import java.io.ByteArrayOutputStream;
  4 import java.io.File;
  5 import java.io.FileInputStream;
  6 import java.io.FileOutputStream;
  7 import java.io.IOException;
  8 import java.security.MessageDigest;
  9 import java.security.SecureRandom;
 10 import java.security.spec.KeySpec;
 11 import java.util.Arrays;
 12 import java.util.zip.Deflater;
 13 import java.util.zip.Inflater;
 14 
 15 import javax.crypto.Cipher;
 16 import javax.crypto.KeyGenerator;
 17 import javax.crypto.SecretKey;
 18 import javax.crypto.SecretKeyFactory;
 19 import javax.crypto.spec.DESedeKeySpec;
 20 import javax.crypto.spec.IvParameterSpec;
 21 import javax.crypto.spec.SecretKeySpec;
 22 
 23 public class EasyEncrypter{
 24 
 25     public static void main(String[] args) throws IOException {
 26         //args = new String[]{"false", "hh", "ffewf", "fdsfds"};
 27         boolean compress = Boolean.parseBoolean(args[0]);
 28         String fileName = args[1];
 29         byte[] key = args[2].getBytes();
 30         byte[] initialVector = args[3]==null?null:args[3].getBytes();
 31         if (compress) {
 32             new EasyEncrypter().ziper(new File(fileName), key, initialVector);
 33         }else {
 34             new EasyEncrypter().unZiper(new File(fileName+".zip"), key, initialVector);
 35         }
 36     }
 37     
 38     private static final String PRE_TEXT = "1234567890@@@";
 39 
 40     public void unZiper(File file, byte[] key, byte[] initialVector) {
 41         try {
 42             byte[] keys = MessageDigest.getInstance("MD5").digest(key);
 43             keys=Arrays.copyOf(keys, keys.length+key.length);
 44             System.arraycopy(key, 0, keys, keys.length-key.length, key.length);
 45             key = MessageDigest.getInstance("SHA-512").digest(keys);
 46             
 47             FileInputStream fis = new FileInputStream(file);
 48             byte[] bytes = new byte[8*1024],files;
 49             String fileDescription = null;
 50             int len=0,size=0,length=0;
 51             while(true){
 52                 try {
 53                     if (len==bytes.length) {
 54                         bytes=Arrays.copyOf(bytes, bytes.length*2);
 55                     }
 56                     length = fis.read(bytes, len, 8);len+=8;
 57                     size+=length==-1?0:length;
 58                     files=Arrays.copyOf(bytes, size);
 59 
 61                     files = doAESEncrpyt(false, 256, key, initialVector, files);
 62                     files = decompress(files);
 63                     
 64                     fileDescription = new String(files, "UTF-8").trim();
 65                     if (fileDescription.toUpperCase().endsWith(PRE_TEXT)) {
 66                         break;
 67                     }
 68                 }catch(Exception e) {
 69                     e.printStackTrace();
 70                 }finally {
 71                     if (length<1){
 72                         break;
 73                     }
 74                 }
 75             }
 76             
 77             String[] array = fileDescription.split(",");
 78             for(int k=0; k<array.length; k++) {
 79                 if (PRE_TEXT.endsWith(array[k])) {break;}
 80                 File tmpfile = new File(new File("").getAbsoluteFile()+File.separator+array[k]);
 81                 tmpfile.getParentFile().mkdirs();
 82                 tmpfile.createNewFile();
 83                 FileOutputStream fos = new FileOutputStream(tmpfile);
 84                 bytes = new byte[Integer.parseInt(array[k+=1])];
 85                 k++;len=0;
 86                 if (bytes.length==0) {continue;}
 87                 while(true){
 88                     length = fis.read(bytes, 0, bytes.length-len);
 89                     if (length<1){
 90                         break;
 91                     }
 92                     len+=length;
 93                 }
 94                 
 96                 bytes = doAESEncrpyt(false, 256, key, initialVector, bytes);
 97                 fos.write(decompress(bytes));
 98                 fos.close();
 99             }
100             fis.close();
101         }catch(Exception e) {e.printStackTrace();}
102     }
103 
104     public void ziper(File root, byte[] key, byte[] initialVector) {
105         try {
106             byte[] keys = MessageDigest.getInstance("MD5").digest(key);
107             keys=Arrays.copyOf(keys, keys.length+key.length);
108             System.arraycopy(key, 0, keys, keys.length-key.length, key.length);
109             key = MessageDigest.getInstance("SHA-512").digest(keys);
110             
111             File zipData = new File(root.getName()+".zipa");
112             FileOutputStream dataFos = new FileOutputStream(zipData);
113             byte[] buffer = new byte[1024000];
114             String fileDescription = "";
115             
116             for (File subFile : root.listFiles()) {
117                 if (subFile.isDirectory()) {continue;}
118                 if (subFile.length()==0) {
119                     fileDescription+=subFile.getPath()+","+0+","+0+",";
120                     continue;
121                 }
122                 FileInputStream fis = new FileInputStream(subFile);
123                 ByteArrayOutputStream bos = new ByteArrayOutputStream();
124 
125                 int len=0;
126                 while ((len = fis.read(buffer)) != -1) {
127                     bos.write(buffer, 0, len);
128                 }
129                 fis.close();
130                 
131                 byte[] bytes = bos.toByteArray();
132                 bos.close();
133 
134                 bytes=compress(bytes);
135                 len = bytes.length;
136                 bytes = doAESEncrpyt(true, 256, key, initialVector, bytes);
138                 dataFos.write(bytes);
139                 fileDescription+=subFile.getPath()+","+bytes.length+","+len+",";
140             }
141             dataFos.close();
142 
143             FileOutputStream fos = new FileOutputStream(root.getName()+".zip");
144             byte[] bytes = (fileDescription+PRE_TEXT).getBytes("UTF-8");
145             bytes=compress(bytes);
146             bytes = doAESEncrpyt(true, 256, key, initialVector, bytes);
148             fos.write(bytes);
149             
150             FileInputStream fis = new FileInputStream(zipData);
151             bytes = new byte[10240000];
152             while(true){
153                 int length = fis.read(bytes);
154                 if (length<1){
155                     break;
156                 }
157                 fos.write(bytes, 0, length);
158             }
159             fis.close();
160             fos.close();
161             zipData.delete();
162         } catch (Exception e) {
163             e.printStackTrace();
164         }
165     }
166     
167     public static byte[] doAESEncrpyt(boolean encrpt, int length, byte[] key, byte[] initialVector, byte[] text) throws Exception {
168         KeyGenerator kgen = KeyGenerator.getInstance("AES");
169         SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG"); 
170         secureRandom.setSeed(key);
171         kgen.init(length, secureRandom);
172         SecretKey secretKey = kgen.generateKey();
173         byte[] enCodeFormat = secretKey.getEncoded();
174         SecretKeySpec secretKeySpec = new SecretKeySpec(enCodeFormat, "AES");
175         Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
176         if (initialVector==null||initialVector.length==0){
177             cipher.init(encrpt?Cipher.ENCRYPT_MODE:Cipher.DECRYPT_MODE, secretKeySpec);
178         }else{
179             byte[] tmp = new byte[16];
180             if (tmp.length>initialVector.length){
181                 System.arraycopy(initialVector, 0, tmp, 0, initialVector.length);
182             }else{
183                 System.arraycopy(initialVector, 0, tmp, 0, tmp.length);
184             }
185             IvParameterSpec iv = new IvParameterSpec(tmp);
186             cipher.init(encrpt?Cipher.ENCRYPT_MODE:Cipher.DECRYPT_MODE, secretKeySpec, iv);
187         }
188         return cipher.doFinal(text);
189     }
190 
234 
235     public static byte[] compress(byte[] data) {
236         return compress(data, 0, data.length);
237     }
238     
239     public static byte[] decompress(byte[] data) {
240         return decompress(data, 0, data.length);
241     }
242     
243     public static byte[] compress(byte[] data, int offset, int len) {
244         byte[] output = new byte[0];
245 
246         Deflater compresser = new Deflater(Deflater.BEST_COMPRESSION);
247         compresser.setInput(data, offset, len);
248         compresser.finish();
249         ByteArrayOutputStream bos = new ByteArrayOutputStream(len);
250         try {
251             byte[] buf = new byte[1024];
252             while (!compresser.finished()) {
253                 int i = compresser.deflate(buf);
254                 bos.write(buf, 0, i);
255             }
256             output = bos.toByteArray();
257         } catch (Exception e) {
258             output = data;
259             e.printStackTrace();
260         } finally {
261             try {
262                 bos.close();
263             } catch (IOException e) {
264                 e.printStackTrace();
265             }
266         }
267         compresser.end();
268         return output;
269     }
270 
271     public static byte[] decompress(byte[] data, int offset, int len) {
272         byte[] output = new byte[0];
273 
274         Inflater decompresser = new Inflater();
275         decompresser.setInput(data, offset, len);
276 
277         ByteArrayOutputStream baos = new ByteArrayOutputStream(len);
278         try {
279             byte[] buff = new byte[1024];
280             while (!decompresser.finished()) {
281                 int count = decompresser.inflate(buff);
282                 baos.write(buff, 0, count);
283             }
284             output = baos.toByteArray();
285         } catch (Exception e) {
286             output = data;
287             e.printStackTrace();
288         } finally {
289             try {
290                 baos.close();
291             } catch (IOException e) {
292                 e.printStackTrace();
293             }
294         }
295 
296         decompresser.end();
297         return output;
298     }
299 
300 }
相關文章
相關標籤/搜索