加密解密工具类: package com.mr.util; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.security.*; import javax.crypto.Cipher; import javax.crypto.CipherInputStream; import javax.crypto.CipherOutputStream; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.DESKeySpec; import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder; public abstract class DESCoder { // a weak key private final static BASE64Encoder base64encoder = new BASE64Encoder(); private final static BASE64Decoder base64decoder = new BASE64Decoder(); private static String encoding = "GBK"; public static String sKey = "k34AAE4TAABMAQAA9HcAAIdCAADbRQAA"; /** * 加密字符串 */ public static String ebotongEncrypto(String str) { String result = str; if (str != null && str.length() > 0) { try { byte[] encodeByte = symmetricEncrypto(str.getBytes(encoding)); result = base64encoder.encode(encodeByte); } catch (Exception e) { e.printStackTrace(); } } return result; } /** * 解密字符串 */ public static String ebotongDecrypto(String str) { String result = str; if (str != null && str.length() > 0) { try { byte[] encodeByte = base64decoder.decodeBuffer(str); byte[] decoder = symmetricDecrypto(encodeByte); result = new String(decoder, encoding); } catch (Exception e) { e.printStackTrace(); } } return result; } /** * 加密byte[] */ public static byte[] ebotongEncrypto(byte[] str) { byte[] result = null; if (str != null && str.length > 0) { try { byte[] encodeByte = symmetricEncrypto(str); result = base64encoder.encode(encodeByte).getBytes(); } catch (Exception e) { e.printStackTrace(); } } return result; } /** * 解密byte[] */ public static byte[] ebotongDecrypto(byte[] str) { byte[] result = null; if (str != null && str.length > 0) { try { byte[] encodeByte = base64decoder.decodeBuffer(new String(str, encoding)); // byte[] encodeByte = base64decoder.decodeBuffer(new // String(str)); byte[] decoder = symmetricDecrypto(encodeByte); result = new String(decoder).getBytes(encoding); result = new String(decoder).getBytes(); } catch (Exception e) { e.printStackTrace(); } } return result; } /** * 对称加密字节数组并返回 * * @param byteSource * 需要加密的数据 * @return 经过加密的数据 * @throws Exception */ public static byte[] symmetricEncrypto(byte[] byteSource) throws Exception { try { int mode = Cipher.ENCRYPT_MODE; SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); byte[] keyData = sKey.getBytes(); DESKeySpec keySpec = new DESKeySpec(keyData); Key key = keyFactory.generateSecret(keySpec); Cipher cipher = Cipher.getInstance("DES"); cipher.init(mode, key); byte[] result = cipher.doFinal(byteSource); return result; } catch (Exception e) { throw e; } finally { } } /** * 对称解密字节数组并返回 * * @param byteSource * 需要解密的数据 * @return 经过解密的数据 * @throws Exception */ public static byte[] symmetricDecrypto(byte[] byteSource) throws Exception { try { int mode = Cipher.DECRYPT_MODE; SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); byte[] keyData = sKey.getBytes(); DESKeySpec keySpec = new DESKeySpec(keyData); Key key = keyFactory.generateSecret(keySpec); Cipher cipher = Cipher.getInstance("DES"); cipher.init(mode, key); byte[] result = cipher.doFinal(byteSource); return result; } catch (Exception e) { throw e; } finally { } } /** * 散列算法 * * @param byteSource * 需要散列计算的数据 * @return 经过散列计算的数据 * @throws Exception */ public static byte[] hashMethod(byte[] byteSource) throws Exception { try { MessageDigest currentAlgorithm = MessageDigest.getInstance("SHA-1"); currentAlgorithm.reset(); currentAlgorithm.update(byteSource); return currentAlgorithm.digest(); } catch (Exception e) { throw e; } } /** * 对文件srcFile进行加密输出到文件distFile * * @param srcFile * 明文文件 * @param distFile * 加密后的文件 * @throws Exception */ public static void EncryptFile(String srcFile, String distFile) throws Exception { InputStream is = null; OutputStream out = null; CipherInputStream cis = null; try { int mode = Cipher.ENCRYPT_MODE; SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); byte[] keyData = sKey.getBytes(); DESKeySpec keySpec = new DESKeySpec(keyData); Key key = keyFactory.generateSecret(keySpec); Cipher cipher = Cipher.getInstance("DES"); cipher.init(mode, key); is = new FileInputStream(srcFile); out = new FileOutputStream(distFile); cis = new CipherInputStream(is, cipher); byte[] buffer = new byte[1024]; int r; while ((r = cis.read(buffer)) > 0) { out.write(buffer, 0, r); } } catch (Exception e) { throw e; } finally { cis.close(); is.close(); out.close(); } } /** * 解密文件srcFile到目标文件distFile * * @param srcFile * 密文文件 * @param distFile * 解密后的文件 * @throws Exception */ public static void DecryptFile(String srcFile, String distFile) throws Exception { InputStream is = null; OutputStream out = null; CipherOutputStream cos = null; try { int mode = Cipher.DECRYPT_MODE; SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); byte[] keyData = sKey.getBytes(); DESKeySpec keySpec = new DESKeySpec(keyData); Key key = keyFactory.generateSecret(keySpec); Cipher cipher = Cipher.getInstance("DES"); cipher.init(mode, key); byte[] buffer = new byte[1024]; is = new FileInputStream(srcFile); out = new FileOutputStream(distFile); cos = new CipherOutputStream(out, cipher); int r; while ((r = is.read(buffer)) >= 0) { cos.write(buffer, 0, r); } } catch (Exception e) { throw e; } finally { cos.close(); is.close(); out.close(); } } /** * 对文件进行加密64位编码 * * @param srcFile * 源文件 * @param distFile * 目标文件 */ public static void BASE64EncoderFile(String srcFile, String distFile) { InputStream inputStream = null; OutputStream out = null; try { inputStream = new FileInputStream(srcFile); out = new FileOutputStream(distFile); byte[] buffer = new byte[1024]; while (inputStream.read(buffer) > 0) { out.write(ebotongEncrypto(buffer)); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { out.close(); inputStream.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } /** * 对文件进行解密64位解码 * * @param srcFile * 源文件 * @param distFile * 目标文件 */ public static void BASE64DecoderFile(String srcFile, String distFile) { InputStream inputStream = null; OutputStream out = null; try { inputStream = new FileInputStream(srcFile); out = new FileOutputStream(distFile); byte[] buffer = new byte[1412]; while (inputStream.read(buffer) > 0) { out.write(ebotongDecrypto(buffer)); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { out.close(); inputStream.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } // 本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/Harry_hua/archive/2010/12/02/6050364.aspx 例子:对文件加密(针对文件加密,而非文件内容): public static void encode() { File file = new File(fatherDirecoryName + zipDirecoryName); // 判断文件夹是否存在,如果不存在则创建文件夹 if (!file.exists()) { file.mkdir(); System.out.println("zip 文件创建"); } else { System.out.println("zip 文件存在,"); file.delete(); System.out.print("文件删除,"); file.mkdir(); System.out.print("文件创建"); } try { DESCoder.EncryptFile(fatherDirecoryName + srcDirecoryName + info_file_name_meijiami, fatherDirecoryName + zipDirecoryName + info_file_name_jiami); DESCoder.EncryptFile(fatherDirecoryName + srcDirecoryName + input_file_name_meijiami, fatherDirecoryName + zipDirecoryName + input_file_name_jiami); DESCoder.EncryptFile(fatherDirecoryName + srcDirecoryName + output_file_name_meijiami, fatherDirecoryName + zipDirecoryName + output_file_name_jiami); DESCoder.EncryptFile(fatherDirecoryName + srcDirecoryName + all_table_file_name_meijiami, fatherDirecoryName + zipDirecoryName + all_table_file_name_jiami); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } 例子:对文件解密(针对文件解密,而非文件内容): public static void decode() { File file = new File(fatherDirecoryName + srcDirecoryName); // 判断文件夹是否存在,如果不存在则创建文件夹 if (!file.exists()) { file.mkdir(); System.out.println("src 文件创建"); } else { System.out.println("src 文件存在,"); file.delete(); System.out.print("文件删除,"); file.mkdir(); System.out.print("文件创建"); } try { DESCoder.DecryptFile(fatherDirecoryName + zipDirecoryName + info_file_name_jiami, fatherDirecoryName + srcDirecoryName + info_file_name_meijiami); DESCoder.DecryptFile(fatherDirecoryName + zipDirecoryName + input_file_name_jiami, fatherDirecoryName + srcDirecoryName + input_file_name_meijiami); DESCoder.DecryptFile(fatherDirecoryName + zipDirecoryName + output_file_name_jiami, fatherDirecoryName + srcDirecoryName + output_file_name_meijiami); DESCoder.DecryptFile(fatherDirecoryName + zipDirecoryName + all_table_file_name_jiami, fatherDirecoryName + srcDirecoryName + all_table_file_name_meijiami); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }