import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.SecretKeySpec;
public class encrypt {
/**
*
* @param srcfilepath 需要加密/解密的原文件
* @param destfilepath 加密/解密后的目标文件
* @param keystr 8位字符以上的密钥
* @param isencrypt 加密/解密状态位-true为加密,false为解密
* @throws exception 异常
*/
public static void filedes(String srcfilepath,String destfilepath,Cipher cipher)throws Exception{
String algorithm = "DES";
File srcfile = new File(srcfilepath);
File destfile = new File(destfilepath);
byte[]tempbuffer=new byte[2048];
FileInputStream inputfile = new FileInputStream(srcfile);
FileOutputStream outputfile = new FileOutputStream(destfile);
//加密或解密
CipherInputStream cins = new CipherInputStream(inputfile,cipher);
for(int n =0;(n=cins.read(tempbuffer))!=-1;){
outputfile.write(tempbuffer,0,n);
};
inputfile.close();
outputfile.close();
cins.close();
}
/**
* 返回DES解码的密钥
*
* @param serialNumber
* 机器序列号
* @return 密钥
*/
public static Key getKey(String serialNumber) {
Key secretKey = null;
try {
MessageDigest md5 = MessageDigest.getInstance("MD5");
md5.update(serialNumber.getBytes());
DESKeySpec dks = new DESKeySpec(md5.digest());
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
secretKey = keyFactory.generateSecret(dks);
} catch (Exception e) {
System.out.println("获取密码错误......");
}
return secretKey;
}
public static void endir(String path,Cipher cipher) throws Exception{
File file = new File(path);
File[] filelistFiles = file.listFiles();
for(int i = 0; i<filelistFiles.length ; i++){
if(filelistFiles[i].isDirectory()){
endir(filelistFiles[i].getPath(),cipher);
}else{
String enfilrpath;
String filename = filelistFiles[i].getName();
File dirFile = new File("D:\\TEST2\\"+
filelistFiles[i].getParentFile().getName()+"加密");
if(!dirFile.exists())
dirFile.mkdirs();
enfilrpath = dirFile.getPath()+File.separator+filename.substring(0, filename.length()-4)+
".o"+filename.substring(filename.length()-3,filename.length());
filedes(filelistFiles[i].getPath(),enfilrpath,cipher);
}
}
}
/**
* @param args
* @throws NoSuchPaddingException
* @throws NoSuchAlgorithmException
* @throws exception
*/
public static void main(String[] args) {
String path1 = "F:\\TEST1";
String path2 = "F:\\TEST2";
String serialNumber = "fghfghrty";
Boolean isencrypt = true;
System.out.println("加密开始......");
//产生钥匙
Key key = getKey(serialNumber);
//产生钥匙
Cipher cipher = null;
try {
cipher = Cipher.getInstance("DES");
if(isencrypt){
//加密算法
cipher.init(Cipher.ENCRYPT_MODE,key);
}else{
//解密算法
cipher.init(Cipher.DECRYPT_MODE,key);
}
} catch (Exception e) {
System.out.println("加密开始错误......");
}
try {
if(cipher == null)
return;
endir(path1,cipher);
} catch (Exception e) {
System.out.println("加密错误......");
}
System.out.println("加密结束......");
}
}