• 上传与下载文件加密


    对称加密算法使用详解

    1、加密算法类型

    AES DES 3DES (黑色字体的为常用算法)

    2、具体使用

      2.1 DES算法

    package web;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.security.InvalidKeyException;
    import java.security.Key;
    import java.security.NoSuchAlgorithmException;
    import java.security.SecureRandom;
    
    import javax.crypto.Cipher;
    import javax.crypto.CipherInputStream;
    import javax.crypto.KeyGenerator;
    import javax.crypto.NoSuchPaddingException;
    import javax.crypto.SecretKey;
    
    public class DESUtil {
        
        
        /**
         * 1.定义密码字符串
         */
        private static String SECRET = "!QAZ@WSX";
        
        /**
         * 2.定义算法类型
         */
        private static String DES = "DES";
        
        /**
         * 3.根据密码字符串生成加密所需秘钥
         * @throws NoSuchAlgorithmException 
         */
        public static Key getKey(String secretStr) throws NoSuchAlgorithmException {
            KeyGenerator generator = KeyGenerator.getInstance(DES);
            generator.init(new SecureRandom(secretStr.getBytes()));
            SecretKey key = generator.generateKey();
            return key;
        }
        
        /**
         * 4.加密
         * @throws NoSuchAlgorithmException 
         * @throws NoSuchPaddingException 
         * @throws InvalidKeyException 
         * @throws IOException 
         */
        public static void encDES(File srcFile, File destFile) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IOException {
            if(null == srcFile) {
                System.out.println("文件不存在!");
                return;
            }
            
            // 获取cipher对象  用于加密解密
            Key key = getKey(SECRET);
            Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding"); // 注:DES 需要填充方式
            cipher.init(Cipher.ENCRYPT_MODE,key);    // 加密是为ENCRYPT_MODE
    
            InputStream is = new FileInputStream(srcFile); // 待加密流
            CipherInputStream cis = new CipherInputStream(is, cipher); // 加密流
            OutputStream os = new FileOutputStream(destFile);
            int readLength = 0;
            while ( (readLength = cis.read()) != -1) {
                os.write(readLength);
            }
            os.flush();
            os.close();
            is.close();
            cis.close();
        }
        
        /**
         * 5.解密
         */
        public static void decDES(File srcFile, File destFile) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IOException {
            // 获取cipher对象  用于加密解密
            Key key = getKey(SECRET);
            Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding"); // 注:DES方式需要填充
            cipher.init(Cipher.DECRYPT_MODE,key);    // 加密是为DECRYPT_MODE
            
            InputStream is = new FileInputStream(srcFile); // 加密流
            CipherInputStream cis = new CipherInputStream(is, cipher); // 解密流 
            
            // 解密文件
            OutputStream os = new FileOutputStream(destFile);
            byte[] bys = new byte[1024];
            int readLength = 0;
            while ( (readLength=cis.read(bys)) != -1) {
                os.write(bys, 0, readLength);
            }
            os.flush();
            os.close();
            is.close();
            cis.close();
        }
    }

      

      2.2 AES算法

    package web;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.security.GeneralSecurityException;
    import java.security.Key;
    import java.security.NoSuchAlgorithmException;
    import java.security.SecureRandom;
    
    import javax.crypto.Cipher;
    import javax.crypto.KeyGenerator;
    import javax.crypto.SecretKey;
    
    public class AESUtil {
        /**
         * 1.定义密码字符串
         */
        private static String SECRET = "!QAZ@WSX"; //任一字符串
        
        /**
         * 2.定义算法类型
         */
        private static String AES = "AES";
        
        /**
         * 3.获取加密秘钥
         * @throws NoSuchAlgorithmException 
         */
        private static Key getKey(String secretStr) throws NoSuchAlgorithmException {
            KeyGenerator genertor = KeyGenerator.getInstance(AES);
            genertor.init(128,new SecureRandom(secretStr.getBytes())); // 128 256...
            SecretKey key = genertor.generateKey();
            return key;
        }
        
        /**
         * 4.加密解密流文件(主要方法)
         */
        private static void crypt(InputStream in, OutputStream out, Cipher cipher) throws IOException,GeneralSecurityException {
            int blockSize = cipher.getBlockSize() * 1000;
            int outputSize = cipher.getOutputSize(blockSize);
            
            byte[] inBytes = new byte[blockSize];
            byte[] outBytes = new byte[outputSize];
            
            int inLength = 0;
            boolean more = true;
            while (more) {
                inLength = in.read(inBytes);
                if (inLength == blockSize) {
                    int outLength = cipher.update(inBytes, 0, blockSize, outBytes);
                    out.write(outBytes, 0, outLength);
                } else {
                    more = false;
                }
            }
            if (inLength > 0)
                outBytes = cipher.doFinal(inBytes, 0, inLength);
            else
                outBytes = cipher.doFinal();
            out.write(outBytes);
        }
        
        /**
         * 5.加密
         * @throws GeneralSecurityException 
         * @throws IOException 
         */
        public static void encAES(File srcFile, File destFile) throws IOException, GeneralSecurityException {
            // 获取cipher对象  用于加密解密
            Key key = getKey(SECRET);
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); // 注:AES需要填充方式
            cipher.init(Cipher.ENCRYPT_MODE,key);    // 加密是为ENCRYPT_MODE
            
            InputStream is = new FileInputStream(srcFile);
            OutputStream os = new FileOutputStream(destFile);
            crypt(is, os, cipher);
            os.close();
            is.close();
        }
        
        /**
         * 6.解密
         * @throws GeneralSecurityException 
         * @throws IOException 
         */
        public static void decAES(File srcFile, File destFile) throws IOException, GeneralSecurityException {
            // 获取cipher对象  用于加密解密
            Key key = getKey(SECRET);
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); // 注:AES需要填充方式
            cipher.init(Cipher.DECRYPT_MODE,key);    // 加密是为ENCRYPT_MODE
            
            InputStream is = new FileInputStream(srcFile);
            OutputStream os = new FileOutputStream(destFile);
            crypt(is, os, cipher);
            os.close();
            is.close();
        }
  • 相关阅读:
    简要描述客户端发送的http request header都包含哪些内容
    如何利用TestNG监听器优化测试报告
    TestNG 监听器 ITestListener
    p3317 [SDOI2014]重建
    EZOJ #387字符串
    EZOJ #386 最小生成树
    EZOJ #385 排列
    EZOJ #375高速公路
    EZOJ #374学习
    EZOJ #373排序
  • 原文地址:https://www.cnblogs.com/hugang2017/p/7978271.html
Copyright © 2020-2023  润新知