天天看点

java附件上传Aes加密,下载解密

package com.demo;

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.OutputStream;

import java.io.UnsupportedEncodingException;

import java.security.InvalidKeyException;

import java.security.NoSuchAlgorithmException;

import java.security.SecureRandom;

import javax.crypto.BadPaddingException;

import javax.crypto.Cipher;

import javax.crypto.CipherInputStream;

import javax.crypto.CipherOutputStream;

import javax.crypto.IllegalBlockSizeException;

import javax.crypto.KeyGenerator;

import javax.crypto.NoSuchPaddingException;

import javax.crypto.SecretKey;

import javax.crypto.spec.SecretKeySpec;

public class Aes {

    public static File encryptFile(File sourceFile, File encrypfile, String sKey) {

        InputStream inputStream = null;

        OutputStream outputStream = null;

        try {

            inputStream = new FileInputStream(sourceFile);

            outputStream = new FileOutputStream(encrypfile);

            Cipher cipher = initAESCipher(sKey, Cipher.ENCRYPT_MODE);

            // 以加密流写入文件

            CipherInputStream cipherInputStream = new CipherInputStream(

                    inputStream, cipher);

            byte[] cache = new byte[1024];

            int nRead = 0;

            while ((nRead = cipherInputStream.read(cache)) != -1) {

                outputStream.write(cache, 0, nRead);

                outputStream.flush();

            }

            cipherInputStream.close();

        } catch (FileNotFoundException e) {

            e.printStackTrace(); // To change body of catch statement use File |

                                    // Settings | File Templates.

        } catch (IOException e) {

            e.printStackTrace(); // To change body of catch statement use File |

                                    // Settings | File Templates.

        } finally {

            try {

                inputStream.close();

            } catch (IOException e) {

                e.printStackTrace(); // To change body of catch statement use

                                        // File | Settings | File Templates.

            }

            try {

                outputStream.close();

            } catch (IOException e) {

                e.printStackTrace(); // To change body of catch statement use

                                        // File | Settings | File Templates.

            }

        }

        return encrypfile;

    }

    public static Cipher initAESCipher(String sKey, int cipherMode) {

        // 创建Key gen

        KeyGenerator keyGenerator = null;

        Cipher cipher = null;

        try {

            keyGenerator = KeyGenerator.getInstance("AES");

            keyGenerator.init(128, new SecureRandom(sKey.getBytes()));

            SecretKey secretKey = keyGenerator.generateKey();

            byte[] codeFormat = secretKey.getEncoded();

            SecretKeySpec key = new SecretKeySpec(codeFormat, "AES");

            cipher = Cipher.getInstance("AES");

            // 初始化

            cipher.init(cipherMode, key);

        } catch (NoSuchAlgorithmException e) {

            e.printStackTrace(); // To change body of catch statement use File |

                                    // Settings | File Templates.

        } catch (NoSuchPaddingException e) {

            e.printStackTrace(); // To change body of catch statement use File |

                                    // Settings | File Templates.

        } catch (InvalidKeyException e) {

            e.printStackTrace(); // To change body of catch statement use File |

                                    // Settings | File Templates.

        }

        return cipher;

    }

    public static File decryptFile(File sourceFile, File decryptFile,

            String sKey) {

        InputStream inputStream = null;

        OutputStream outputStream = null;

        try {

            Cipher cipher = initAESCipher(sKey, Cipher.DECRYPT_MODE);

            inputStream = new FileInputStream(sourceFile);

            outputStream = new FileOutputStream(decryptFile);

            CipherOutputStream cipherOutputStream = new CipherOutputStream(

                    outputStream, cipher);

            byte[] buffer = new byte[1024];

            int r;

            while ((r = inputStream.read(buffer)) >= 0) {

                cipherOutputStream.write(buffer, 0, r);

            }

            cipherOutputStream.close();

        } catch (IOException e) {

            e.printStackTrace(); // To change body of catch statement use File |

                                    // Settings | File Templates.

        } finally {

            try {

                inputStream.close();

            } catch (IOException e) {

                e.printStackTrace(); // To change body of catch statement use

                                        // File | Settings | File Templates.

            }

            try {

                outputStream.close();

            } catch (IOException e) {

                e.printStackTrace(); // To change body of catch statement use

                                        // File | Settings | File Templates.

            }

        }

        return decryptFile;

    }

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

        byte[] result = encrypt("你回家啊", "222");

        byte[] result1=decrypt(result, "222");

        System.out.println(new String(result));

        System.out.println(new String(result1));

        String cKey = "00b09e37363e596e1f25b23c78e49939238b";

        //未加密文件路径

        File oldfile = new File("C:\\Users\\6418000146\\Desktop\\文件测试.txt");

        //加密后的文件路径

        File encrypfile = new File("C:\\Users\\6418000146\\Desktop\\文件测试1.txt");

        //解密后的文件路径

        File decrypfile  = new File("C:\\Users\\6418000146\\Desktop\\文件测试2.txt");

        //加密文件

        File a =encryptFile(oldfile,encrypfile,cKey);

        System.out.println(a.getPath()+"---"+a.getName());

        //解密文件

        decryptFile(encrypfile, decrypfile,cKey);

    }

}

继续阅读