最近项目中需要用到加解密功能,言外之意就是不想让人家在反编译后通过不走心就能获取文件里一些看似有用的信息,但考虑到加解密的简单实现,这里并不使用AES或DES加解密
为了对android中assets文件里的数据加密,我决定自己动手丰衣足食。
首先我们需要一个配置文件命名为config.properties
数据如下:

#sex信息
YB_APP_ID = wx1c7zxc5049b364eNB_SEX_PARTNERID=128sd72701
WY_NOTI_KEY= eSJaA8ESk6xYiTIma0px4lYO0P7yBnzz

上述信息格式有些不整齐,但无关大雅,通过后续代码正好可以验证只要是类似的数据都会加密成功,因为我们会剔除带有”#”的数据注释,和两条数据之间的空格,以及通过”=”号分隔最终会把每一条数据之间的含有的空格也排除在外,所以”=”后面跟的数据会按照正确的方式被加密。
然后通过java代码如下:

//定义文件名
private static final String CONFIG_PROPERTIES = "config.properties";
//调用加密方法
public static void main(String[] args) {//这里因为要传入输入路径和输出路径,所以要现在D盘上新建两个文件夹分别命名为encodeFile和myEncodedFile//我将文件放在了电脑的D盘encodeFile文件夹下,//并让它最后输出在D盘的myEncodedFile文件夹下encodeFile("D:\\encodeFile\\" + CONFIG_PROPERTIES,"D:\\myEncodedFile\\"+ CONFIG_PROPERTIES);
}
    /***加密文件* @param inputPath 输入路径* @param outputPath 输出路径* @return 是否加密文件成功*/private static boolean encodeFile(String inputPath, String outputPath) {File localFile = new File(inputPath);try {if (!localFile.exists()) {return false;}StringBuilder builder = new StringBuilder();BufferedReader in = new BufferedReader(new FileReader(localFile));String line = "";while ((line = in.readLine()) != null) {if (!line.trim().startsWith("#") && !line.trim().equals("")) {builder.append(line + '\n');}}System.out.print("AA..:" + builder.toString());//产生加密文件generateFile(builder2Encode(builder.toString()), outputPath);return true;} catch (IOException e) {e.printStackTrace();}return false;}/*** 实现算法产生加密数据* @param eString 要加密的数据* @return 加密后的数据*/private static String builder2Encode(String eString) {StringBuilder builder = new StringBuilder();long lenth = eString.length();for (int i = 0; i < lenth; i ++){builder.append((char) (eString.charAt(i) + i % 5));}System.out.println("=========encode string======================");System.out.print("AA..:\\"+builder.toString());return builder.toString();}/***产生加密文件* @param text 要加密的数据* @param filePath 输出路径*/private static void generateFile(String text, String filePath) {try {File file = new File(filePath);if (!file.exists()) {File dir = new File(file.getParent());dir.mkdirs();file.createNewFile();}FileOutputStream outStream = new FileOutputStream(file);outStream.write(text.getBytes());outStream.close();} catch (Exception e) {e.printStackTrace();}}

以上这些方法就为我们生成了加密文件,然后我们在android项目中再通过非对称解密,反解出真正需要的数据用于处理实际操作,
这里因为要先运行以上这个加密demo,然后用在项目中,的确会有些繁琐,但因为assets文件是只读文件,所以我们能操作的也就这点能力了。

接下来我们看Android里如何实现解密:

    /*** 获取解密信息* @param c* @return*/public static String getEncryptProperty(Context c) {InputStream is = null;ByteArrayOutputStream outStream = null;try {is = c.getAssets().open("config.properties");outStream = new ByteArrayOutputStream();byte[] data = new byte[1024];int count = -1;while ((count = is.read(data, 0, 1024)) != -1) {outStream.write(data, 0, count);}Log.i("load file encode start...");String encode = new String(outStream.toByteArray(), "UTF-8");//获取解密字符串String stringNative = GaoCryptUtil.getEString(encode);Log.i("load file encode end..."+stringNative);return stringNative;} catch (IOException e) {Log.i("load file encode end..."+e.toString());e.printStackTrace();} finally {try {if (is != null) {is.close();is = null;}if (outStream != null) {outStream.close();outStream = null;}} catch (IOException e) {e.printStackTrace();}}return null;}public class GaoCryptUtil {/*** 获取解密后的数据* @param eCode 传入加密过的数据* @return*/public static String getEString(String eCode){StringBuilder builder = new StringBuilder();long lenth = eCode.length();for (int i = 0; i < lenth; i++){builder.append((char)(eCode.charAt(i)- i%5));}return builder.toString();}
}最后我们对getEString再通过如下获取各个键-值
String[] split = getEString().split('\n' + "");for (String sp : split) {String[] str = sp.split("=");String value = str[1].trim();if(sp.contains("YB_APP_ID")){android.util.Log.d("aa","YB_APP_ID:"+value);}else if(sp.contains("NB_SEX_PARTNERID")){   android.util.Log.d("aa","NB_SEX_PARTNERID:"+value);}else if(sp.contains("WY_NOTI_KEY")){   android.util.Log.d("aa","NB_SEX_PARTNERID:"+value);}}

总结:这样在经过加密和解密一圈后,我们又回到起点最终拿到真实的数据,做后续操作。以上只是我对加密和解密的一种实现,粗糙的地方不要见怪哈。
加密demo看这里

android中文件加密和解密的实现相关推荐

  1. android obb在哪,在Android中使用加密的OBB文件

    我想知道人们是否成功地在Android中创建/加载加密的OBB(不透明二进制Blob)文件?这是一个跟进这个question 1:What is OBB(Opaque Binary Blob) in ...

  2. tkinter实现文件加密和解密

    源代码:https://gitee.com/mydreamambitious/file-encryption-and-decryption 注:这个代码和界面是做好的,但是还在进行优化和美化中.但是在 ...

  3. 【译】Android中的安全数据— Android中的加密(第2部分)

    目录 锁屏 选择一个钥匙 密钥存储 密钥生成 密钥管理 加密与解密 使用范例 下一步是什么 安全提示 锁屏 如果要保护数据,请保护设备. 为了更加安全,在提供对任何应用程序功能的访问权限之前,我们可以 ...

  4. Windows中EFS加密及解密应用

    Windows中EFS加密及解密应用<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office&q ...

  5. Java基础篇之利用IO流给文件加密、解密

    目标:给文件加密.解密(最基础版) 注意: a.加密过程:先读取整个目标文件,然后将目标文件写入一个新的文件中,在写入的过程中,每隔几个字符插入一个加密符号(随意定),此时这个新的文件就是加密文件 b ...

  6. (C语言)简单的文件加密和解密程序

    一个简单的文件加密和解密程序 main.c #include <stdio.h> #include <stdlib.h> #include "fun.h"i ...

  7. 文件加密和解密软件:AutoCrypt for mac

    为大家推荐一款好用的文件加密和解密软件,AutoCrypt for mac具备强大的AES-256算法,只需点击一下,即可加密并保存在自定义位置,能够轻松帮助用户加密或解密文件,而且autocrypt ...

  8. Android 中文件类型与MIME的匹配表

    Android 中文件类型与MIME的匹配表 转载于:https://www.cnblogs.com/zhujiabin/p/5669008.html

  9. 文件加密和解密 - 密钥存储

    当我们想要做一次加密系统,或者只是有一个关于这个问题,它是如何保存的加密和解密密钥. 一般认为想要的文件加密和解密,对称算法用于.一般是AES要么DES. 这就存在密钥管理的问题,它是如何? 基本上两 ...

最新文章

  1. 六周第四次课(5月2日)
  2. 透過proxychains讓不支持代理的程序通過代理上網
  3. 引导win7+linux系统安装,win7 + ubuntu16.04LTS双系统安装(Legacy引导模式)
  4. Hadoop分布式文件系统hdfs的shell操作命令大全
  5. redis分片_5000+字硬核干货!Redis 分布式集群部署实战
  6. linux nfs系统客户端,Linux系统中挂载共享目录NFS文件系统客户端安装与配置
  7. 如何批量转化成jpg格式_heic图片格式快速转换jpg,批量转换方法
  8. ACwing 2. 01背包问题(DP)
  9. 产品经理如果有捷径,那可能是多读书
  10. 爱上一个人的七个预兆
  11. 华为P30系列双景录像功能上线:同屏展示全景与特写
  12. SQLServer left join 出现比左表多的数据
  13. 使用jquery第三方插件(生成曲线图)
  14. VisualBrush
  15. 7寸显示器 树莓派4b_树莓派7寸触摸屏安装指南
  16. 局域网IPC入侵心得
  17. Salesforce随笔: 解决被指定给Chatter相关用户的RecordType无法被删除的问题
  18. 【软件工程】第一、二章总结
  19. 微信小程序版本更新后提示用户更新
  20. 《曾文正公嘉言钞》读书笔记

热门文章

  1. 查看android模拟器ip地址
  2. 2018最新程序员必备技术类微信公众号
  3. java安全管理器SecurityManager入门
  4. 易拉罐增强WiFi信号
  5. 新概念二册 Lesson 18 He often does this!他经常干这种事! ( have的用法)
  6. LeCun称梯度下降是最优雅的 ML 算法,Marcus:我不同意!
  7. 英文文章汇总+翻译小亮点
  8. 三分钟熟悉进制转换与位运算
  9. git push.default Update were rejected because a pushed branch tip is behand remote
  10. 微信企业号开发:微信考勤百度地图定位,错误修正