android中檔案加密和解密的實現
阿新 • • 發佈:2019-01-27
最近專案中需要用到加解密功能,言外之意就是不想讓人家在反編譯後通過不走心就能獲取檔案裡一些看似有用的資訊,但考慮到加解密的簡單實現,這裡並不使用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看這裡