android 中文件加密 解密 算法实战

时间:2023-03-09 08:32:59
android 中文件加密 解密 算法实战

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

思路:

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

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

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

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

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