package com.espeed.yxy.tool; import java.io.BufferedReader; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.OutputStream; import java.security.Key; import java.security.SecureRandom; import javax.crypto.Cipher; import javax.crypto.CipherInputStream; import javax.crypto.CipherOutputStream; import javax.crypto.KeyGenerator; import javax.crypto.SecretKey; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.DESKeySpec; import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder; /** * 程序名称: EspeedMail_时速邮箱 * 程序版本: V1.0 * 作 者: 深圳市科飞时速网络技术有限公司(0755-88843776) * 版权所有: 深圳市科飞时速网络技术有限公司 * 技术支持: Tech@21gmail.com * 单元名称: DES加密工具类(营销游) * 开始时间: 2013.12.09 * 程 序 员: 谢勇 * 最后修改: * 备 注: 如需修改请通知程序员 */ public class Encrypt { /** 密钥算法*/ private static final String KEY_ALGORITHM = "DES"; private static final String DEFAULT_CIPHER_ALGORITHM = "DES/ECB/PKCS5Padding"; static byte[] byteArray=null; /** 初始化密钥*/ public static byte[] initSecretKey() throws Exception{ //返回生成指定算法的秘密密钥的 KeyGenerator 对象 KeyGenerator kg = KeyGenerator.getInstance("DES"); //初始化此密钥生成器,使其具有确定的密钥大小 kg.init(56); //生成一个密钥 SecretKey secretKey = kg.generateKey(); byteArray=secretKey.getEncoded(); return secretKey.getEncoded(); } /**转换密钥*/ private static Key toKey() throws Exception{ //实例化DES密钥规则 DESKeySpec dks = new DESKeySpec(byteArray); //实例化密钥工厂 SecretKeyFactory skf = SecretKeyFactory.getInstance("DES"); //生成密钥 ,工厂中对密钥进行处理加工 SecretKey secretKey = skf.generateSecret(dks); return secretKey; } /**获得密钥*/ public static Key getKey() throws Exception { Key kp =toKey(); return kp; } /**文件加密需要传入一个流跟一个文件保存的路径*/ public static Object encrypt(InputStream is, String dest) throws Exception { Cipher cipher = Cipher.getInstance("DES"); //初始化密钥 initSecretKey(); //获得密钥 Object key =getKey(); cipher.init(Cipher.ENCRYPT_MODE,(Key)key); //写入文件 OutputStream out = new FileOutputStream(dest); CipherInputStream cis = new CipherInputStream(is, cipher); byte[] buffer = new byte[1024]; int r; while ((r = cis.read(buffer)) > 0) { out.write(buffer, 0, r); } //数据库保存需要一个字符 BASE64Encoder enc=new BASE64Encoder(); String string=enc.encode(byteArray); cis.close(); is.close(); out.close(); return string; } /**文件解密返回一个流*/ public static InputStream decrypt(String file,Key key) throws IOException { CipherInputStream cis = null; InputStream is =null; try { Cipher cipher = Cipher.getInstance("DES"); cipher.init(Cipher.DECRYPT_MODE,key); is = new FileInputStream(file); cis = new CipherInputStream(is, cipher); } catch (Exception e) { e.printStackTrace(); return null; } return cis; } /**通过传过来的流生成文件*/ public static void decrypt(String outfile,String fileName,CipherInputStream in) throws Exception { File file=new File(outfile); if(!file.exists()){ file.mkdirs(); } OutputStream out = new FileOutputStream(outfile+"\\"+fileName); byte[] buffer = new byte[1024]; int r; while ((r = in.read(buffer)) >= 0) { out.write(buffer, 0, r); } out.close(); in.close(); } /**文件解密返回一个字符串*/ public static String decryptAndGetString(String file,Key key) throws Exception { Cipher cipher = Cipher.getInstance("DES"); cipher.init(Cipher.DECRYPT_MODE,key); InputStream is = new FileInputStream(file); CipherInputStream cis = new CipherInputStream(is, cipher); return convertStreamToString(cis); } public static String inputStream2String(InputStream is) throws IOException{ ByteArrayOutputStream baos = new ByteArrayOutputStream(); int i=-1; while((i=is.read())!=-1){ baos.write(i); } return baos.toString(); } public static String convertStreamToString(InputStream is) { BufferedReader reader = new BufferedReader(new InputStreamReader(is)); StringBuilder sb = new StringBuilder(); String line = null; try { while ((line = reader.readLine()) != null) { sb.append(line + "/n"); } } catch (IOException e) { e.printStackTrace(); } finally { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } return sb.toString(); } /**转换密钥*/ public static Key toKey(byte[] key) throws Exception{ //实例化DES密钥规则 DESKeySpec dks = new DESKeySpec(key); //实例化密钥工厂 SecretKeyFactory skf = SecretKeyFactory.getInstance("DES"); //生成密钥 ,工厂中对密钥进行处理加工 SecretKey secretKey = skf.generateSecret(dks); return secretKey; } public static void main(String[] args){ /* Encrypt.saveDesKey(); System.out.println("生成key"); Encrypt.getKey(); System.out.println("获取key"); try { Encrypt.encrypt("d:\\dd.eml", "d:\\e.eml"); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("加密");*/ try { //Encrypt.decrypt("d:\\dddd.eml", "d:\\ccc.eml"); BASE64Decoder enc=new BASE64Decoder(); Key a=Encrypt.toKey(enc.decodeBuffer("MQ5RobbCg8s=")); System.out.println("qqq"+Encrypt.decryptAndGetString("H:\\test.txt",a)); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("解密"); // System.out.println("加密");*/ // try { // Encrypt.decrypt("c:/test.txt", "d:\\ccc.eml"); // } catch (Exception e) { // // TODO Auto-generated catch block // e.printStackTrace(); // } // System.out.println("解密"); } }