加密单个或文件夹下所有文件

本文介绍了一个文件加密器的设计与实现,支持对小文件和大文件进行加解密操作,并能批量处理文件夹内的文件。该应用提供了图形界面,便于用户选择文件、输入密码并执行加密或解密。

package com.nec.yjg.file.curity;

/*
 * 新问题:
 * ②判断是什么加密方式后,更改Rediao选中项。
 * ③根据文件类型的不同,加密方式不同(txt,mp3等等)。
 *
 */
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.util.List;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.LineBorder;

public class InitFrame extends WindowAdapter implements ActionListener{
 
 JFrame jframe = new JFrame("文件加密器");
 JRadioButton minFileRadio = new JRadioButton("加/解密小文件",true);
 JRadioButton maxFileRadio = new JRadioButton("加/解大 文 件");
 ButtonGroup groupmm = new ButtonGroup();
 
 JRadioButton singleFileRadio = new JRadioButton("加/解密文件夹下所有小文件",true);
 JRadioButton sumFileRadio = new JRadioButton("加/解密文件夹下所有大文件");
// ButtonGroup groupss = new ButtonGroup();
 
 JLabel jLabel = new JLabel("文件路径:");
 JTextField jtext = new JTextField(30);
 JButton jButton = new JButton("浏览..");
 
 JLabel secLabel = new JLabel("密码");
 JPasswordField secText = new JPasswordField(20);
 JLabel sec2Lael = new JLabel("确认密码");
 JPasswordField sec2Text = new JPasswordField(20);
 
 JFileChooser jFileChooser = new JFileChooser();
 JButton addSecButton = new JButton("加密");
 JButton dicSecButton = new JButton("解密");
 
 // 设置边框
 LineBorder lineBorder = new LineBorder(Color.LIGHT_GRAY );
 LineBorder lineBorder1 = new LineBorder(Color.LIGHT_GRAY );
 
 JPanel radioPanel = new JPanel();
 JPanel radioPanel2 = new JPanel();
 JPanel panel3 = new JPanel();
 JPanel buttonPanel  = new JPanel();
 
 JTextArea jta = new JTextArea();
 public static void main(String[] args){
  new InitFrame();
 }
 
 public InitFrame(){
  makeCenter();
  jframe.addWindowListener(this);
  
  // 给Panel设置布局
  initJieMian();
  jframe.setResizable(false);
  
  // 加入单选按钮
  groupmm.add(minFileRadio);
  groupmm.add(maxFileRadio);
  groupmm.add(singleFileRadio);
  groupmm.add(sumFileRadio);
  radioPanel.add(minFileRadio);
  radioPanel.add(maxFileRadio);
  radioPanel.add(singleFileRadio);
  radioPanel.add(sumFileRadio);
  radioPanel.setBorder(lineBorder);
  
  radioPanel2.add(secLabel);
  radioPanel2.add(secText);
  radioPanel2.add(sec2Lael);
  radioPanel2.add(sec2Text);
  radioPanel2.setBorder(lineBorder1);
  
  panel3.add(jLabel);
  panel3.add(jtext);
  panel3.add(jButton);
  jtext.setEditable(false);
  
  buttonPanel.add(addSecButton);
  buttonPanel.add(dicSecButton);
  
  // 加入按钮事件
  jButton.addActionListener(this);
  addSecButton.addActionListener(this);
  dicSecButton.addActionListener(this);
  
  // 设置默认打开位置
  jFileChooser.setCurrentDirectory(new File("C://Documents and Settings//Administrator//桌面"));
  jframe.setLayout(new GridLayout(4,1));
  jframe.add(radioPanel);
  jframe.add(radioPanel2);
  jframe.add(panel3);
  jframe.add(buttonPanel);
  jframe.setSize(800, 200);
  jframe.setVisible(true);
 }

 @Override
 public void windowClosing(WindowEvent arg0) {
  jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  System.exit(0);
 }

 public void actionPerformed(ActionEvent evt){
  boolean checkFlag;
  String passwordFlag;
  selectChooserMode();
  if(("浏览..".equals(evt.getActionCommand()))){
   if(JFileChooser.APPROVE_OPTION == jFileChooser.showOpenDialog(jframe)){
    String strSelectedPreAddr = jFileChooser.getSelectedFile().getPath();
    jtext.setText(strSelectedPreAddr);
   }
  }
  
  /*
   * 判断文件是否已经加密。
   *
   */
  if("加密".equals(evt.getActionCommand())){
   
   //判断是否输入密码
   if(false == judgePasswordNull()){
    return;
   }
   
   //判断密码是否一致
   passwordFlag = judgeUnity();
   if("1".equals(passwordFlag)){    
    return;
   }else if("3".equals(passwordFlag)){
    return;
   }
   
   //判断是否给出文件或文件夹路径
   checkFlag = checkFileSize();
   if(checkFlag == false){
    return;
   }
   
   //加密小文件同时又是单个文件
   if(minFileRadio.isSelected()/* && singleFileRadio.isSelected()*/){
    singleMinFile();
   }
   
   // 加密大文件同时又是单个文件
   else if(maxFileRadio.isSelected()/* && singleFileRadio.isSelected()*/){
    singleMaxFile();
   }
   
   // 加密小文件同时处在某一文件夹下
   else if(singleFileRadio.isSelected()/*minFileRadio.isSelected() && sumFileRadio.isSelected()*/){
    makeSumMinFile();
   }
   
   // 加密大文件同时处在某一个文件夹下
   else if(/*maxFileRadio.isSelected() && */sumFileRadio.isSelected()){
    makeSumFile();
   }
  }
  /*
   * 判断文件是否已经解密
   * 界面的合理配部
   */
  
  
  if("解密".equals(evt.getActionCommand())){
   
   //判断是否输入密码
   if(false == judgePasswordNull()){
    return;
   }
   
   //判断密码是否一致
   passwordFlag = judgeUnity();
   if("1".equals(passwordFlag)){    
    return;
   }else if("3".equals(passwordFlag)){
    return;
   }
   
   //判断是否给出文件或文件夹路径
   checkFlag = checkFileSize();
   if(checkFlag == false){
    return;
   }

   //解密小文件同时又是单个文件
   if(minFileRadio.isSelected()/* && singleFileRadio.isSelected()*/){
    resSingleMinFile();
   }
   
   // 解密大文件同时又是单个文件
   else if(maxFileRadio.isSelected()/* && singleFileRadio.isSelected()*/){
    resSingleMaxFile();
   }
   
   // 解密小文件同时处在某一文件夹下
   else if(singleFileRadio.isSelected()/*minFileRadio.isSelected() && sumFileRadio.isSelected()*/){
    resSumMinFile();
   }
   
   // 解密大文件同时处在某一个文件夹下
   else if(/*maxFileRadio.isSelected() &&*/ sumFileRadio.isSelected()){
    resSumMaxFile();
   }
  }
 }
 
 private void initJieMian(){
  GridLayout grid = new GridLayout(1,1);
  radioPanel.setLayout(grid);
//  radioPanel2.setLayout(grid);
  FlowLayout flow = new FlowLayout();
  panel3.setLayout(flow);
 }
 
 private boolean checkFileSize(){
  if(jtext.getText().trim().length()== 0 || jtext.getText().trim() == null){
   JOptionPane.showMessageDialog(jframe,"<html><font color=/"red/">" + "请选择必要的文件路径" + "</font></html>");
   return false;
  }else{
   return true;
  }
 }

 // 设置选择文件夹还是文件模式
 private void selectChooserMode(){
  if(minFileRadio.isSelected() || maxFileRadio.isSelected()){
   jFileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
  }
  
  if(sumFileRadio.isSelected() || singleFileRadio.isSelected()){
   jFileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
  }
 }
 
 private boolean  judgePasswordNull(){
  String password1 = new String(secText.getPassword());
  String password2 = new String(sec2Text.getPassword());
  if(password1 == null || password1.length() == 0){
   JOptionPane.showMessageDialog(jframe, "<html><font color=/"red/">"
     + "密码为空,请输入大于等于6位的密码" + "</font></html>");
   return false;
  }
  
  if(password2 == null || password2.length() == 0){
   JOptionPane.showMessageDialog(jframe, "<html><font color=/"red/">"
     + "确认密码为空,请输入大于等于6位的密码" + "</font></html>");
   return false;
  }
  return true;
 }
 
 
 private String judgeUnity(){
  String password1 = new String(secText.getPassword());
  String password2 = new String(sec2Text.getPassword());
  if(password1.length()<6 || password2.length() < 6){
   JOptionPane.showMessageDialog(jframe, "<html><font color=/"red/">"
     + "密码太短,应输入大于等于6位的密码" + "</font></html>");
   return "1";
  }
  if(password1.equals(password2)){   
   return "2";
  }else {
   JOptionPane.showMessageDialog(jframe, "<html><font color=/"red/">"
     + "密码输入不一致" + "</font></html>");
   return "3";
  }
 }
 
 //加密单个小文件
 private void singleMinFile(){
  MakeSecret mm = new MakeSecret();
  JudgeFileError jf = new JudgeFileError();
  jf.getProperties(jtext.getText());
  long fileSize = jf.getFileSizes(new File(jtext.getText()));
  
  // 判断文件大于30M
  int num = 1;
  if(fileSize > 30 * 1048576){
   num =JOptionPane.showConfirmDialog(jframe, "<html><font color=/"#CC0000/">" +
     "您所加密的文件大于30M,改为大文件加密?" + "</font></html>", "判断大小", JOptionPane.YES_NO_OPTION);
  }
  if(num == 1){
   try {
    // 判断文件是否已经加密
    String imFlag = mm.isMi(jtext.getText(), new String(secText
      .getPassword()));
    if (imFlag == "0") {
     
     // 加密文件
     mm.makeSec(new File(jtext.getText()), new String(secText
       .getPassword()));
     JOptionPane.showMessageDialog(jframe,
       "<html><font color=/"blue/">" + "加密成功"
         + "</font></html>");

    } else {
     JOptionPane.showMessageDialog(jframe,
       "<html><font color=/"red/">" + "文件已经加密,不能重复加密"
         + "</font></html>");
     return;
    }
   } catch (Exception e) {
    e.printStackTrace();
   }

  }
  
  // 用户选择"是"时,加密形式改为大文件加密
  if(num == 0){
   singleMaxFile();
  }
 }
 
 // 解密单个小文件
 private void resSingleMinFile(){
  MakeSecret mmRes = new MakeSecret(jtext.getText());
  try {
   //判断文件是否已经加密
   String mmFlag = mmRes.isMi(jtext.getText(),new String(secText.getPassword()));
   if(mmFlag == "1"){
    boolean judFlag = mmRes.judPassword(jtext.getText(),new String(secText.getPassword()));
    if(judFlag == true){
     mmRes.resolveSec(new File(jtext.getText()),new String(secText.getPassword()));
     JOptionPane.showMessageDialog(jframe,
       "<html><font color=/"blue/">" + "解密成功"
         + "</font></html>");
    }else{
     JOptionPane.showMessageDialog(jframe,
       "<html><font color=/"red/">" + "密码错误,请重新输入"
         + "</font></html>");
     return;
    }
    
   }else if(mmFlag == "2"){
    JOptionPane.showMessageDialog(jframe,
      "<html><font color=/"red/">" + "此文件为大文件加密,请选择大文件解密",
      "解密错误", JOptionPane.WARNING_MESSAGE);
    return;
   }else{
    JOptionPane.showMessageDialog(jframe,
      "<html><font color=/"red/">" + "文件没有加密,不能解密"
        + "</font></html>");
    return;
   }
  } catch (Exception e) {
   e.printStackTrace();
  }
 }
 
 // 加密单个大文件
 private void singleMaxFile(){
  MakeSecret mm = new MakeSecret();
  
  // 判断文件是否已经加密
  String imFlag = mm.isMi(jtext.getText(), new String(secText
    .getPassword()));
  if (imFlag == "0") {
   // 加密文件
   mm.makeMaxSec(new File(jtext.getText()),new String(secText
     .getPassword()));
   JOptionPane.showMessageDialog(jframe,
     "<html><font color=/"blue/">" + "大文件加密成功"
       + "</font></html>");
   return;

  } else {
   JOptionPane.showMessageDialog(jframe,
     "<html><font color=/"red/">" + "此大文件已经加密,不能重复加密"
       + "</font></html>");
   return;
  }
 }
 
 // 解密单个大文件
 private void resSingleMaxFile(){
  MakeSecret mmRes = new MakeSecret();
  try {
   //判断文件是否已经加密
   String mmFlag = mmRes.isMi(jtext.getText(),new String(secText.getPassword()));
   if(mmFlag == "2"){
    boolean judFlag = mmRes.judPassword(jtext.getText(),new String(secText.getPassword()));
    if(judFlag == true){
     mmRes.resMaxSec(new File(jtext.getText()),new String(secText.getPassword()));
     JOptionPane.showMessageDialog(jframe,
       "<html><font color=/"blue/">" + "大文件解密成功"
         + "</font></html>");
     return;
    }else{
     JOptionPane.showMessageDialog(jframe,
       "<html><font color=/"red/">" + "密码错误,请重新输入"
         + "</font></html>");
     return;
    }
   }else if("1".equals(mmFlag)){
    JOptionPane.showMessageDialog(jframe,
      "<html><font color=/"red/">" + "此文件为小文件加密,请选择小文件解密",
      "解密错误", JOptionPane.ERROR_MESSAGE);
    return;
   }else{
    JOptionPane.showMessageDialog(jframe,
      "<html><font color=/"red/">" + "文件没有加密,不能解密"
        + "</font></html>");
    return;
   }
   
  } catch (Exception e) {
   e.printStackTrace();
  }
 }
 
 // 加密某文件夹下所有小文件
 private void makeSumMinFile(){
  MakeSecret mmRes = new MakeSecret();
  
  //判断文件是否已经加密
  mmRes.queryFileSumNumber(new File(jtext.getText()),new String(secText.getPassword()));
  List<String> filePathTemp = mmRes.getFilePathTemp();
  
  if(filePathTemp.size() == 0){
   JOptionPane.showMessageDialog(jframe,
     "<html><font color=/"blue/">" + "文件加密成功"
       + "</font></html>");
  }else{
   String fileJud = "";
   for(int i = 0; i<filePathTemp.size(); i++){
    fileJud = fileJud + filePathTemp.get(i) + "<br>";
   }
   JOptionPane.showMessageDialog(jframe,
     "<html><font color=/"blue/">" + fileJud  + " 文件已经加密成功,不能再进行加密."
       + "</font></html>");
   return;
  }
 }
 
 // 解密某文件夹下所有小文件
 private void resSumMinFile(){
  MakeSecret mmRes = new MakeSecret();
  
  //判断文件是否已经加密
  mmRes.resSumFile(new File(jtext.getText()),new String(secText.getPassword()));
  List<String> filePathTemp = mmRes.getFilePathTemp();
  List<String> passwordErrorList = mmRes.getPasswordErrorList();
  
  if(filePathTemp.size() == 0 && passwordErrorList.size()== 0){
   JOptionPane.showMessageDialog(jframe,
     "<html><font color=/"blue/">" + "文件解密成功"
       + "</font></html>");
  }else if(filePathTemp.size() != 0){
   String fileJud = "";
   for(int i = 0; i<filePathTemp.size(); i++){
    fileJud = fileJud + filePathTemp.get(i) + "<br>";
   }
   JOptionPane.showMessageDialog(jframe,
     "<html><font color=/"blue/">" + fileJud
       + "</font>" + "<font color=/"red/">" + " 文件未进行加密,不能进行解密." + "</font></html>");
   return;
  }else if(passwordErrorList.size() != 0){
   String fileJud = "";
   for(int i = 0; i<passwordErrorList.size(); i++){
    fileJud = fileJud + passwordErrorList.get(i) + "<br>";
   }
   JOptionPane.showMessageDialog(jframe,
     "<html><font color=/"blue/">" + fileJud
       + "</font>" + "<font color=/"red/">" + " 密码错误,不能进行解密." + "</font></html>");
   return;
  }
 } 
 
 // 加密多个大文件
 private void makeSumFile(){
  MakeSecret mmRes = new MakeSecret();
  
  //判断文件是否已经加密
  mmRes.makeSumFile(new File(jtext.getText()),new String(secText.getPassword()));
  List<String> filePathTemp = mmRes.getFilePathTemp();
  
  if(filePathTemp.size() == 0){
   JOptionPane.showMessageDialog(jframe,
     "<html><font color=/"blue/">" + "文件加密成功"
       + "</font></html>");
  }else{
   String fileJud = "";
   for(int i = 0; i<filePathTemp.size(); i++){
    fileJud = fileJud + filePathTemp.get(i) + "<br>";
   }
   JOptionPane.showMessageDialog(jframe,
     "<html><font color=/"blue/">" + fileJud + "</font><font color=/"red/">"   + " 文件已经加密成功,不能再进行加密."
       + "</font></html>");
   return;
  }
 }
 
 // 解密多个大文件
 private void resSumMaxFile(){
  MakeSecret mmRes = new MakeSecret();
  
  //判断文件是否已经加密
  mmRes.resSumMaxFile(new File(jtext.getText()),new String(secText.getPassword()));
  List<String> filePathTemp = mmRes.getFilePathTemp();
  List<String> passwordErrorList = mmRes.getPasswordErrorList();
  if(filePathTemp.size() == 0 && passwordErrorList.size() == 0){
   JOptionPane.showMessageDialog(jframe,
     "<html><font color=/"blue/">" + "文件解密成功"
       + "</font></html>");
  }else if(filePathTemp.size() != 0){
   String fileJud = "";
   for(int i = 0; i<filePathTemp.size(); i++){
    fileJud = fileJud + filePathTemp.get(i) + "<br>";
   }
   JOptionPane.showMessageDialog(jframe,
     "<html><font color=/"blue/">" + fileJud + "</font><font color=/"red/">"   + " 文件未进行加密,不能进行解密."
       + "</font></html>");
   return;
  }else if(passwordErrorList.size() != 0){
   String fileJud = "";
   for(int i = 0; i<passwordErrorList.size(); i++){
    fileJud = fileJud + passwordErrorList.get(i) + "<br>";
   }
   JOptionPane.showMessageDialog(jframe,
     "<html><font color=/"blue/">" + fileJud + "</font><font color=/"red/">"   + "密码错误,不能进行解密."
       + "</font></html>");
   return;
  }
 }
 
 // 使窗口居中
 private void makeCenter()
 {  
    int x = 670;
    int y = 650;

    Toolkit tk = Toolkit.getDefaultToolkit();
    Dimension d = tk.getScreenSize();
    int screenHeight = d.height;
    int screenWidth = d.width;
    jframe.setSize(x, y);
    jframe.setLocation((screenWidth - x) / 2, (screenHeight - y) / 2);
 }  
}

 

 

 

package com.nec.yjg.file.curity;

 

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.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.util.ArrayList;
import java.util.List;

public class MakeSecret {
 
 private  String filePath;
 public static final String newFilePath = "e://summer.zip";
 public static void main(String[] args) throws Exception{
  MakeSecret mm = new MakeSecret();
//  mm.resolveSec(new File(filePath));
//  mm.modifyFileName(filePath,newFilePath);
//  mm.makeMaxSec(new File(filePath));
//  mm.resolveMaxSec(new File(filePath));
//  mm.modifyFileName(filePath,newFilePath);
//  mm.makeSec(new File(newFilePath),"123456789");
//  mm.resolveSec(new File(newFilePath),"123456789");
  mm.modifyFileName("d://12.txt", "d://11.txt");
 }
 
 public MakeSecret(){
  
 }
 
 
 public MakeSecret(String filePath){
  this.filePath = filePath;
 }
 
 // 返回信息
 List<String> filePathTemp = new ArrayList<String>();
 
 // 加密文件夹下所有小文件
 public void queryFileSumNumber(File file,String password){
  if(file.isFile()){
   String fileFlag = isMi(file.getAbsolutePath(),password);
   if(fileFlag != "1" || fileFlag != "2"){
    try {
     JudgeFileError jfe = new JudgeFileError();
     long fileSize = jfe.getFileSizes(file);
     if(fileSize > 30 * 1048576){
      
      //大文件加密
      makeMaxSec(file,password);
     }else{
      
      // 小文件加密
      makeSec(file,password);
     }
    } catch (Exception e) {
     e.printStackTrace();
    }
   }else{
    filePathTemp.add(file.getAbsolutePath());
   }
  }else{
   File[] files = file.listFiles();
   for(int i = 0; i < files.length; i++){
    queryFileSumNumber(files[i],password);
   }
  }
 }
 
 // 返回错误密码信息
 List<String> passwordErrorList = new ArrayList<String>();
 public List<String> getPasswordErrorList() {
  return passwordErrorList;
 }
 
 // 解密文件夹下所有小文件
 public void resSumFile(File file,String password){
  if(file.isFile()){
   String fileFlag = isMi(file.getAbsolutePath(),password);
   if("1".equals(fileFlag)){
    boolean secErrorFlag = judPassword(file.getAbsolutePath(),password);
    if(secErrorFlag == false){
     passwordErrorList.add(file.getAbsolutePath());
    }else{
     try {
      resolveSec(file,password);
     } catch (Exception e) {
      e.printStackTrace();
     }
    }
   }else if("2".equals(fileFlag)){
    boolean secErrorFlag = judPassword(file.getAbsolutePath(),password);
    if(secErrorFlag == false){
     passwordErrorList.add(file.getAbsolutePath());
    }else{
     resMaxSec(file,password);
    }
   }else{
    filePathTemp.add(file.getAbsolutePath());
   }
  }else{
   File[] files = file.listFiles();
   for(int i = 0; i < files.length; i++){
    resSumFile(files[i],password);
   }
  }
 }
 
 // 加密文件夹下所有大文件
 public void makeSumFile(File file,String password){
  if(file.isFile()){
   String fileFlag = isMi(file.getAbsolutePath(),password);
   if("0".equals(fileFlag)){
    try {
     JudgeFileError jfe = new JudgeFileError();
     long fileSize = jfe.getFileSizes(file);
     if(fileSize > 30 * 1048576){
      
      //大文件加密
      makeMaxSec(file,password);
     }else{
      
      // 小文件加密
      makeSec(file,password);
     }
    } catch (Exception e) {
     e.printStackTrace();
    }
   }else{
    filePathTemp.add(file.getAbsolutePath());
   }
  }else{
   File[] files = file.listFiles();
   for(int i = 0; i < files.length; i++){
    makeSumFile(files[i],password);
   }
  }
 }
 
 // 解密文件夹下所有大文件
 public void resSumMaxFile(File file,String password){
  if(file.isFile()){
   String fileFlag = isMi(file.getAbsolutePath(),password);
   if("2".equals(fileFlag)){
    boolean secErrorFlag = judPassword(file.getAbsolutePath(),password);
    if(secErrorFlag == true){
     try {
      resMaxSec(file,password);
     } catch (Exception e) {
      e.printStackTrace();
     }
    }else{
     passwordErrorList.add(file.getAbsolutePath());
    }
    
   }else if("1".equals(fileFlag)){
    boolean secErrorFlag = judPassword(file.getAbsolutePath(),password);
    if(secErrorFlag == true){
     try {
      resolveSec(file,password);
     } catch (Exception e) {
      e.printStackTrace();
     }
    }else{
     passwordErrorList.add(file.getAbsolutePath());
    }
    
   }else{
    filePathTemp.add(file.getAbsolutePath());
   }
  }else{
   File[] files = file.listFiles();
   for(int i = 0; i < files.length; i++){
    resSumMaxFile(files[i],password);
   }
  }
 }
 
 public List<String> getFilePathTemp() {
  return filePathTemp;
 }

 public void setFilePathTemp(List<String> filePathTemp) {
  this.filePathTemp = filePathTemp;
 }

 // 小文件加密
 public void makeSec(File file,String password) throws Exception{
   RandomAccessFile raf = new RandomAccessFile(file,"r");
   int fileLen = (int)raf.length();
   raf.close();
   InputStream is = new FileInputStream(file);
   OutputStream os = new FileOutputStream(file.getAbsolutePath() + "bak");
   byte pass[] = password2Byte(password);
   int passLen = password.length();
   
   // 文件长度大于密码长度
   if(fileLen >= passLen){
    byte[] b = new byte[1024];
    int isLen = 0;
    while(((isLen = is.read(b))) != -1){
     for(int i = 0,j = 0; i < isLen; i++,j++){
      if(j == pass.length -1){
       j = 0;
      }
      b[i] = (byte) (b[i]^pass[j]);
     }
     os.write(b,0,isLen);
    }
    
    // 文件长度小于密码长度
   }else {
    byte[] b = new byte[fileLen];
    is.read(b);
    for(int i = 0; i < fileLen; i++){
     b[i] = (byte)(b[i]^pass[i]);
    }
    os.write(b);
   }
   is.close();
   os.close();
   byte[] b = new byte[]{12};
   savePssword(file.getAbsolutePath()+"bak",password,b);
   modifyFileName(file.getAbsolutePath(),file.getAbsolutePath()+"bak");
  }
 
 // 小文件解密
 public void resolveSec(File file,String password) throws Exception{
  RandomAccessFile raf = new RandomAccessFile(file,"rw");
  InputStream is = new FileInputStream(file);
  OutputStream os = new FileOutputStream(file.getAbsolutePath() + "bak");
  byte pass[] = password2Byte(password);
  int fileLen = (int)raf.length() - pass.length -1;
  raf.close();
  
  // 文件长度大于密码长度
  if(fileLen >= pass.length){
   byte[] b = new byte[1024];
   int isLen = 0;
   long sumReadLen = 0;
   while((isLen = is.read(b)) != -1){
    
    // 处理密码遗留在源文件末尾问题
    sumReadLen = sumReadLen + isLen;
    if(sumReadLen >= fileLen){
     isLen = (int) (isLen - (sumReadLen - fileLen));
    }
    for(int i = 0,j = 0; i < isLen; i++,j++){
     if(j == pass.length -1){
      j = 0;
     }
     b[i] = (byte) (b[i]^pass[j]);
    }
    os.write(b,0,isLen);
   }

   // 文件长度小于密码长度
  }else {
   byte[] b = new byte[fileLen];
   is.read(b);
   for(int i = 0; i < fileLen; i++){
    b[i] = (byte)(b[i]^pass[i]);
   }
   os.write(b);
  }
  
  is.close();
  os.close();
  modifyFileName(file.getAbsolutePath(),file.getAbsolutePath()+"bak");
 }
 
 
 // 大文件加密
 public void makeMaxSec(File file,String password){
  RandomAccessFile tempFile = null;
  RandomAccessFile raf = null;
  try {
   raf = new RandomAccessFile(file.getAbsoluteFile(),"r");
   tempFile = new RandomAccessFile(file.getAbsoluteFile()+"temp","rw");
   byte[] b = new byte[1024];
   for(int i = 0; i < 1024 * 2; i++){
    try {
     raf.read(b);
     tempFile.write(b);
    } catch (IOException e) {
     e.printStackTrace();
    }
   } 
  }catch(IOException e){
   e.printStackTrace();
  }finally{
   try {
    raf.close();
    tempFile.close();
   } catch (IOException e) {
    e.printStackTrace();
   }
  }
  
  
  RandomAccessFile rafw = null;
  RandomAccessFile tempFile1 = null;
  try {
   rafw = new RandomAccessFile(file.getAbsoluteFile(),"rw");
   tempFile1 = new RandomAccessFile(file.getAbsoluteFile()+"temp","r");
   byte[] b = new byte[1024];
   byte[] pass = password2Byte(password);
   int k = 0;
   for(int i = 0; i< 1024 * 2; i++){
    try {
     tempFile1.read(b);
     for(int j = 0; j < 1024; j++,k++){
      if(k == pass.length -1){
       k = 0;
      }
      b[j] = (byte) (b[j]^pass[k]);
     }
     rafw.write(b);
    } catch (IOException e) {
     e.printStackTrace();
    }
   }
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  }finally{
   try {
    rafw.close();
    tempFile1.close();
   } catch (IOException e) {
    e.printStackTrace();
   }
  }
  
  //删除临时文件
  File fileTemp = new File(file.getAbsoluteFile()+"temp");
  fileTemp.delete();
  
  // 保存密码
  byte b []= new byte[]{15};
  savePssword(file.getAbsolutePath(),password,b);
 }
 
 // 大文件解密
 public void resMaxSec(File file,String password){
  RandomAccessFile tempFile = null;
  RandomAccessFile raf = null;
  try {
   raf = new RandomAccessFile(file.getAbsoluteFile(),"r");
   tempFile = new RandomAccessFile(file.getAbsoluteFile()+"temp","rw");
   byte[] b = new byte[1024];
   for(int i = 0; i < 1024 * 2; i++){
    try {
     raf.read(b);
     tempFile.write(b);
    } catch (IOException e) {
     e.printStackTrace();
    }
   } 
  }catch(IOException e){
   e.printStackTrace();
  }finally{
   try {
    raf.close();
    tempFile.close();
   } catch (IOException e) {
    e.printStackTrace();
   }
  }
  
  
  RandomAccessFile rafw = null;
  RandomAccessFile tempFile1 = null;
  long fileLen = 0;
  try {
   rafw = new RandomAccessFile(file.getAbsoluteFile(),"rw");
   try {
    fileLen = rafw.length();
   } catch (IOException e1) {
    e1.printStackTrace();
   }
   fileLen = fileLen  - password.length() - 1;
   tempFile1 = new RandomAccessFile(file.getAbsoluteFile()+"temp","rw");
   byte[] b = new byte[1024];
   byte[] pass = password2Byte(password);
   int k = 0;
   for(int i = 0; i< 1024 * 2; i++){
    try {
     tempFile1.read(b);
     for(int j = 0; j < 1024; j++,k++){
      if(k == pass.length -1){
       k = 0;
      }
      b[j] = (byte) (b[j]^pass[k]);
     }
     rafw.write(b);
    } catch (IOException e) {
     e.printStackTrace();
    }
   }
   try {
    rafw.setLength(fileLen);
   } catch (IOException e) {
    e.printStackTrace();
   }
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  }finally{
   try {
    rafw.close();
    tempFile1.close();
   } catch (IOException e) {
    e.printStackTrace();
   }
  }
  
  //删除临时文件
  File fileTemp = new File(file.getAbsoluteFile()+"temp");
  fileTemp.delete();
  
 }
 
 // 加密文件改名和原文件的删除
 private void modifyFileName(String oriStr,String newStr){
  File oriFile = new File(oriStr);
  oriFile.delete();
  File newFile = new File(newStr);
  newFile.renameTo(oriFile);
 }
 
 // 判断文件是否存在
 private boolean fileExists(File file){
  if(file.exists()){
   return true;
  }else{
   return false;
  }
 }
 
 // 密码转化为字节数组
 private byte[] password2Byte(String password){
  byte pass[] = password.getBytes();
  return pass;
 }
 
 // 密码加密保存
 private void savePssword(String filePath,String password,byte[] b){
  try {
   RandomAccessFile raf = new RandomAccessFile(filePath,"rw");
   try {
    long len = raf.length();
    raf.seek(len);
    byte[] pass = password2Byte(password);
    
    // 密码加密
    for(int i = 0; i < pass.length; i++){
     pass[i] = (byte) (pass[i]^15);
    }
    raf.write(pass);
//    byte b[] = new byte[]{12};
    raf.write(b[0]);
   } catch (IOException e) {
    e.printStackTrace();
   }finally{
    try {
     raf.close();
    } catch (IOException e) {
     e.printStackTrace();
    }
   }
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  }
 }
 
 // 判断是否已经加密和是否已经解密
 public String isMi(String filePath,String password){
  try {
   RandomAccessFile  raf = new RandomAccessFile(filePath,"rw");
   try {
    long len = raf.length();
    raf.seek(len-1);
    byte br = raf.readByte();
    byte b[] = new byte[]{12,15};
    if(br == b[0]){
     raf.close();
     return "1";
    }else if(br == b[1]){
     raf.close();
     return "2";
    }else{
     raf.close();
     return "0";
    }
   } catch (IOException e) {
    e.printStackTrace();
   }
  } catch (FileNotFoundException e) {
   
   e.printStackTrace();
  }
  return null;
 }
 
 // 判断密码是否正确
 public boolean judPassword(String filePath,String password){
  try {
   RandomAccessFile  raf = new RandomAccessFile(filePath,"r");
   byte[] pass = password2Byte(password);
   try {
    long fileLen = raf.length();
    int passLen = pass.length;
    long oriFileLen = fileLen - passLen -1;
    raf.seek(oriFileLen);
    byte b[] = new byte[passLen];
    raf.read(b);
    byte b1[] = password.getBytes();
    for(int i = 0; i < passLen; i++){
     b[i] = (byte) (b[i]^15);
     if(b[i] == b1[i]){
      
      //什么也不做
     }else{      
      return false;
     }
    }
    return true;
   } catch (IOException e) {
    e.printStackTrace();
   }finally{
    try {
     raf.close();
    } catch (IOException e) {
     e.printStackTrace();
    }
   }
   
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  }
  return false;
 }
}

package com.nec.yjg.file.curity;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.DecimalFormat;

public class JudgeFileError {
 
 public static void main(String[] args){
  new JudgeFileError().getProperties("E://NCICServlet.jsp");
  new JudgeFileError().getFileSizes(new File("E://NCICServlet.jsp"));
 }
 
 // 更改文件属性
 public void getProperties(String filePath){
  File file = new File(filePath);
  if(!file.canWrite()){
   try {
    Runtime.getRuntime().exec("attrib " + "/"" + file.getAbsolutePath() + "/""+ " -R");
   } catch (IOException e) {
    e.printStackTrace();
   }
  }
 }
 
  public long getFileSizes(File f) {// 取得文件大小
  long s = 0;
  if (f.exists()) {
   FileInputStream fis = null;
   try {
    fis = new FileInputStream(f);
   } catch (FileNotFoundException e) {
    e.printStackTrace();
   }
   try {
    s = fis.available();
   } catch (IOException e) {
    e.printStackTrace();
   }finally{
    try {
     fis.close();
    } catch (IOException e) {
     e.printStackTrace();
    }
   }
  } else {
   System.out.println("文件不存在");
  }
  return s;
 }

  private String FormetFileSize(long fileS) {// 转换文件大小
  DecimalFormat df = new DecimalFormat("#.00");
  String fileSizeString = "";
  if (fileS < 1024) {
   fileSizeString = df.format((double) fileS) + "B";
  } else if (fileS < 1048576) {
   fileSizeString = df.format((double) fileS / 1024) + "K";
  } else if (fileS < 1073741824) {
   fileSizeString = df.format((double) fileS / 1048576) + "M";
  } else {
   fileSizeString = df.format((double) fileS / 1073741824) + "G";
  }
  return fileSizeString;
 }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值