package com.x.sso.core.entity;

import org.bouncycastle.jce.provider.BouncyCastleProvider;

import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.*;
import java.security.*;
import java.util.Random;

public class AESFile {
    @SuppressWarnings("static-access")
    //文件加密的实现方法
    public static void encryptFile2(String fileName,String encryptedFileName,String sKey,String siv){
        try {
            //导入支持AES/CBC/PKCS7Padding的Provider
            Security.addProvider(new BouncyCastleProvider());

            FileInputStream fis = new FileInputStream(fileName);
            FileOutputStream fos = new FileOutputStream(encryptedFileName);

            byte[] keyValue = sKey.getBytes();

            SecretKeySpec encryKey= new SecretKeySpec(keyValue,"AES");//加密秘钥

            //byte[] ivValue=new byte[16];

            byte[] ivValue = siv.getBytes();
            Random random = new Random(System.currentTimeMillis());
            random.nextBytes(ivValue);
            IvParameterSpec iv = new IvParameterSpec(ivValue);//获取系统时间作为IV

            fos.write(ivValue);	//记录IV
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding","BC");
            cipher.init(Cipher.ENCRYPT_MODE, encryKey,iv);

            CipherInputStream cis=new CipherInputStream(fis, cipher);

            byte[] buffer=new byte[1024];
            int n=0;
            while((n=cis.read(buffer))!=-1){
                fos.write(buffer,0,n);
            }
            cis.close();
            fos.close();
        } catch (InvalidKeyException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InvalidAlgorithmParameterException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (NoSuchProviderException e) {
            e.printStackTrace();
        }

    }

    @SuppressWarnings("static-access")
    //文件解密的实现代码 通过文件流
    public static CipherInputStream decryptedFile(InputStream fis,String sKey, String siv){

        try {
            //导入支持AES/CBC/PKCS7Padding的Provider
            Security.addProvider(new BouncyCastleProvider());

            byte[] keyValue = sKey.getBytes();

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

            byte[] ivValue = siv.getBytes();
            //fis.read(ivValue);//获取IV值
            IvParameterSpec iv= new IvParameterSpec(ivValue);
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding","BC");
            cipher.init(Cipher.DECRYPT_MODE, key,iv);

            CipherInputStream cis= new CipherInputStream(fis, cipher);

            return cis;

        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            return null;
        } catch (InvalidKeyException e) {
            e.printStackTrace();
            return null;
        } catch (InvalidAlgorithmParameterException e) {
            e.printStackTrace();
            return null;
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
            return null;
        } catch (NoSuchProviderException e) {
            e.printStackTrace();
            return null;
        }
    }
    @SuppressWarnings("static-access")
    //文件解密的实现代码
    public static void decryptedFile2(String encryptedFileName,String decryptedFileName, String sKey, String siv){

        try {
            //导入支持AES/CBC/PKCS7Padding的Provider
            Security.addProvider(new BouncyCastleProvider());

            FileInputStream fis = new FileInputStream(encryptedFileName);
            FileOutputStream fos = new FileOutputStream(decryptedFileName);
//            BASE64Decoder decoder = new BASE64Decoder();

            //byte[] fileIdentifier=new byte[15];

            //byte[] keyValue=new byte[16];
            //String sKey= "12345678123456781234567812345678";
            byte[] keyValue = sKey.getBytes();
            //fis.read(keyValue);//读记录的文件加密密码的消息摘要
            //fis.read(fileIdentifier);
            //System.out.println("file=="+new String(fileIdentifier,"UTF-8"));
            //if(new String (fileIdentifier).equals("MyFileEncryptor")){
            SecretKeySpec key= new SecretKeySpec(keyValue,"AES");
            //byte[] ivValue= new byte[16];
            //String siv = "1234567812345678";
            byte[] ivValue = siv.getBytes();
            //fis.read(ivValue);//获取IV值
            IvParameterSpec iv= new IvParameterSpec(ivValue);
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding","BC");
            cipher.init(Cipher.DECRYPT_MODE, key,iv);

            CipherInputStream cis= new CipherInputStream(fis, cipher);
            byte[] buffer=new byte[1024];

            int n=0;
            while((n=cis.read(buffer))!=-1){
                fos.write(buffer,0,n);
            }
            cis.close();

            fos.close();
            //JOptionPane.showMessageDialog(null, "解密成功");
            //}else{
            //JOptionPane.showMessageDialog(null, "文件不是我加密的，爱找谁着谁去");
            //}
        } catch (IOException e) {
            e.printStackTrace();

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

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

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

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

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

        }
    }


}