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. }  



 

代碼就是這么多,都有注釋。以后再有這種需求可以直接用。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM