DES 文件加密解密

DES文件加解密

public class DESFileUtils {
private static final Logger logger = Logger.getLogger(DESFileUtils.class);


//DES加密算法
private static final String ALGORITHM = "DES";

/**
 * 对文件进行加密
 * @param file 待加密的文件
 * @param destFile 加密后的文件
 * @return 加密后的文件路径
 * @throws Exception
 */
public String encrypt(String filePath, String fileName, String key)throws Exception{
	logger.info("进行文件加密处理");
	String newPath;
	if(filePath.endsWith(File.separator)){
		newPath = filePath;
	}else{
		newPath = filePath + File.separator;
	}
	String file = newPath + fileName;
	
	//新建路径,目标目录为:原路径/encypt
	String path = newPath + "encypt" + File.separator;
	File f = new File(path);
	if(!f.exists()){
		f.mkdirs();
	}
	String destFile = path + fileName;
		try{
			
			//获取随机数据
			SecureRandom secureRandom = new SecureRandom();
			
			//从原始密钥数据创建一个DESKeySpec对象
			DESKeySpec desKeySpec = new DESKeySpec(key.getBytes());
			//创建密钥工厂
			SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);
			SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
			
			//获得DES算法实例
			Cipher cipher = Cipher.getInstance(ALGORITHM);
			//进入加密模式
			cipher.init(Cipher.ENCRYPT_MODE, secretKey, secureRandom);
			
			//获得文件
			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();

		}catch(Exception e){
		logger.error("文件加密失败", e);
		}
		//返回加密后的文件路径
		return destFile.toString();
}

/**
 * 对加密文件进行解密
 * @param file 已加密的文件
 * @param dest 解密后的文件(应与源文件相同)
 * @return 解密后的文件路径
 * @throws Exception
 */
public String decrypt(String filePath, String fileName, String key)throws Exception{
	
	logger.info("进行文件解密处理");
	String newPath;
	if(filePath.endsWith(File.separator)){
		newPath = filePath;
	}else{
		newPath = filePath + File.separator;
	}
	
	//待解密文件路径
	String file = newPath + fileName;
	//解密后文件路径
	filePath = newPath + "decypt" + File.separator;
	File f = new File(filePath);
	if(!f.exists()){
		f.mkdirs();
	}
	String destFile = filePath + fileName;
		try{
			
			//获取随机数据
			SecureRandom secureRandom = new SecureRandom();
			//从原始密钥数据创建一个DESKeySpec对象
			DESKeySpec desKeySpec = new DESKeySpec(key.getBytes());
			//创建密钥工厂
			SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);
			SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
			//获得解密实例
			Cipher cipher = Cipher.getInstance(ALGORITHM);
			//进入解密模式
			cipher.init(Cipher.DECRYPT_MODE, secretKey, secureRandom);
			//获取待解密文件
			InputStream is = new FileInputStream(file);
			//写入目标文件
			OutputStream out = new FileOutputStream(destFile);
			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();
		logger.info("文件解密成功");
		}catch(Exception e){
		logger.error("文件解密失败",e);
		}
	return destFile.toString();
	
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值