Java implements DES file encryption

package com.yh.file; /**
 * @Description: Description
 * @Author: yh
 * @CreateDate: 2018/4/28 15:28
 * @UpdateUser: yh.z
 * @UpdateDate: 2018/4/28 15:28
 * @UpdateRemark: The modified content
 * @Version: 1.0
 */

import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.KeyGenerator;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.Key;
import java.security.SecureRandom;

public class FileWithDES {
    public static final String postfix = ".crypt";

    /**
     * 根据参数生成KEY
     */
    public static Key getKey(String strKey) {
        try {
            KeyGenerator _generator = KeyGenerator.getInstance("DES");
            //_generator.init(new SecureRandom(strKey.getBytes()));这种方式,在windows可以,可是在linux每次生成的key不一样
            SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG" );
            secureRandom.setSeed(strKey.getBytes());
            _generator.init(secureRandom);
            Key key = _generator.generateKey();
            _generator = null;
            //System.out.println(key);
            return key;
        } catch (Exception e) {
            throw new RuntimeException("Error initializing SqlMap class. Cause: " + e);
        }
    }

    /**
     * @Description: 加密
     * @Author: yh
     * @Date: 2018/4/28 16:26
     * @param: [file, destFile, strKey]
     * @return: void
     * @Version: 1.0
     */
    public static void encrypt(String file, String destFile, String strKey) throws Exception {
        //ZipUtils.compress();
        Cipher cipher = Cipher.getInstance("DES");
        // cipher.init(Cipher.ENCRYPT_MODE, getKey());
        cipher.init(Cipher.ENCRYPT_MODE, getKey(strKey));
        InputStream is = new FileInputStream(file);
        OutputStream out = new FileOutputStream(destFile);
        CipherInputStream cis = new CipherInputStream(is, cipher);
        byte[] buffer = new byte[1024];
        int r;
        while ((r = cis.read(buffer)) > 0) {
            out.write(buffer, 0, r);
        }
        cis.close();
        is.close();
        out.close();
    }

    /**
     * @Description: 解密
     * @Author: yh
     * @Date: 2018/4/28 16:26
     * @param: [file, dest, strKey]
     * @return: void
     * @Version: 1.0
     */
    public static void decrypt(String file, String dest, String strKey) throws Exception {
        Cipher cipher = Cipher.getInstance("DES");
        cipher.init(Cipher.DECRYPT_MODE, getKey(strKey));
        InputStream is = new FileInputStream(file);
        OutputStream out = new FileOutputStream(dest);
        CipherOutputStream cos = new CipherOutputStream(out, cipher);
        byte[] buffer = new byte[1024];
        int r;
        while ((r = is.read(buffer)) >= 0) {
            cos.write(buffer, 0, r);
        }
        cos.close();
        out.close();
        is.close();
    }

    /**
     * @Description: 压缩并加密
     * @Author: yh
     * @Date: 2018/4/28 17:04
     * @param: [file, destFile, strKey]
     * @return: void
     * @Version: 1.0
     */
//    public static void zipAndEncrypt(String file, String strKey) throws Exception {
//        /*压缩**/
//        if (!file.contains(File.separator)) {
//            throw new Exception("源文件必须输入绝对路径");
//        }
//        String currpath = file.substring(0, file.lastIndexOf(File.separator) + 1);
//        String oldFileName = file.replace(currpath, "");
//        String tempFileName = "";
//        String simpleNmae = "";
//        if (oldFileName.contains(".")) {
//            String postfix = oldFileName.substring(oldFileName.lastIndexOf("."));
//            simpleNmae = oldFileName.replace(postfix, "");
//            tempFileName = simpleNmae + ".temp";
//        }
//        ZipUtil.zip(file, currpath, tempFileName);
//        /*加密**/
//        FileWithDES.encrypt(currpath + tempFileName, currpath + simpleNmae + postfix, strKey);
//        /*删除零时文件**/
//        File zipFile = new File(currpath + tempFileName);
//        zipFile.delete();
//    }

    /**
     * @Description: 解密并解压
     * @Author: yh
     * @Date: 2018/4/28 18:05
     * @param: [file, destFile, strKey]
     * @return: void
     * @Version: 1.0
     */
//    public static void deZipAndEncrypt(String srcFile, String strKey) throws Exception {
//        String currpath = srcFile.substring(0, srcFile.lastIndexOf(File.separator) + 1);
//        String tempFile = srcFile.replace(postfix, ".temp");
//        FileWithDES.decrypt(srcFile, tempFile, strKey);
//        ZipUtil.unzip(tempFile, currpath, true);
//        /*删除临时文件**/
//        File zipFile = new File(tempFile);
//        zipFile.delete();
//    }
    public static void main(String[] args) throws Exception {
        //FileWithDES.encrypt("G:\\temp\\db0.sql", "G:\\temp\\db1.sql", "1");
        //FileWithDES.decrypt("G:\\temp\\db1.sql", "G:\\temp\\2.sql", "1");
        if (args.length == 0) {
            System.err.println("参数不能为空!");
            System.out.println("参数依次为:加密(-e)/解密(-d),源文件,目标文件,密码");
            return;
        }
        if (args.length == 4 &&  "-e".equals(args[0])) {
            System.out.println("加密中。。。");
            FileWithDES.encrypt(args[1], args[2], args[3]);
            System.out.println("加密成功");
        } else if (args.length == 4 &&  "-d".equals(args[0])) {
            System.out.println("解密中。。。");
            FileWithDES.decrypt(args[1], args[2], args[3]);
            System.out.println("解密成功");
        } else if (args.length == 3){
            System.out.println("加密中。。。");
            FileWithDES.encrypt(args[0], args[1], args[2]);
            System.out.println("加密成功");
        }else{
            System.err.println("参数不正确");
            System.out.println("参数依次为:加密(-e)/解密(-d),源文件,目标文件,密码");
        }

    }
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325223476&siteId=291194637