当前位置: 移动技术网 > IT编程>移动开发>Android > android中对文件加密解密的实现

android中对文件加密解密的实现

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

排列三专家独家发布neiba,念亲恩电视剧全集,开心鱼塘

现在项目里面有一个需求,本项目里面下载的视频和文档都不允许通过其他的播放器播放,在培训机构里面这样的需求很多。防止有人交一份钱,把所有的课件就拷给了别人。这样的事情培训机构肯定是不愿意的。现在我项目里面也出了这么个需求。下面介绍一下我的实现。

文件加解密的流程及原理

1、加密方法:存储文件时,从输入流中截取文件的字节数组,对字节数组进行加密,至于加密的方式和算法就可以视需求而定了,然后把加密后的字节数组写入到文件中,最后生成加密后的文件;

2、解密方法:同加密方法一样,只不过是对字节数据进行解密,最后生成明文文件;

3、加密算法:android系统本身引入了javax包的cipher类,这个类里提供了各种各样的通用的加密方式,如aes对称加密等;该程序中有个cipherutil工具类,里面有一些简单的使用cipher进行aes加解密的方法;当然最好还是好好学习一下cipher类的使用;

4、注意事项:

  1. 如何判断一个文件是加密后的文件,最简单的方法就是对加密后的文件统一增加一个后缀名,然后在解密之后将这个后缀名去除,还原回原有文件格式;如:密文文件的统一后缀名为“.cipher”,明文文件名为"测试.txt",加密后的密文文件应该为“测试.txt.cipher”;
  2. 加密文件时还有一个重要的注意事项,就是加密后的密文和明文的长度是否相同,如果文件时一次读取出所有字节数组进行加密的话不用担心这个问题,但是当对文件分次读取加密或分段加密的话,就不得不考虑这个问题了,最方便的方法就是保证明文和加密后的密文长度相同;如果长度不同,由于是分段加密的,密文是由一段一段子密文拼接成的,解密时会找不到每段子密文,因为不知道每段子密文的长度是多少;

主要代码

/**自定义实现简单的文件加密解密工具 
 * created by zhangshuo on 2016/6/28. 
 */ 
public class customfilecipherutil { 
 
  /** 
   * 加密后的文件的后缀 
   */ 
  public static final string cipher_text_suffix = ".cipher"; 
 
  /** 
   * 加解密时以32k个字节为单位进行加解密计算 
   */ 
  private static final int cipher_buffer_lenght = 32 * 1024; 
 
  /** 
   * 加密,这里主要是演示加密的原理,没有用什么实际的加密算法 
   * 
   * @param filepath 明文文件绝对路径 
   * @return 
   */ 
  public static boolean encrypt(string filepath, cipherlistener listener) { 
    try { 
      long starttime = system.currenttimemillis(); 
      file f = new file(filepath); 
      randomaccessfile raf = new randomaccessfile(f, "rw"); 
      long totallenght = raf.length(); 
      filechannel channel = raf.getchannel(); 
 
      long multiples = totallenght / cipher_buffer_lenght; 
      long remainder = totallenght % cipher_buffer_lenght; 
 
      mappedbytebuffer buffer = null; 
      byte tmp; 
      byte rawbyte; 
 
      //先对整除部分加密 
      for(int i = 0; i < multiples; i++){ 
        buffer = channel.map( 
            filechannel.mapmode.read_write, i * cipher_buffer_lenght, (i + 1) * cipher_buffer_lenght); 
 
        //此处的加密方法很简单,只是简单的异或计算 
        for (int j = 0; j < cipher_buffer_lenght; ++j) { 
          rawbyte = buffer.get(j); 
          tmp = (byte) (rawbyte ^ j); 
          buffer.put(j, tmp); 
 
          if(null != listener){ 
            listener.onprogress(i * cipher_buffer_lenght + j, totallenght); 
          } 
        } 
        buffer.force(); 
        buffer.clear(); 
      } 
 
      //对余数部分加密 
      buffer = channel.map( 
          filechannel.mapmode.read_write, multiples * cipher_buffer_lenght, multiples * cipher_buffer_lenght + remainder); 
 
      for (int j = 0; j < remainder; ++j) { 
        rawbyte = buffer.get(j); 
        tmp = (byte) (rawbyte ^ j); 
        buffer.put(j, tmp); 
 
        if(null != listener){ 
          listener.onprogress(multiples * cipher_buffer_lenght + j, totallenght); 
        } 
      } 
      buffer.force(); 
      buffer.clear(); 
 
      channel.close(); 
      raf.close(); 
 
      //对加密后的文件重命名,增加.cipher后缀 
//      f.renameto(new file(f.getpath() + cipher_text_suffix)); 
      log.d("加密用时:", (system.currenttimemillis() - starttime) /1000 + "s"); 
      return true; 
    } catch (exception e) { 
      e.printstacktrace(); 
      return false; 
    } 
  } 
 
 
  /** 
   * 解密,这里主要是演示加密的原理,没有用什么实际的加密算法 
   * 
   * @param filepath 密文文件绝对路径,文件需要以.cipher结尾才会认为其实可解密密文 
   * @return 
   */ 
  public static boolean decrypt(string filepath, cipherlistener listener) { 
    try { 
      long starttime = system.currenttimemillis(); 
      file f = new file(filepath); 
//      if(!f.getpath().tolowercase().endswith(cipher_text_suffix)){ 
//        //后缀不同,认为是不可解密的密文 
//        return false; 
//      } 
 
      randomaccessfile raf = new randomaccessfile(f, "rw"); 
      long totallenght = raf.length(); 
      filechannel channel = raf.getchannel(); 
 
      long multiples = totallenght / cipher_buffer_lenght; 
      long remainder = totallenght % cipher_buffer_lenght; 
 
      mappedbytebuffer buffer = null; 
      byte tmp; 
      byte rawbyte; 
 
      //先对整除部分解密 
      for(int i = 0; i < multiples; i++){ 
        buffer = channel.map( 
            filechannel.mapmode.read_write, i * cipher_buffer_lenght, (i + 1) * cipher_buffer_lenght); 
 
        //此处的解密方法很简单,只是简单的异或计算 
        for (int j = 0; j < cipher_buffer_lenght; ++j) { 
          rawbyte = buffer.get(j); 
          tmp = (byte) (rawbyte ^ j); 
          buffer.put(j, tmp); 
 
          if(null != listener){ 
            listener.onprogress(i * cipher_buffer_lenght + j, totallenght); 
          } 
        } 
        buffer.force(); 
        buffer.clear(); 
      } 
 
      //对余数部分解密 
      buffer = channel.map( 
          filechannel.mapmode.read_write, multiples * cipher_buffer_lenght, multiples * cipher_buffer_lenght + remainder); 
 
      for (int j = 0; j < remainder; ++j) { 
        rawbyte = buffer.get(j); 
        tmp = (byte) (rawbyte ^ j); 
        buffer.put(j, tmp); 
 
        if(null != listener){ 
          listener.onprogress(multiples * cipher_buffer_lenght + j, totallenght); 
        } 
      } 
      buffer.force(); 
      buffer.clear(); 
 
      channel.close(); 
      raf.close(); 
 
      //对加密后的文件重命名,增加.cipher后缀 
//      f.renameto(new file(f.getpath().substring(f.getpath().tolowercase().indexof(cipher_text_suffix)))); 
 
      log.d("解密用时:", (system.currenttimemillis() - starttime) / 1000 + "s"); 
      return true; 
    } catch (exception e) { 
      e.printstacktrace(); 
      return false; 
    } 
  } 
 
  /** 
   * 用于加解密进度的监听器 
   */ 
  public interface cipherlistener{ 
    void onprogress(long current, long total); 
  } 
} 

效果如图:

代码就是这么多,都有注释。以后再有这种需求可以直接用。以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网