【tool】-AES文件加密

一 AES介绍

密码学中的高级加密标准(Advanced Encryption Standard,AES),又称Rijndael加密法,是美国联邦政府采用的一种区块加密标准;其加密方式是对称加密方式,即将明文通过加密函数(参数带密码)进行加密成密文,经过网络传输后再经过解密函数(参数带密码)解密成明文;
AES介绍博文

二 AES文件加密

  /*
     * @Author lsc
     * @Description <p>对文件进行AES加密 </p>
     * @Date  2019/11/6 9:45
     * @Param [sourceFile, encrypfile, sKey]
     * @Return java.io.File
     */
    public static void encryptFile(File sourceFile, File encrypfile, String stringKey, String algorithm) {

        InputStream inputStream = null;
        OutputStream outputStream = null;
        try {
            // huode获得输入流
            inputStream = new FileInputStream(sourceFile);
            // 获得输出流
            outputStream = new FileOutputStream(encrypfile);
            Cipher cipher = createAESCipher(stringKey, Cipher.ENCRYPT_MODE,algorithm);
            // 加密流写入文件
            CipherInputStream cipherInputStream = new CipherInputStream(
                    inputStream, cipher);
            byte[] bytes = new byte[1024];
            int reladLine = 0;
            while ((reladLine = cipherInputStream.read(bytes)) != -1) {
                outputStream.write(bytes, 0, reladLine);
                outputStream.flush();
            }
            cipherInputStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                inputStream.close();
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();

            }
        }
    }

三 AES文件解密

/*
     * @Author lsc
     * @Description <p> 指定格式解密 </p>
     * @Date  2019/11/6 10:28
     * @Param [sourceFile, decryptFile, sKey, algorithm]
     * @Return java.io.File
     */
    public static void decryptFile(File sourceFile, File decryptFile, String stringKey, String algorithm) {
        InputStream inputStream = null;
        OutputStream outputStream = null;
        CipherOutputStream cipherOutputStream = null;

        try {
            // 获得指定算法的密码器
            Cipher cipher = createAESCipher(stringKey, Cipher.DECRYPT_MODE,algorithm);
            // 获得输入流
            inputStream = new FileInputStream(sourceFile);
            // 获得输出流
            outputStream = new FileOutputStream(decryptFile);
            // 解密输出流
            cipherOutputStream = new CipherOutputStream(
                    outputStream, cipher);
            // 输出文件
            byte[] bytes = new byte[1024];
            int readLine = 0;
            while ((readLine = inputStream.read(bytes)) >=0) {
                cipherOutputStream.write(bytes, 0, readLine);
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                // 关闭流
                cipherOutputStream.close();
                inputStream.close();
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

四 公共代码

 /*
     * @Author lsc
     * @Description <p>获得指定算法的密码器 </p>
     * @Date  2019/11/6 9:55
     * @Param [sKey, cipherMode]
     * @Return javax.crypto.Cipher
     */
    public static Cipher createAESCipher(String sKey, int cipherMode, String algorithm) {
        KeyGenerator keyGenerator = null;
        Cipher cipher = null;
        try {
            // 创建Key生成器
            keyGenerator = KeyGenerator.getInstance(algorithm);
            // 用户密码作为随机数初始化
            keyGenerator.init(128, new SecureRandom(sKey.getBytes()));
            // 根据用户密码生成密钥
            SecretKey secretKey = keyGenerator.generateKey();
            // 返回指定编码格式的密钥
            byte[] codeFormat = secretKey.getEncoded();
            // 转换为指定算法的密钥
            SecretKeySpec key = new SecretKeySpec(codeFormat, algorithm);
            // 获得指定算法的密码器
            cipher = Cipher.getInstance(algorithm);
            // 初始化
            cipher.init(cipherMode, key);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();

        } catch (NoSuchPaddingException e) {
            e.printStackTrace();

        } catch (InvalidKeyException e) {
            e.printStackTrace();
        }
        return cipher;
    }

五 使用方式

    public static void main(String[] args) throws Exception {

        String stringKey = "youku1327";
        //未加密文件路径
        File file = new File("C:\\mydata\\generator\\youku1327.properties");
        //加密后的文件路径
        File encrypfile = new File("C:\\mydata\\generator\\encryp.properties");
        //解密后的文件路径
        File decrypfile = new File("C:\\mydata\\generator\\decryp.properties");
        //加密文件
        encryptFile(file, encrypfile, stringKey,"AES");
        //解密文件
        decryptFile(encrypfile, decrypfile, stringKey,"AES");
    }

六 关于作者

在这里插入图片描述

发布了95 篇原创文章 · 获赞 106 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/youku1327/article/details/103056524