最近项目中需要用到加解密功能,言外之意就是不想让人家在反编译后通过不走心就能获取文件里一些看似有用的信息,但考虑到加解密的简单实现,这里并不使用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. 把myeclipse中的web项目导入eclipse中不能编程web项目的解决办法

    title: 把myeclipse中的web项目导入eclipse中不能编程web项目的解决办法 tags: grammar_cjkRuby: true --- 右键单击项目,properties-- ...

  2. css3 翻起页脚

    html <div class="demo2"> <img src="images/1.jpg"> </div> css . ...

  3. Object中的方法

    1.equals() 2.toString() package com_package1; public class Person44 { private int age; public int ge ...

  4. Stored Procedures CASE 用法错误

    )) )     select @type=[type] from sys.objects with(nolock) where name=@ObjectName          case @typ ...

  5. poj3535 A+B (大数加法)

    A+B Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 811   Accepted: 371 Description The ...

  6. Spring Boot错误errMsg: "request:ok"

    在把评论写到数据库并且动态刷新评论区的时候,有时候正常写入,有时候就会有“request:ok”的的错误出现,错误信息如下: data: {timestamp: , error: "Inte ...

  7. [翻译]Nativescript 中 Web 视图与 Android/IOS 的双向通信

    English document From http://shripalsoni.com/blog/nativescript-webview-native-bi-directional-communi ...

  8. Gym - 100851A Adjustment Office(O(1)求行列和)

    Adjustment Office Gym - 100851A 2       3       4 3       4       5 4       5       6 n<=10^6,q&l ...

  9. 用VisualSVN Server创建版本库,以及TortoiseSVN的使用

    介绍了VisualSVN Server和TortoiseSVN的下载,安装,汉化. SVN服务器搭建和使用(一) Subversion是优秀的版本控制工具,其具体的的优点和详细介绍,这里就不再多说. ...

  10. uva11584 Partitioning by Palindromes

    题目大意: 给出一个字符串,把他划分成尽量少的回文串,问最少的回文串个数 /* 先预处理所有回文子串 dp[i]表示字符1~i划分成的最小回文串的个数 */ #include<iostream& ...