zoukankan      html  css  js  c++  java
  • 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字

  • 相关阅读:
    Android Butterknife(黄油刀) 使用方法总结【转】
    Andriod- 一些包
    Andriod- 学习网站
    Android热点 8.0 ,7.1 ,6.0一7.0 以及6.0以下热点创建到连接完全适配
    Android- 动态修改ToolBar的Menu菜单
    C#- Socket实现服务器与多个客户端通信
    html使用pdf.js途中遇到的坑和坑
    小程序内嵌H5页面和小程序内部页面互相传参和内嵌H5页面的调试
    记录一次Centos7宕机事件
    Spring Boot 2.x实战
  • 原文地址:https://www.cnblogs.com/changhai/p/9663309.html
Copyright © 2011-2022 走看看