代理请求新增AES加密

Monke 7 years ago
parent ad4e5af7c7
commit 1731ed4bfe

@ -9,9 +9,9 @@ android {
applicationId "com.monke.monkeybook"
minSdkVersion 17
targetSdkVersion 26
versionCode 11
versionName "1.3.0"
manifestPlaceholders = [UMENG_CHANNEL_VALUE: "debug"]
versionCode 12
versionName "1.3.1"
manifestPlaceholders = [UMENG_CHANNEL_VALUE: "pugongying"]
}
lintOptions {

@ -1,10 +1,8 @@
package com.monke.monkeybook;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import com.monke.monkeybook.utils.AESUtil;
import org.jsoup.helper.StringUtil;
import java.util.regex.Pattern;
public class ProxyManager {
@ -16,7 +14,7 @@ public class ProxyManager {
public static boolean proxyState;
public static String proxyHttp;
private static final String proxyHttpMatch = "(http|ftp|https):\\/\\/[\\w\\-_]+(\\.[\\w\\-_]+)+([\\w\\-\\.,@?^=%&:/~\\+#]*[\\w\\-\\@?^=%&/~\\+#])?";//http正则表达式
private static final String encode = "代理包名加密key";
private static final String proxyPackageNameEncode = "代理包名加密key";
public static String packAgeEncode; //加密后的包名
public static void saveProxyState(boolean state){
proxyState = state;
@ -27,10 +25,10 @@ public class ProxyManager {
private static void initProxyState(){
try {
packAgeEncode = MApplication.getInstance().getPackageManager().getPackageInfo(MApplication.getInstance().getPackageName(), 0).packageName;
} catch (PackageManager.NameNotFoundException e) {
packAgeEncode = AESUtil.aesEncode(MApplication.getInstance().getPackageManager().getPackageInfo(MApplication.getInstance().getPackageName(), 0).packageName,proxyPackageNameEncode);
} catch (Exception e) {
e.printStackTrace();
System.out.println("=================包名获取失败,可能会影响代理请求功能");
System.out.println("=================包名获取失败,可能会影响代理请求功能===========");
}
proxyState = MApplication.getInstance().getSharedPreferences("CONFIG", 0).getBoolean(SP_KEY_PROXY_STATE,PROXY_STATE_DEFAULT);
}

@ -0,0 +1,130 @@
package com.monke.monkeybook.utils;
import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Scanner;
public class AESUtil {
public static String aesEncode(String content,String password){
byte[] result = encrypt(content,password);
if(result!=null && result.length>0){
return ParseSystemUtil.parseByte2HexStr(result);
}
return null;
}
public static String aesDecode(String content,String password){
byte[] temp = ParseSystemUtil.parseHexStr2Byte(content);
if(temp!=null && temp.length>0){
byte[] result = decrypt(temp,password);
if(result!=null && result.length>0){
return new String(result);
}
}
return null;
}
/**
* AES
*
* @param content
*
* @param keyWord
*
* @return
*/
private static byte[] encrypt(String content, String keyWord) {
try {
KeyGenerator kgen = KeyGenerator.getInstance("AES");// 创建AES的Key生产者
SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
random.setSeed(keyWord.getBytes());
kgen.init(128, random);// 利用用户密码作为随机数初始化出
// 128位的key生产者
//加密没关系SecureRandom是生成安全随机数序列password.getBytes()是种子只要种子相同序列就一样所以解密只要有password就行
SecretKey secretKey = kgen.generateKey();// 根据用户密码,生成一个密钥
byte[] enCodeFormat = secretKey.getEncoded();// 返回基本编码格式的密钥,如果此密钥不支持编码,则返回
// null。
SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");// 转换为AES专用密钥
Cipher cipher = Cipher.getInstance("AES");// 创建密码器
byte[] byteContent = content.getBytes("utf-8");
cipher.init(Cipher.ENCRYPT_MODE, key);// 初始化为加密模式的密码器
byte[] result = cipher.doFinal(byteContent);// 加密
return result;
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
}
return null;
}
/**
* AES
*
* @param content
* AES
* @param keyWord
*
* @return
*/
private static byte[] decrypt(byte[] content, String keyWord) {
try {
KeyGenerator kgen = KeyGenerator.getInstance("AES");// 创建AES的Key生产者
SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
random.setSeed(keyWord.getBytes());
kgen.init(128, random);
SecretKey secretKey = kgen.generateKey();// 根据用户密码,生成一个密钥
byte[] enCodeFormat = secretKey.getEncoded();// 返回基本编码格式的密钥
SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");// 转换为AES专用密钥
Cipher cipher = Cipher.getInstance("AES");// 创建密码器
cipher.init(Cipher.DECRYPT_MODE, key);// 初始化为解密模式的密码器
byte[] result = cipher.doFinal(content);
return result; // 明文
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
}
return null;
}
public static void main(String[] args){
while (true){
Scanner sn = new Scanner(System.in);
String content = sn.next();
// System.out.println(aesDecode(content,ServerConfig.AES_ENCODE_KEY));
System.out.println();
}
}
}

@ -0,0 +1,36 @@
package com.monke.monkeybook.utils;
public class ParseSystemUtil {
/**16
* @param buf
* @return
*/
public static String parseByte2HexStr(byte buf[]) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < buf.length; i++) {
String hex = Integer.toHexString(buf[i] & 0xFF);
if (hex.length() == 1) {
hex = '0' + hex;
}
sb.append(hex.toUpperCase());
}
return sb.toString();
}
/**16
* @param hexStr
* @return
*/
public static byte[] parseHexStr2Byte(String hexStr) {
if (hexStr==null || hexStr.length() ==0) {
return null;
}
byte[] result = new byte[hexStr.length()/2];
for (int i = 0;i< hexStr.length()/2; i++) {
int high = Integer.parseInt(hexStr.substring(i*2, i*2+1), 16);
int low = Integer.parseInt(hexStr.substring(i*2+1, i*2+2), 16);
result[i] = (byte) (high * 16 + low);
}
return result;
}
}
Loading…
Cancel
Save