日期:2014-05-20  浏览次数:20930 次

怎么对文件进行加密解密,我的速度太低了,要好几秒.各位大侠帮帮忙,来者有分
输入一个密码,对文件进行简单的加密,再保存,可以输入密码再解密
Java code
    public static String readString(String fileName, Boolean bEn)
            throws IOException {
        File file = new File(fileName);

        BufferedReader bf = new BufferedReader(new FileReader(file));

        String content = "";
        StringBuilder sb = new StringBuilder();

        if (bEn) {
            while (content != null) {
                content = bf.readLine();
                String str = "";
                for (int i = 0; i < content.length(); i++) {
                    str += content.charAt(i) + 3;
                }

                content = str;
                if (content == null) {
                    break;
                }
                content = str;

                sb.append(content);// .trim());
            }
        } else {
            while (content != null) {
                content = bf.readLine();
                String str = "";
                for (int i = 0; i < content.length(); i++) {
                    str += content.charAt(i) - 3;
                }
                if (content == null) {
                    break;
                }
                sb.append(content);// .trim());
            }
        }

        bf.close();
        fileOperation.writeBytes(fileName, sb.toString());
        return sb.toString();

    }

    public static int writeBytes(String file, String string) {
        java.io.ObjectOutputStream out;
        try {
            out = new java.io.ObjectOutputStream(new java.io.FileOutputStream(
                    file));
            out.writeObject(string);
            out.close();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return -1;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return -2;
        }

        return 0;
    }

怎么加快速度,只要简单的加密就可以


------解决方案--------------------
凯撒密码。
------解决方案--------------------
1 StringBuilder初始化的时候,设置其初始大小,避免因为内容添加而不停的扩容
StringBuilder sb = new StringBuilder(file.size());

Java code
while ((content = bf.readLine()) != null) {
  for (int i = 0; i < content.length(); i++) {
    char c = content.charAt(i) + 3;
    sb.append(c); // 既然用了StringBuffer/Builder,就绝对绝对不要str += xxx + xxx + xxx; sb.append(str);
  }
}

------解决方案--------------------
貌似你这里,对换行有遗漏
------解决方案--------------------
你都读到内存上操作,要考虑大文件的时候的性能问题
建议还是读一行写一行,写到一个临时文件,最后再把临时文件改名
感觉你的代码要很多累赘的处理,还有,如果要对字符处理,最好转成字符数组来操作,charAt的性能比较低
Java code

    public static String readString(String fileName, Boolean bEn)
            throws IOException {
        File file = new File(fileName);

        BufferedReader bf = new BufferedReader(new FileReader(file));

        String content = "";
        StringBuilder sb = new StringBuilder();

        if (bEn) {
            while (content != null) {
                content = bf.readLine();
                //String str = "";
                //for (int i = 0; i < content.length(); i++) {
                //    str += content.charAt(i) + 3;
                //}
                for (char c : content.toCharArray()) {
                    sb.append(c+3);
                }

                //content = str;
                //if (content == null) {
                //    break;
                //}
                //content = str;

                //sb.append(content);// .trim());
            }
        } else {
            while (content != null) {
                content = bf.readLine();
                //String str = "";
                //for (int i = 0; i < content.length(); i++) {
                //    str += content.charAt(i) - 3;
                //}
                for (char c : content.toCharArray()) {
                    sb.append(c-3);
                }
                //if (content == null) {
                //    break;
                //}
                //sb.append(content);// .trim());
            }
        }

        bf.close();
        fileOperation.writeBytes(fileName, sb.toString());
        return sb.toString();

    }

    public static int writeBytes(String file, String string) {
        java.io.ObjectOutputStream out;
        try {
            out = new java.io.ObjectOutputStream(new java.io.FileOutputStream(
                    file));
            out.writeObject(string);
            out.close();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return -1;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return -2;
        }

        return 0;
    }