DES加密解密

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.security.SecureRandom;

import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;





public class DESUtil 
{
	/**
	 * 3DES实现: 主要有CBC,ECB实现,java默认是ECB,本算法采用默认的ECB实现
对于待加密解密的数据的填充方式:NoPadding、PKCS5Padding、SSL3Padding,默认填充方式为,PKCS5Padding,本算法采用默认的PKCS5Padding填充方式
	 */
	
	private final static String DES = "DES";
	//密码,长度要是8的倍数
	private final static String PASSWORD = "zxms10ok";
	
	
    public DESUtil() 
    {
    }
    
    /**
     * 加密
     * @param datasource byte[]
     * @return byte[]
     */
    public static  byte[] encrypt(byte[] datasource) 
    {            
        try
        {
	        SecureRandom random = new SecureRandom();
	        DESKeySpec desKey = new DESKeySpec(PASSWORD.getBytes());
	        //创建一个密匙工厂,然后用它把DESKeySpec转换成
	        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
	        SecretKey securekey = keyFactory.generateSecret(desKey);
	        //Cipher对象实际完成加密操作
	        Cipher cipher = Cipher.getInstance(DES);
	        //用密匙初始化Cipher对象
	        cipher.init(Cipher.ENCRYPT_MODE, securekey, random);
	        //现在,获取数据并加密
	        //正式执行加密操作
	        return cipher.doFinal(datasource);
        }
        catch(Throwable e)
        {
                e.printStackTrace();
        }
        return null;
    }
    
    /**
     * 解密
     * @param src byte[]
     * @return byte[]
     * @throws Exception
     */
    public static byte[] decrypt(byte[] src) throws Exception 
    {
        // DES算法要求有一个可信任的随机数源
        SecureRandom random = new SecureRandom();
        // 创建一个DESKeySpec对象
        DESKeySpec desKey = new DESKeySpec(PASSWORD.getBytes());
        // 创建一个密匙工厂
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
        // 将DESKeySpec对象转换成SecretKey对象
        SecretKey securekey = keyFactory.generateSecret(desKey);
        // Cipher对象实际完成解密操作
        Cipher cipher = Cipher.getInstance(DES);
        // 用密匙初始化Cipher对象
        cipher.init(Cipher.DECRYPT_MODE, securekey, random);
        // 真正开始解密操作
        return cipher.doFinal(src);
    }
    
    /**
     * 加密文件
     * 
     * @param srcfile
     *            要加密的文件
     * @param destfile
     *            加密后存放的文件名
     */
    public static void encryptfile(String srcfile, String destfile) throws Exception {
        InputStream is = new FileInputStream(srcfile);
        OutputStream out = new FileOutputStream(destfile);
 
        SecureRandom random = new SecureRandom();
        DESKeySpec desKey = new DESKeySpec(PASSWORD.getBytes());
        //创建一个密匙工厂,然后用它把DESKeySpec转换成
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
        SecretKey securekey = keyFactory.generateSecret(desKey);
        //Cipher对象实际完成加密操作
        Cipher cipher = Cipher.getInstance(DES);
        //用密匙初始化Cipher对象
        cipher.init(Cipher.ENCRYPT_MODE, securekey, random);
        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();
    }
    
    /***
     * 解密文件
     * @param destfile
     */
    public static String decryptfile(String destfile) throws Exception {
    	StringBuilder strBuilder = new StringBuilder(); 
    	
        try {
        	// DES算法要求有一个可信任的随机数源
            SecureRandom random = new SecureRandom();
            // 创建一个DESKeySpec对象
            DESKeySpec desKey = new DESKeySpec(PASSWORD.getBytes());
            // 创建一个密匙工厂
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
            // 将DESKeySpec对象转换成SecretKey对象
            SecretKey securekey = keyFactory.generateSecret(desKey);
            // Cipher对象实际完成解密操作
            Cipher cipher = Cipher.getInstance(DES);
            // 用密匙初始化Cipher对象
            cipher.init(Cipher.DECRYPT_MODE, securekey, random);
            
            InputStream is = new FileInputStream(destfile);
            
            CipherInputStream cis = new CipherInputStream(is, cipher);
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    cis));
            String line = null;
            while ((line = reader.readLine()) != null) {
            	strBuilder.append(line);
            }
            reader.close();
            cis.close();
            is.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        
        return strBuilder.toString();
    }
    
    /**
     * 加密字符串并保存到文件中
    * @Title: encryptStrToFile  
    * @param @param content  字符串内容
    * @param @param destfile    目标文件  
    * @return void    返回类型  
    * @throws
     */
    public static void encryptStrToFile(String content, String destfile) throws Exception
    {
    	byte[] encdata = DESUtil.encrypt(content.getBytes());
    	
    	FileOutputStream fos = new FileOutputStream(new File(destfile)); 
    	
    	try
        {
            fos.write(encdata);
        }
        finally
        {
            fos.close();
        }
    }
    
 
    }


你可能感兴趣的:(DES加密解密)