DES 加密解密 文件工具類


public class DESEncrypt {
    
    /** 加密工具 */
    private Cipher encryptCipher = null;
 
    /** 解密工具 */
    private Cipher decryptCipher = null;
    private static String keyVal = "asdf1234";
    
    public DESEncrypt(){
        try {
            this.initialize_encryptKey(keyVal);
            this.initalize_dencryptkey(keyVal);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    private void initialize_encryptKey(String keyValue) throws Exception{
            Key key = getKey(keyValue.getBytes());
        encryptCipher = Cipher.getInstance("DES");
        encryptCipher.init(Cipher.ENCRYPT_MODE, key);
    }
 
    public void initalize_dencryptkey(String keyValue) throws Exception {
                Key key = getKey(keyValue.getBytes());
        decryptCipher = Cipher.getInstance("DES");
        decryptCipher.init(Cipher.DECRYPT_MODE, key);
    }
 
    /**
     * 從指定字符串生成密鑰,密鑰所需的字節數組長度為8位 不足8位時后面補0,超出8位只取前8位
     * 
     * @param arrBTmp
     *            構成該字符串的字節數組
     * @return 生成的密鑰
     * @throws java.lang.Exception
     */
    private Key getKey(byte[] arrBTmp) throws Exception {
        // 創建一個空的8位字節數組(默認值為0)
        byte[] arrB = new byte[8];
 
        // 將原始字節數組轉換為8位
        for (int i = 0; i < arrBTmp.length && i < arrB.length; i++) {
            arrB[i] = arrBTmp[i];
        }
        // 生成密鑰
        Key key = new javax.crypto.spec.SecretKeySpec(arrB, "DES");
        return key;
    }
 
    /**
     * 加密字節數組
     * 
     * @param arrB
     *            需加密的字節數組
     * @return 加密后的字節數組
     * @throws Exception
     */
    public byte[] encrypt(byte[] arrB) throws Exception {
        return encryptCipher.doFinal(arrB);
    }
 
    /**
     * 解密字節數組
     * 
     * @param arrB
     *            需解密的字節數組
     * @return 解密后的字節數組
     * @throws Exception
     */
    public byte[] decrypt(byte[] arrB) throws Exception {
        return decryptCipher.doFinal(arrB);
    }
 
    /**
     * 文件file進行加密並保存目標文件destFile中
     * 
     * @param file
     *            要加密的文件 如c:/test/srcFile.txt
     * @param destFile
     *            加密后存放的文件名 如c:/加密后文件.txt
     */
    public void encrypt(String sourceFileName, String diminationFileName) throws Exception {
        InputStream is = new FileInputStream(sourceFileName);
        OutputStream out = new FileOutputStream(diminationFileName);
        CipherInputStream cis = new CipherInputStream(is, encryptCipher);
        byte[] buffer = new byte[1024];
        int r;
        while ((r = cis.read(buffer)) > -1) {
            out.write(buffer, 0, r);
        }
        cis.close();
        is.close();
        out.close();
    }
    public void encrypt(File sourceFile, File diminationFile) throws Exception {
        InputStream is = new FileInputStream(sourceFile);
        OutputStream out = new FileOutputStream(diminationFile);
        CipherInputStream cis = new CipherInputStream(is, encryptCipher);
        byte[] buffer = new byte[1024];
        int r;
        while ((r = cis.read(buffer)) > -1) {
            out.write(buffer, 0, r);
        }
        cis.close();
        is.close();
        out.close();
    }
    /**
     * 文件采用DES算法解密文件
     * 
     * @param file
     *            已加密的文件 如c:/加密后文件.txt * @param destFile 解密后存放的文件名 如c:/
     *            test/解密后文件.txt
     */
    public void decrypt(String sourceFileName, String diminationFileName) throws Exception {
        InputStream is = new FileInputStream(sourceFileName);
        OutputStream out = new FileOutputStream(diminationFileName);
        CipherOutputStream cos = new CipherOutputStream(out, decryptCipher);
        byte[] buffer = new byte[1024];
        int r;
        while ((r = is.read(buffer)) >= 0) {
            cos.write(buffer, 0, r);
        }
        cos.close();
        out.close();
        is.close();
    }
    public void decrypt(File sourceFile, File fileout ) throws Exception {
        InputStream is = new FileInputStream(sourceFile);
        OutputStream out = new FileOutputStream(fileout);
        CipherOutputStream cos = new CipherOutputStream(out, decryptCipher);
        byte[] buffer = new byte[1024];
        int r;
        while ((r = is.read(buffer)) >= 0) {
            cos.write(buffer, 0, r);
        }
        cos.close();
        out.close();
        is.close();
    }
    public static void main(String[] args) throws Exception {
        DESEncrypt t = new DESEncrypt();
        //t.initialize_encryptKey(keyVal);
        //t.initalize_dencryptkey(keyVal);
         Long l = System.currentTimeMillis();
         System.out.println("開始"+l);
        t.encrypt("e:/ceshi.sql", "e:/ceshi2.sql");
        System.out.println("結束"+(System.currentTimeMillis()-l));
        t.decrypt("e:/ceshi2.sql", "e:/ceshi3.sql");
        System.out.println("結束2"+(System.currentTimeMillis()-l));
    }
}

湊150字湊150字湊150字湊150字湊150字湊150字湊150字湊150字湊150字湊150字湊150字湊150字湊150字湊150字湊150字湊150字湊150字湊150字湊150字湊150字湊150字湊150字湊150字湊150字湊150字湊150字湊150字湊150字湊150字湊150字湊150字湊150字湊150字湊150字湊150字湊150字


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM