java des加解密
程序员文章站
2024-03-14 17:09:43
...
为什么选择des加密
对称算法,数据加密标准,速度较快,适用于加密大量数据的场合,跟目前项目需求较匹配
代码解析
该加密采用三层加解密,其中加解密分为两类:文件整体加密和文件部分加密,大型文件在加密时耗时较多,所以提供了一个部分加解密,应该能满足绝大多数项目需求
废话也不多说了,直接上代码
package com.tydic.util;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Date;
/**
* @author dengyichao
* @Title: Fileencrypter
* @Package: com.tydic.util
* @Description: des加密解密程序
* @date 2018/5/25 14:30
*/
public class FileencrypterPull {
/**
* @Author: dengyichao
* @Date: 2018/5/25 16:05
* @param fileIn
* @param sKey
* @param dateStr
* @param skey2
* @Description: 加密函数 输入: 要加密的文件,密码,对输入的文件加密后,
* 保存到同一文件夹下增加了 "en+原文件名" 为扩展名的文件中。
*/
public void encrypt(File fileIn, String sKey, String dateStr,String skey2) {
try {
byte[] bytK1 = getKeyByStr(sKey);
byte[] bytK2 = getKeyByStr(dateStr);
byte[] bytK3 = getKeyByStr(skey2);
FileInputStream fis = new FileInputStream(fileIn);
byte[] bytIn = new byte[(int) fileIn.length()];
for (int i = 0; i < fileIn.length(); i++) {
bytIn[i] = (byte) fis.read();
}
// 加密
byte[] bytOut = encryptByDES(encryptByDES(encryptByDES(bytIn,
bytK1), bytK2), bytK3);
String fileOut = fileIn.getParent() + File.separator + "en_"+fileIn.getName();
FileOutputStream fos = new FileOutputStream(fileOut);
for (int i = 0; i < bytOut.length; i++) {
fos.write((int) bytOut[i]);
}
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* @Author: dengyichao
* @Date: 2018/5/25 15:51
* @param fileIn
* @param sKey uuid
* @param dateStr 当前加密时间
* @param skey2 uuid
* @Description: 解密函数 输入: 要解密的文件,密码 des
*/
public void decrypt(File fileIn, String sKey, String dateStr,String skey2) {
FileInputStream fis = null;
FileOutputStream fos = null;
try {
byte[] bytK1 = getKeyByStr(sKey);
byte[] bytK2 = getKeyByStr(dateStr);
byte[] bytK3 = getKeyByStr(skey2);
fis = new FileInputStream(fileIn);
byte[] bytIn = new byte[(int) fileIn.length()];
for (int i = 0; i < fileIn.length(); i++) {
bytIn[i] = (byte) fis.read();
}
// 解密
byte[] bytOut = decryptByDES(decryptByDES(decryptByDES(bytIn,
bytK3), bytK2), bytK1);
String fileOut = fileIn.getParent() + File.separator + "de_"+fileIn.getName();
fos = new FileOutputStream(fileOut);
for (int i = 0; i < bytOut.length; i++) {
fos.write((int) bytOut[i]);
}
System.out.println("解密成功");
} catch (Exception e) {
System.out.println("解密错误!");
} finally {
try {
fos.close();
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* @Author: dengyichao
* @Date: 2018/5/25 15:54
* @param bytP 文件
* @param bytKey 密码
* @Description:
*/
private byte[] encryptByDES(byte[] bytP, byte[] bytKey) throws Exception {
DESKeySpec desKS = new DESKeySpec(bytKey);
SecretKeyFactory skf = SecretKeyFactory.getInstance("DES");
SecretKey sk = skf.generateSecret(desKS);
Cipher cip = Cipher.getInstance("DES");
cip.init(Cipher.ENCRYPT_MODE, sk);
return cip.doFinal(bytP);
}
/**
* 用DES方法解密输入的字节 bytKey需为8字节长,是解密的密码
*/
private byte[] decryptByDES(byte[] bytE, byte[] bytKey) throws Exception {
DESKeySpec desKS = new DESKeySpec(bytKey);
SecretKeyFactory skf = SecretKeyFactory.getInstance("DES");
SecretKey sk = skf.generateSecret(desKS);
Cipher cip = Cipher.getInstance("DES");
cip.init(Cipher.DECRYPT_MODE, sk);
return cip.doFinal(bytE);
}
/**
* 输入密码的字符形式,返回字节数组形式。 如输入字符串:AD67EA2F3BE6E5AD
* 返回字节数组:{173,103,234,47,59,230,229,173}
*/
private byte[] getKeyByStr(String str) {
byte[] bRet = new byte[str.length() / 2];
for (int i = 0; i < str.length() / 2; i++) {
Integer itg = new Integer(16 * getChrInt(str.charAt(2 * i))
+ getChrInt(str.charAt(2 * i + 1)));
bRet[i] = itg.byteValue();
}
return bRet;
}
/**
* 计算一个16进制字符的10进制值 输入:0-F
*/
private int getChrInt(char chr) {
int iRet = 0;
if (chr == "0".charAt(0))
iRet = 0;
if (chr == "1".charAt(0))
iRet = 1;
if (chr == "2".charAt(0))
iRet = 2;
if (chr == "3".charAt(0))
iRet = 3;
if (chr == "4".charAt(0))
iRet = 4;
if (chr == "5".charAt(0))
iRet = 5;
if (chr == "6".charAt(0))
iRet = 6;
if (chr == "7".charAt(0))
iRet = 7;
if (chr == "8".charAt(0))
iRet = 8;
if (chr == "9".charAt(0))
iRet = 9;
if (chr == "A".charAt(0))
iRet = 10;
if (chr == "B".charAt(0))
iRet = 11;
if (chr == "C".charAt(0))
iRet = 12;
if (chr == "D".charAt(0))
iRet = 13;
if (chr == "E".charAt(0))
iRet = 14;
if (chr == "F".charAt(0))
iRet = 15;
return iRet;
}
/**
* @Author: dengyichao
* @Date: 2018/5/28 10:00
* @param fileIn
* @param sKey
* @param dateStr
* @param skey2
* @Description: 部分加密
*/
public int encryptPartial(File fileIn, String sKey, String dateStr,String skey2) {
Integer encryptSize = null;
try {
byte[] bytK1 = getKeyByStr(sKey);
byte[] bytK2 = getKeyByStr(dateStr);
byte[] bytK3 = getKeyByStr(skey2);
FileInputStream fis = new FileInputStream(fileIn);
String fileOut = fileIn.getParent() + File.separator + "en_"+fileIn.getName();
FileOutputStream fos = new FileOutputStream(fileOut);
byte[] bytIn = new byte[1024];
int count = 0;
while ((fis.read(bytIn))!=-1){
++count;
// 如果
if(count == 1){
bytIn = encryptByDES(encryptByDES(encryptByDES(bytIn,
bytK1), bytK2), bytK3);
encryptSize = bytIn.length;
}
fos.write(bytIn);
}
fos.close();
fis.close();
} catch (Exception e) {
e.printStackTrace();
}
return encryptSize;
}
/**
* @Author: dengyichao
* @Date: 2018/5/28 10:01
* @param fileIn
* @param sKey
* @param dateStr
* @param skey2
* @param startSize 之前加密时返回的加密长度
* @Description: 解析部分加密的数据
*/
public void decryptPartial(File fileIn, String sKey, String dateStr,String skey2,int startSize) {
try {
byte[] bytK1 = getKeyByStr(sKey);
byte[] bytK2 = getKeyByStr(dateStr);
byte[] bytK3 = getKeyByStr(skey2);
FileInputStream fis = new FileInputStream(fileIn);
String fileOut = fileIn.getParent() + File.separator + "de_"+fileIn.getName();
FileOutputStream fos = new FileOutputStream(fileOut);
byte[] bytIn = new byte[startSize];
int count = 0;
while ((fis.read(bytIn))!=-1){
++count;
// 如果
if(count == 1){
// 解密
bytIn = decryptByDES(decryptByDES(decryptByDES(bytIn,
bytK3), bytK2), bytK1);
fos.write(bytIn);
bytIn = new byte[1024];
continue;
}
fos.write(bytIn);
}
fos.close();
fis.close();
System.out.println("解密成功");
} catch (Exception e) {
System.out.println("解密错误!");
}
}
}
上一篇: JAVA中AES对称加密和解密
下一篇: Java AES对称加密解密