assets下的文件加密/解密

import android.content.Context;
import android.os.Environment;
import android.util.Log;

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;

public class EncryptUtil {
    //输入地址(加密后的地址)
    public static final String SD_PATH = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "videorecord" + File.separator + "jiamiData.txt";
    //输出地址(解密后的文件地址)
    public static final String SD_PATH2 = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "videorecord" + File.separator + "jiemiData.txt";


    /**
     * 获取加密后的数据
     *
     * @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(Context context,String filename) {

        InputStream is = null;
        ByteArrayOutputStream outStream = null;
        try {
            is = context.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.e("AAAAAA", "load file encode start...");
            String encode = new String(outStream.toByteArray(), "UTF-8");
            //获取解密字符串
            String stringNative = getEString(encode);
            Log.e("AAAAAA", "--------获取加密数据=====" + stringNative);
            String s = builder2Encode(stringNative);
//            Log.i("getEncryptProperty", "--------解密数据===" + s);

            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 解密后的数据
     */
    public static String builder2Encode(String eString) {
        Log.e("AAAAAA=" ,"加密数据==="+ 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======================");
        Log.e("AAAAAA=" ,"解密数据==="+ builder.toString());
        return builder.toString();
    }
    

}

你可能感兴趣的:(assets下的文件加密/解密)