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

#sex信息
YB_APP_ID = wx1c7zxc5049b364e NB_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 = ; i < lenth; i ++){
builder.append((char) (eString.charAt(i) + i % ));
}
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[];
int count = -;
while ((count = is.read(data, , )) != -) {
outStream.write(data, , 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 = ; i < lenth; i++){
builder.append((char)(eCode.charAt(i)- i%));
}
return builder.toString();
}
} 最后我们对getEString再通过如下获取各个键-值
String[] split = getEString().split('\n' + "");
for (String sp : split) {
String[] str = sp.split("=");
String value = str[].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 中文件加密 解密 算法实战

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

  2. C#使用RSA证书文件加密和解密示例

    修改MSDN上的示例,使之可以通过RSA证书文件加密和解密,中间遇到一个小问题. Q:执行ExportParameters()方法时,回报CryptographicException:该项不适于在指定 ...

  3. .Net中的加密与解密

    加密与解密概述 加密与解密属于数据安全的范畴.在消息传输时,通过对消息进行特殊编码(加密),建立一种安全的交流方式,使得只有发送者所期望的接收者能够理解(解密).这里我们定义一个场景:发送方,接收方, ...

  4. TEA加密算法的文件加密和解密的实现

    一.TEA加密算法简介 TEA加密算法是由英国剑桥大学计算机实验室提出的一种对称分组加密算法.它采用扩散和混乱方法,对64位的明文数据块,用128位密钥分组进行加密,产生64位的密文数据块,其循环轮数 ...

  5. AES —— JAVA中对称加密和解密

    package demo.security; import java.io.IOException; import java.io.UnsupportedEncodingException; impo ...

  6. 在ASP.NET MVC环境中使用加密与解密

    在.NET Framework 4.5的NET框架中,在程序中加密与解密很方便.现在均学习ASP.NET MVC程序了,因此Insus.NET也在此写个学习的例子.在需要时可以参考与查阅. 写一个Ut ...

  7. nodejs中aes-128-cbc加密和解密

    和java程序进行交互的时候,java那边使用AES 128位填充模式:AES/CBC/PKCS5Padding加密方法,在nodejs中采用对应的aes-128-cbc加密方法就能对应上,因为有使用 ...

  8. 转 node.js和 android中java加密解密一致性问题;

    原文地址,请大家去原文博客了解; http://blog.csdn.net/linminqin/article/details/19972751 我保留一份,防止删除: var crypto = re ...

  9. java spring中对properties属性文件加密及其解密

    http://blog.csdn.net/yaerfeng/article/details/26561791

随机推荐

  1. 01_SQlite数据库简介

  2. [poj1986]Distance Queries(LCA)

    解题关键:LCA模板题 复杂度:$O(n\log n)$ #pragma comment(linker, "/STACK:1024000000,1024000000") #incl ...

  3. POJ - 3268 Silver Cow Party SPFA+SLF优化 单源起点终点最短路

    Silver Cow Party One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to ...

  4. MongoDB官方C#驱动的AsQueryable踩到坑了

    collection.AsQueryable().Where()有4个重载,分别是: public static IQueryable<TSource> Where<TSource& ...

  5. IT兄弟连 Java语法教程 运行Java程序

    编译好Java字节码文件后,接下来就应该运行Java程序了. 运行Java程序需要使用JDK中提供的java命令,因为已经把java命令所在的路径添加到了系统的Path环境变量中,因此现在可以直接使用 ...

  6. IT兄弟连 Java语法教程 Java开发环境 安装JDK

    因为我们要开发Java程序,所以必须在我们的计算机中安装Sun(Oracle)公司提供给我们的JDK.目前最新版本的JDK是JDK 10,但是我们以学习JDK 8为主,所以我们要安装的版本是JDK 8 ...

  7. js函数-构成

    前言 函数是一种封装,在任何语言中都是一个核心概念.在js中,函数是做为对象的子类型存在的.可以拥有自己的属性和方法,可以做为值进行传递,这两个特性让js拥有使用函数式编程的能力. 函数的声明 字面量 ...

  8. 教程 | Linux常用命令大全

    Linux常用命令 目录操作命令 ls 命令名称:ls 命令英文原意:list 命令所在路径:/bin/ls 执行权限:所有用户 功能描述:显示目录文件 ls (显示当前目录下文件) ls 目录名 ( ...

  9. Corn Fields(模板)

    题目链接 #include <stdio.h> #include <algorithm> #include <string.h> #include <iost ...

  10. ES6入门教程---变量和常量

    ES6提出了两个新的声明变量的命令:let 和 const 1. 建议不再使用var,而使用let 和const .优先使用const. 在定义之后值是固定不变的,即为常量 常量的值不能修改,但是如果 ...