java des 文件加密_java实现 DES文件加密

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),源文件,目标文件,密码");

}

}

}

执行方式:

b759ec6e03c03c548f358779db22390b.png

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值