当前位置: 移动技术网 > IT编程>开发语言>Java > Java实现文件的加密解密功能示例

Java实现文件的加密解密功能示例

2019年07月19日  | 移动技术网IT编程  | 我要评论

本文实例讲述了java实现文件的加密解密功能分享给大家供大家参考,具体如下:

package com.copy.encrypt;
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;
public class fileencryptanddecrypt {
  /**
   * 文件file进行加密
   * @param fileurl 文件路径
   * @param key 密码
   * @throws exception
   */
  public static void encrypt(string fileurl, string key) throws exception {
    file file = new file(fileurl);
    string path = file.getpath();
    if(!file.exists()){
      return;
    }
    int index = path.lastindexof("\\");
    string destfile = path.substring(0, index)+"\\"+"abc";
    file dest = new file(destfile);
    inputstream in = new fileinputstream(fileurl);
    outputstream out = new fileoutputstream(destfile);
    byte[] buffer = new byte[1024];
    int r;
    byte[] buffer2=new byte[1024];
    while (( r= in.read(buffer)) > 0) {
        for(int i=0;i<r;i++)
        {
          byte b=buffer[i];
          buffer2[i]=b==255?0:++b;
        }
        out.write(buffer2, 0, r);
        out.flush();
    }
    in.close();
    out.close();
    file.delete();
    dest.renameto(new file(fileurl));
    appendmethoda(fileurl, key);
    system.out.println("加密成功");
  }
  /**
   *
   * @param filename
   * @param content 密钥
   */
   public static void appendmethoda(string filename, string content) {
      try {
        // 打开一个随机访问文件流,按读写方式
        randomaccessfile randomfile = new randomaccessfile(filename, "rw");
        // 文件长度,字节数
        long filelength = randomfile.length();
        //将写文件指针移到文件尾。
        randomfile.seek(filelength);
        randomfile.writebytes(content);
        randomfile.close();
      } catch (ioexception e) {
        e.printstacktrace();
      }
   }
   /**
   * 解密
   * @param fileurl 源文件
   * @param tempurl 临时文件
   * @param ketlength 密码长度
   * @return
   * @throws exception
   */
   public static string decrypt(string fileurl, string tempurl, int keylength) throws exception{
      file file = new file(fileurl);
      if (!file.exists()) {
        return null;
      }
      file dest = new file(tempurl);
      if (!dest.getparentfile().exists()) {
        dest.getparentfile().mkdirs();
      }
      inputstream is = new fileinputstream(fileurl);
      outputstream out = new fileoutputstream(tempurl);
      byte[] buffer = new byte[1024];
      byte[] buffer2=new byte[1024];
      byte bmax=(byte)255;
      long size = file.length() - keylength;
      int mod = (int) (size%1024);
      int div = (int) (size>>10);
      int count = mod==0?div:(div+1);
      int k = 1, r;
      while ((k <= count && ( r = is.read(buffer)) > 0)) {
        if(mod != 0 && k==count) {
          r = mod;
        }
        for(int i = 0;i < r;i++)
        {
          byte b=buffer[i];
          buffer2[i]=b==0?bmax:--b;
        }
        out.write(buffer2, 0, r);
        k++;
      }
      out.close();
      is.close();
      return tempurl;
    }
   /**
   * 判断文件是否加密
   * @param filename
   * @return
   */
   public static string readfilelastbyte(string filename, int keylength) {
     file file = new file(filename);
     if(!file.exists())return null;
     stringbuffer str = new stringbuffer();
      try {
        // 打开一个随机访问文件流,按读写方式
        randomaccessfile randomfile = new randomaccessfile(filename, "r");
        // 文件长度,字节数
        long filelength = randomfile.length();
        //将写文件指针移到文件尾。
        for(int i = keylength ; i>=1 ; i--){
          randomfile.seek(filelength-i);
          str.append((char)randomfile.read());
        }
        randomfile.close();
        return str.tostring();
      } catch (ioexception e) {
        e.printstacktrace();
      }
      return null;
     }
}

ps:关于加密解密感兴趣的朋友还可以参考本站在线工具:

文字在线加密解密工具(包含aes、des、rc4等):

md5在线加密工具:

在线散列/哈希算法加密工具:

在线md5/hash/sha-1/sha-2/sha-256/sha-512/sha-3/ripemd-160加密工具:

在线sha1/sha224/sha256/sha384/sha512加密工具:

更多关于java相关内容感兴趣的读者可查看本站专题:《java数学运算技巧总结》、《java数据结构与算法教程》、《java字符与字符串操作技巧总结》、《java日期与时间操作技巧汇总》、《java操作dom节点技巧总结》和《java缓存操作技巧汇总

希望本文所述对大家java程序设计有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网