import java.io.*;
public class TextFileRW 
{
/***
 * @该类实现对文本文件的加密读写功能
 * @加密算法:设置一个密钥Key,读取文本文件将每一个字符的ASCII码移动密钥对应的数字的距离;
 * @要求能实现将一个文本文件转换为密文保存,并能将密文解密成明文保存
 */
    private BufferedReader src_fin;
    private BufferedWriter des_fout;
    private String srcname;
    private String desname;
    private static final String key="681208";
    public TextFileRW() throws IOException
    {
     this.srcname="sa.txt";
     this.desname="sa.txt";
    }
    public void getSrcName(String srcname)                 //从图形界面获取源文件名
    {
     this.srcname=srcname;
    }
    public void getDesName(String desname)                //从图形界面获取目标文件名
    {
     this.desname=desname;
    }
    public String readFile() throws IOException
    {
     if(!new File(this.srcname).exists())
     new File(this.srcname).createNewFile();
     src_fin=new BufferedReader(new FileReader(this.srcname));
     String str=new String("");
     String aline="";
     while(aline!=null)
     {
     str+=aline+"\r\n";
     aline=src_fin.readLine();
     }
        src_fin.close();
        return str;
    }
    public void writeFile(String str) throws IOException
    {//实现对某个字符串进行加密
     if(!new File(this.desname).exists())
     new File(this.desname).createNewFile();
     des_fout=new BufferedWriter(new FileWriter(this.desname));
     des_fout.write(str);
     des_fout.flush();
     des_fout.close();
    }
    public void writeFile(File filename) throws IOException                  
    {//实现将整个文本文件进行加密
     BufferedReader src_fin=new BufferedReader(new FileReader(filename));
     String aline=src_fin.readLine();
     String str=new String("");
     while(aline!=null)
     {
     str+=aline;
     aline=src_fin.readLine();
     }
     des_fout.write(str);
     des_fout.flush();
     des_fout.close();
    }
    public String encrypt(String str)                         //实现加密算法,返回加密后的字符串
    {
     String results="";
     for(int i=0;i<str.length();i+=key.length())
     {
     if(i+key.length()<str.length())                   //若剩下字符串的长度任然大于密钥长度
     {
     int k=i;
     for(int j=0;j<key.length();j++)
     {  
     results+=(char)(str.charAt(k++)-Integer.parseInt(""+key.charAt(j)));
     }
     }
     else
     {//若剩下字符串的长度小于密钥长度
     int n=i;
     for(int m=0;m<str.length()-i;m++)
     {
                    results+=(char)(str.charAt(n++)-Integer.parseInt(""+key.charAt(m)));
     }
     }
     }
     return results;
    }
    public String decrypt(String str)
    {
     String results="";
     for(int i=0;i<str.length();i+=key.length())
     {
     if(i+key.length()<str.length())                   //若剩下字符串的长度任然大于密钥长度
     {
     int k=i;
     for(int j=0;j<key.length();j++)
     {
    
      results+=(char)(str.charAt(k++)+Integer.parseInt(""+key.charAt(j)));
     }
     }
     else
     {//若剩下字符串的长度小于密钥长度
     int n=i;
     for(int m=0;m<str.length()-i;m++)
     {    
     results+=(char)(str.charAt(n++)+Integer.parseInt(""+key.charAt(m)));
     }
     }
     }
     return results;
    }
}

解决方案 »

  1.   

    加密:
    des_fout.write(str)改成des_fout.write(encrypt(str));
    解密:
    des_fout.write(str)改成des_fout.write(decrypt(str));
      

  2.   


    import java.awt.*;
    import java.awt.event.*;
    import java.io.IOException;import javax.swing.*;
    public class TextFileFrame extends JFrame implements ActionListener
    {
        /**
     * @author sasuke
     * @version 1.0
     * @该类实现文件转换程序的GUI
     */
    private static final long serialVersionUID = 1L;
    private TextFileRW t_file;                  //文件编码解码内核
        private JTextField src_filename;            //源文件名文本域
        private JTextField des_filename;            //目标文件名文本域
        private JButton en_button;                  //解密按钮
        private JButton de_button;                  //加密按钮
        private JMenuBar menubar;                   //菜单栏
        public TextFileFrame() throws IOException
        {
         super("文件转换");
         t_file=new TextFileRW();
         this.setBackground(Color.white);
         this.setLayout(new BorderLayout());
         this.setBounds(200, 200, 200, 200);
         this.setDefaultCloseOperation(EXIT_ON_CLOSE);
         //菜单栏初始化
         String []menu_str={"文件","插入","编辑","格式","工具"};
         menubar=new JMenuBar();
         for(int i=0;i<menu_str.length;i++)
         {
         menubar.add(new JMenu(menu_str[i]));
         }
         this.getContentPane().add(menubar,"North");
        
         //中心文本域和按钮初始化
            JPanel panel=new JPanel();
            panel.setLayout(new FlowLayout());
            JLabel label_src=new JLabel("源文件名");
            JLabel label_des=new JLabel("目标文件名");
            src_filename=new JTextField(15);
            des_filename=new JTextField(15);
            en_button=new JButton("加密");
            de_button=new JButton("解密");
            en_button.addActionListener(this);
            de_button.addActionListener(this);
            panel.add(label_src);
            panel.add(src_filename);
            panel.add(label_des);
            panel.add(des_filename);
            panel.add(en_button);
            panel.add(de_button);
            this.add(panel);
            
            this.setVisible(true);
        }
    public void actionPerformed(ActionEvent e) 
    {
    if(e.getSource()==en_button)
    {
    t_file.getSrcName(src_filename.getText());
    t_file.getDesName(des_filename.getText());
    try {
    t_file.writeFile(t_file.encrypt(t_file.readFile()));
    } catch (IOException e1) 
    {
    e1.printStackTrace();
    }
    }
    if(e.getSource()==de_button)
    {
    t_file.getSrcName(src_filename.getText());
    t_file.getDesName(des_filename.getText());
    try {
    t_file.writeFile(t_file.decrypt(t_file.readFile()));
    } catch (IOException e1) 
    {
    e1.printStackTrace();
    }
    }
    }
    public static void main(String args[]) throws IOException
    {
    new TextFileFrame();
    }
    }
    这是用swing写的主函数,我是先将明文读取加密后存到目标文件,然后取密文解密再存到指定文件~