android文件加解密

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

思路:

首先下载文件,这个就不说了,java代码写个下载管理器。

下载完成后存储文件的时候不是直接存储,要加密存储,加密方法是将文件的每个字节与这个字节在流中的下标做异或运算。

在我们项目里面播放的时候要解密,方法也是将文件的每个字节与这个字节在流中的下标做异或运算。两次异或得到的就是没有加密的值。


[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. /** 
  2.  * 加密解密管理类 
  3.  *  
  4.  * 加密算法 : 将文件的数据流的每个字节与该字节的下标异或. 
  5.  * 解密算法 : 已经加密的文件再执行一次对文件的数据流的每个字节与该字节的下标异或 
  6.  *  
  7.  * @author Administrator 
  8.  *  
  9.  */  
  10. public class FileEnDecryptManager {  
  11.   
  12.     private FileEnDecryptManager() {  
  13.     }  
  14.   
  15.     private static FileEnDecryptManager instance = null;  
  16.   
  17.     public static FileEnDecryptManager getInstance() {  
  18.         synchronized (FileEnDecryptManager.class) {  
  19.             if (instance == null)  
  20.                 instance = new FileEnDecryptManager();  
  21.         }  
  22.         return instance;  
  23.     }  
  24.   
  25.     /** 
  26.      * 记录上次解密过的文件名 
  27.      */  
  28.     private final String LastDecryptFile = Framework  
  29.             .getModule(DownloadModule.class).getDownloadDir().getAbsolutePath()  
  30.             + "/LastDecryptFilename.ttt";  
  31.   
  32.     /** 
  33.      * LastDecryptFilename.ttt 文件是否被清空 
  34.      */  
  35.     private boolean isClear = false;  
  36.   
  37.     /** 
  38.      * 加密入口 
  39.      *  
  40.      * @param fileUrl 
  41.      *            文件绝对路径 
  42.      * @return 
  43.      */  
  44.     public boolean InitEncrypt(String fileUrl) {  
  45.         encrypt(fileUrl);  
  46.         return true;  
  47.     }  
  48.   
  49.     private final int REVERSE_LENGTH = 56;  
  50.   
  51.     /** 
  52.      * 加解密 
  53.      *  
  54.      * @param strFile 
  55.      *            源文件绝对路径 
  56.      * @return 
  57.      */  
  58.     private boolean encrypt(String strFile) {  
  59.         int len = REVERSE_LENGTH;  
  60.         try {  
  61.             File f = new File(strFile);  
  62.             RandomAccessFile raf = new RandomAccessFile(f, "rw");  
  63.             long totalLen = raf.length();  
  64.   
  65.             if (totalLen < REVERSE_LENGTH)  
  66.                 len = (int) totalLen;  
  67.   
  68.             FileChannel channel = raf.getChannel();  
  69.             MappedByteBuffer buffer = channel.map(  
  70.                     FileChannel.MapMode.READ_WRITE, 0, REVERSE_LENGTH);  
  71.             byte tmp;  
  72.             for (int i = 0; i < len; ++i) {  
  73.                 byte rawByte = buffer.get(i);  
  74.                 tmp = (byte) (rawByte ^ i);  
  75.                 buffer.put(i, tmp);  
  76.             }  
  77.             buffer.force();  
  78.             buffer.clear();  
  79.             channel.close();  
  80.             raf.close();  
  81.             return true;  
  82.         } catch (Exception e) {  
  83.             e.printStackTrace();  
  84.             return false;  
  85.         }  
  86.     }  
  87.   
  88.     /** 
  89.      * 解密入口 
  90.      *  
  91.      * @param fileUrl 
  92.      *            源文件绝对路径 
  93.      */  
  94.     public void Initdecrypt(String fileUrl) {  
  95.         try {  
  96.             if (isDecripted(fileUrl)) {  
  97.                 decrypt(fileUrl);  
  98.             }  
  99.         } catch (Exception e) {  
  100.             e.printStackTrace();  
  101.         }  
  102.     }  
  103.   
  104.     private void decrypt(String fileUrl) {  
  105.         encrypt(fileUrl);  
  106.     }  
  107.   
  108.     /** 
  109.      * fileName 文件是否已经解密了 
  110.      *  
  111.      * @param fileName 
  112.      * @return 
  113.      * @throws IOException 
  114.      */  
  115.     private boolean isDecripted(String fileName) throws IOException {  
  116.         // 上次加密的文件  
  117.         File lastDecryptFile = new File(LastDecryptFile);  
  118.         if (lastDecryptFile.exists() && isClear == false) {  
  119.             String lastDecryptfilepath = getLastDecryptFilePath(LastDecryptFile);  
  120.             if (lastDecryptfilepath != null  
  121.                     && lastDecryptfilepath.equals(fileName)) {  
  122.                 return false;  
  123.             } else {  
  124.                 clear();  
  125.             }  
  126.         }  
  127.         StringBufferWrite(fileName);  
  128.         return true;  
  129.     }  
  130.   
  131.     /** 
  132.      * 将需要加密的文件绝对路径写入LastDecryptFile 
  133.      *  
  134.      * @param filePath 
  135.      *            需要加密的文件绝对路径 
  136.      * @param content 
  137.      * @throws IOException 
  138.      */  
  139.     private void StringBufferWrite(String filePath) throws IOException {  
  140.         File lastDecryptFile = new File(LastDecryptFile);  
  141.         if (!lastDecryptFile.exists())  
  142.             lastDecryptFile.createNewFile();  
  143.         FileOutputStream out = new FileOutputStream(lastDecryptFile, true);  
  144.         StringBuffer sb = new StringBuffer();  
  145.         sb.append(filePath);  
  146.         out.write(sb.toString().getBytes("utf-8"));  
  147.         out.close();  
  148.     }  
  149.   
  150.     /** 
  151.      * 清空加密记录 
  152.      */  
  153.     public synchronized void clear() {  
  154.         isClear = true;  
  155.         File decryptTempFile = new File(LastDecryptFile);  
  156.         if (decryptTempFile.exists()) {  
  157.             try {  
  158.                 String fileName = getLastDecryptFilePath(LastDecryptFile);  
  159.                 decrypt(fileName);  
  160.                 new File(LastDecryptFile).delete();  
  161.             } catch (IOException e) {  
  162.                 e.printStackTrace();  
  163.             }  
  164.         }  
  165.         isClear = false;  
  166.     }  
  167.   
  168.     /** 
  169.      * 从LastDecryptFile中读取记录 
  170.      *  
  171.      * @param filePath 
  172.      * @return 
  173.      * @throws IOException 
  174.      */  
  175.     private String getLastDecryptFilePath(String filePath) throws IOException {  
  176.         BufferedReader br = new BufferedReader(new FileReader(filePath));  
  177.         String str = br.readLine();  
  178.         br.close();  
  179.         return str;  
  180.     }  
  181. }  


代码就是这么多,都有注释。以后再有这种需求可以直接用。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值