Android对assets下文件进行加密

版权声明:转载请@我原创地址 https://blog.csdn.net/weixin_39706415/article/details/89207931

 加密效果图

解密效果

实现代码

package com.xinli.wenet.utils;

import android.util.Log;

import com.xinli.wenet.base.MyApplication;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;

/**
 * 作者 Created by maxd
 * 邮箱 [email protected]
 * 时间 on 2019/4/11 0011
 */
public class CryptUtil {
    //文件名称
    private static final String CONFIG_PROPERTIES = "config.json";
    //输出地址
    private static final String OUTPUT_FILE_PATH = "C:\\Users\\Administrator\\Desktop\\project\\";
    //输入地址
    private static final String INPUT_FILE_PATH = "C:\\Users\\Administrator\\Desktop\\apk\\";


    /**
     * 获取解密后的数据
     *
     * @param eCode 传入加密过的数据
     */
    private 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();
    }

    /**
     * 获取assets下文件进行加密处理
     */
    public static String getEncryptProperty(String filename) {

        InputStream is = null;
        ByteArrayOutputStream outStream = null;
        try {
            is = MyApplication.getAppContext().getAssets().open(filename);
            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("getEncryptProperty", "load file encode start...");
            String encode = new String(outStream.toByteArray(), "UTF-8");
            //获取解密字符串
            String stringNative = CryptUtil.getEString(encode);
            Log.i("getEncryptProperty", "load file encode end..." + stringNative);
            return stringNative;
        } catch (IOException e) {
            Log.i("getEncryptProperty", "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;
    }

    /**
     * @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 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();
            } else {
                file.delete();
                File dir = new File(file.getParent());
                dir.mkdirs();
                file.createNewFile();
            }
            FileOutputStream outStream = new FileOutputStream(file);
            outStream.write(text.getBytes("UTF-8"));
            outStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 产生加密数据
     *
     * @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();
    }


    public static void main(String[] args) {
        boolean encodeFile = encodeFile(INPUT_FILE_PATH + CONFIG_PROPERTIES,
                OUTPUT_FILE_PATH + CONFIG_PROPERTIES);
        System.out.println("result-------" + encodeFile);
    }
}

有问题请留言

猜你喜欢

转载自blog.csdn.net/weixin_39706415/article/details/89207931