android中文件加密和解密的实现

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

#sex信息
YB_APP_ID = wx1c7zxc5049b364e

NB_SEX_PARTNERID=128sd72701
WY_NOTI_KEY= eSJaA8ESk6xYiTIma0px4lYO0P7yBnzz
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5

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

//定义文件名
private static final String CONFIG_PROPERTIES = "config.properties";
   
   
  • 1
  • 2
  • 1
  • 2
//调用加密方法
public static void main(String[] args) {
        //这里因为要传入输入路径和输出路径,所以要现在D盘上新建两个文件夹分别命名为encodeFile和myEncodedFile
        //我将文件放在了电脑的D盘encodeFile文件夹下,
        //并让它最后输出在D盘的myEncodedFile文件夹下
        encodeFile("D:\\encodeFile\\" + CONFIG_PROPERTIES,"D:\\myEncodedFile\\"+ CONFIG_PROPERTIES);
}
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
    /**
     *加密文件
     * @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();
        }
    }

   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67

以上这些方法就为我们生成了加密文件,然后我们在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);
     }
 }
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值