android 中文件加密 解密 算法实战
现在项目里面有一个需求,本项目里面下载的视频和文档都不允许通过其他的播放器播放,在培训机构里面这样的需求很多。防止有人交一份钱,把所有的课件就拷给了别人。这样的事情培训机构肯定是不愿意的。现在我项目里面也出了这么个需求。下面介绍一下我的实现。
思路:
首先下载文件,这个就不说了,java代码写个下载管理器。
下载完成后存储文件的时候不是直接存储,要加密存储,加密方法是将文件的每个字节与这个字节在流中的下标做异或运算。
在我们项目里面播放的时候要解密,方法也是将文件的每个字节与这个字节在流中的下标做异或运算。两次异或得到的就是没有加密的值。
- /**
- * 加密解密管理类
- *
- * 加密算法 : 将文件的数据流的每个字节与该字节的下标异或.
- * 解密算法 : 已经加密的文件再执行一次对文件的数据流的每个字节与该字节的下标异或
- *
- * @author Administrator
- *
- */
- public class FileEnDecryptManager {
- private FileEnDecryptManager() {
- }
- private static FileEnDecryptManager instance = null;
- public static FileEnDecryptManager getInstance() {
- synchronized (FileEnDecryptManager.class) {
- if (instance == null)
- instance = new FileEnDecryptManager();
- }
- return instance;
- }
- /**
- * 记录上次解密过的文件名
- */
- private final String LastDecryptFile = Framework
- .getModule(DownloadModule.class).getDownloadDir().getAbsolutePath()
- + "/LastDecryptFilename.ttt";
- /**
- * LastDecryptFilename.ttt 文件是否被清空
- */
- private boolean isClear = false;
- /**
- * 加密入口
- *
- * @param fileUrl
- * 文件绝对路径
- * @return
- */
- public boolean InitEncrypt(String fileUrl) {
- encrypt(fileUrl);
- return true;
- }
- private final int REVERSE_LENGTH = 56;
- /**
- * 加解密
- *
- * @param strFile
- * 源文件绝对路径
- * @return
- */
- private boolean encrypt(String strFile) {
- int len = REVERSE_LENGTH;
- try {
- File f = new File(strFile);
- RandomAccessFile raf = new RandomAccessFile(f, "rw");
- long totalLen = raf.length();
- if (totalLen < REVERSE_LENGTH)
- len = (int) totalLen;
- FileChannel channel = raf.getChannel();
- MappedByteBuffer buffer = channel.map(
- FileChannel.MapMode.READ_WRITE, 0, REVERSE_LENGTH);
- byte tmp;
- for (int i = 0; i < len; ++i) {
- byte rawByte = buffer.get(i);
- tmp = (byte) (rawByte ^ i);
- buffer.put(i, tmp);
- }
- buffer.force();
- buffer.clear();
- channel.close();
- raf.close();
- return true;
- } catch (Exception e) {
- e.printStackTrace();
- return false;
- }
- }
- /**
- * 解密入口
- *
- * @param fileUrl
- * 源文件绝对路径
- */
- public void Initdecrypt(String fileUrl) {
- try {
- if (isDecripted(fileUrl)) {
- decrypt(fileUrl);
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- private void decrypt(String fileUrl) {
- encrypt(fileUrl);
- }
- /**
- * fileName 文件是否已经解密了
- *
- * @param fileName
- * @return
- * @throws IOException
- */
- private boolean isDecripted(String fileName) throws IOException {
- // 上次加密的文件
- File lastDecryptFile = new File(LastDecryptFile);
- if (lastDecryptFile.exists() && isClear == false) {
- String lastDecryptfilepath = getLastDecryptFilePath(LastDecryptFile);
- if (lastDecryptfilepath != null
- && lastDecryptfilepath.equals(fileName)) {
- return false;
- } else {
- clear();
- }
- }
- StringBufferWrite(fileName);
- return true;
- }
- /**
- * 将需要加密的文件绝对路径写入LastDecryptFile
- *
- * @param filePath
- * 需要加密的文件绝对路径
- * @param content
- * @throws IOException
- */
- private void StringBufferWrite(String filePath) throws IOException {
- File lastDecryptFile = new File(LastDecryptFile);
- if (!lastDecryptFile.exists())
- lastDecryptFile.createNewFile();
- FileOutputStream out = new FileOutputStream(lastDecryptFile, true);
- StringBuffer sb = new StringBuffer();
- sb.append(filePath);
- out.write(sb.toString().getBytes("utf-8"));
- out.close();
- }
- /**
- * 清空加密记录
- */
- public synchronized void clear() {
- isClear = true;
- File decryptTempFile = new File(LastDecryptFile);
- if (decryptTempFile.exists()) {
- try {
- String fileName = getLastDecryptFilePath(LastDecryptFile);
- decrypt(fileName);
- new File(LastDecryptFile).delete();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- isClear = false;
- }
- /**
- * 从LastDecryptFile中读取记录
- *
- * @param filePath
- * @return
- * @throws IOException
- */
- private String getLastDecryptFilePath(String filePath) throws IOException {
- BufferedReader br = new BufferedReader(new FileReader(filePath));
- String str = br.readLine();
- br.close();
- return str;
- }
- }
代码就是这么多,都有注释。以后再有这种需求可以直接用。
android 中文件加密 解密 算法实战的更多相关文章
- Android中文件加密和解密的实现
最近项目中需要用到加解密功能,言外之意就是不想让人家在反编译后通过不走心就能获取文件里一些看似有用的信息,但考虑到加解密的简单实现,这里并不使用AES或DES加解密 为了对android中assets ...
- 转 node.js和 android中java加密解密一致性问题;
原文地址,请大家去原文博客了解; http://blog.csdn.net/linminqin/article/details/19972751 我保留一份,防止删除: var crypto = re ...
- N个整数(数的大小为0-255)的序列,把它们加密为K个整数(数的大小为0-255).再将K个整数顺序随机打乱,使得可以从这乱序的K个整数中解码出原序列。设计加密解密算法,且要求K<=15*N.
N个整数(数的大小为0-255)的序列,把它们加密为K个整数(数的大小为0-255).再将K个整数顺序随机打乱,使得可以从这乱序的K个整数中解码出原序列.设计加密解密算法,且要求K<=15*N. ...
- 兼容javascript和C#的RSA加密解密算法,对web提交的数据进行加密传输
Web应用中往往涉及到敏感的数据,由于HTTP协议以明文的形式与服务器进行交互,因此可以通过截获请求的数据包进行分析来盗取有用的信息.虽然https可以对传输的数据进行加密,但是必须要申请证书(一般都 ...
- 2019-2-20C#开发中常用加密解密方法解析
C#开发中常用加密解密方法解析 一.MD5加密算法 我想这是大家都常听过的算法,可能也用的比较多.那么什么是MD5算法呢?MD5全称是 message-digest algorithm 5[|ˈmes ...
- Android逆向之旅---Android中锁屏密码算法解析以及破解方案
一.前言 最近玩王者荣耀,下载了一个辅助样本,结果被锁机了,当然破解它很简单,这个后面会详细分析这个样本,但是因为这个样本引发出的欲望就是解析Android中锁屏密码算法,然后用一种高效的方式制作锁机 ...
- 【转】C#中RSA加密解密和签名与验证的实现
[转]C#中RSA加密解密和签名与验证的实现 RSA加密算法是一种非对称加密算法.在公钥加密标准和电子商业中RSA被广泛使用.RSA是1977年由罗纳德•李维斯特(Ron Rivest).阿迪•萨莫尔 ...
- .Net中的加密解密
返回博客列表 转 .Net中的加密解密 李朝强 发布时间: 2015/11/23 12:55 阅读: 33 收藏: 3 点赞: 0 评论: 0 在一些比较重要的应用场景中,通过网络传递数据需要进行加密 ...
- RC4加密解密算法
RC4相对是速度快.安全性高的加密算法.在实际应用中,我们可以对安全系数要求高的文本进行多重加密,这样破解就有一定困难了.如下测试给出了先用RC4加密,然后再次用BASE64编码,这样双重锁定,保证数 ...
随机推荐
- 理解JavaScript中作用域链的关系
javascript里的关系又多又乱.作用域链是一种单向的链式关系,还算简单清晰:this机制的调用关系,稍微有些复杂:而关于原型,则是prototype.proto和constructor的三角关系 ...
- Java 图片设置圆角(设置边框,旁白)
/** * 图片设置圆角 * @param srcImage * @param radius * @param border * @param padding * @return * @throws ...
- 在OC中调用Swift类中定义delegate出现:Property 'delegate' not found on object of type ...
找了许久没找到答案, 在下面的链接中, 我解决了这个问题: http://stackoverflow.com/questions/26366082/cannot-access-property-of- ...
- 关于textarea标签在谷歌跟火狐可以拖动大小
关于textarea标签在谷歌和火狐可以拖动大小 而在IE是不会出现这种情况的 解决的方法:我们给这个标签加个 resize: none; 就可以解决了
- [Leetcode][016] 3Sum Closest (Java)
题目: https://leetcode.com/problems/3sum-closest/ [标签]Array; Two Pointers [个人分析] 这道题和它的姊妹题 3Sum 非常类似, ...
- javascript一些常用函数
1.indexof 方法可返回某个指定的字符串值在字符串中首次出现的位置. 注释:indexOf() 方法对大小写敏感! 如果要检索的字符串值没有出现,则该方法返回 -1. 例 : 在本例中,我们将 ...
- VNC配置连接远程服务器桌面-linux\windows
一.VNC配置连接远程服务器桌面 1.服务器安装VNC-server # yum -y install vnc-server 2.配置VNC连接登陆密码 # vncpasswd 回车 3.配置VNC- ...
- fgets和scanf的区别
fgets和scanf的区别 1.测试使用scanf的一个例子: #include "stdio.h" #include "string.h" int main ...
- Swift—计算属性-备
计算属性本身不存储数据,而是从其他存储属性中计算得到数据. 计算属性概念: 计算属性提供了一个getter(取值访问器)来获取值,以及一个可选的setter(设置访问器)来间接设置其他属性或变量的值. ...
- 提交svn报错说 有 unversioned 的文件
这个说明 有未add的图片等东西,需要先add进去再提交