zoukankan      html  css  js  c++  java
  • android 中文件加密 解密 算法实战

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

    思路:

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

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

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

    [java] view plaincopy在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. }  



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

  • 相关阅读:
    Python之转换py文件为无需依赖python环境的exe文件的方法
    Python之回调函数
    Python之静态语法检查
    Ubuntu中一次更改用户名带来的连锁反应
    Python之FTP传输
    qemu 源码调试
    Lattice Reduction (LLL) 算法C代码实现
    一道Apple公司(中国)的面试题目
    github免密码设置
    HiHo Coder字典树 TrieTree
  • 原文地址:https://www.cnblogs.com/dongweiq/p/4270258.html
Copyright © 2011-2022 走看看