App打包以后 assets 下的文件 是明文的 为了安全 对文件进行加密 用的时候解密
加密后
解密后
加密代码
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
* 邮箱 maxd@nroad.com.cn
* 时间 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);
}
}
原理为 取得文件的字符串 减去 固定的值 取得是 ASC码 值
获取文件夹下多个文件
public static void main(String[] args) {
String path = "D:\\JAVA"; //要遍历的路径
File file = new File(path); //获取其file对象
func(file);
}
private static void func(File file){
File[] fs = file.listFiles();
for(File f:fs){
if(f.isDirectory()) //若是目录,则递归打印该目录下的文件
func(f);
if(f.isFile()) //若是文件,直接打印
System.out.println(f);
}
}
private static List<File> func(File file){
File[] fs = file.listFiles();
List<File> list = new ArrayList();
for(File f:fs){
if(f.isDirectory()) {
//若是目录,则递归打印该目录下的文件
func(f);
}
if(f.isFile()) {
//若是文件,直接打印
System.out.println(f.getPath());
System.out.println(f.getName());
list.add(f);
}
}
return list;
}