public class SGMWUtil {
//上传企业基本信息
public static String authKey_Por = "6DEF9596FB92DB5BE053360116AC258C";
public static String authPWD_Por = "6DEF9596FB93DB5BE053360116AC258C";
//上传电池单体备案数据
public static String authKey_MnR = "6DEF8492F4CED9F2E053360116ACFF11";
public static String authPWD_MnR = "6DEF8492F4CFD9F2E053360116ACFF11";
public static JSONObject httpInterfaceForJson(String requestUrl, String requestMethod, JSONObject fullJson) {
String res = "";
StringBuffer buffer = new StringBuffer();
HttpURLConnection httpUrlConn = null;
try {
URL url = new URL(requestUrl);
httpUrlConn = (HttpURLConnection) url.openConnection();
httpUrlConn.setDoOutput(true);
httpUrlConn.setDoInput(true);
httpUrlConn.setUseCaches(false);
// httpUrlConn.setRequestProperty("Accept", "text/plain");
httpUrlConn.setRequestProperty("Content-Type", "application/json");
httpUrlConn.setRequestMethod(requestMethod);
httpUrlConn.getOutputStream().write(fullJson.toString().getBytes("UTF-8"));
InputStream inputStream = httpUrlConn.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String str = null;
while((str = bufferedReader.readLine()) != null) {
buffer.append(str);
}
bufferedReader.close();
inputStreamReader.close();
inputStream.close();
inputStream = null;
httpUrlConn.disconnect();
res = buffer.toString();
} catch (ConnectException var21) {
} catch (Exception var22) {
} finally {
try {
httpUrlConn.disconnect();
} catch (Exception var20) {
}
}
return JSONObject.parseObject(res);
}
public static String encodeMD5(String str){
String strDigest = "";
try{
MessageDigest md5 = MessageDigest.getInstance("MD5");
byte[] data = md5.digest(str.getBytes("utf-8"));
strDigest = bytesToHexString(data);
} catch (Exception ex){
throw new RuntimeException(ex);
}
return strDigest;
}
public static String bytesToHexString(byte[] src){
StringBuilder stringBuilder = new StringBuilder("");
if(src == null || src.length <= 0){
return null;
}
for (int i = 0; i < src.length; i++) {
int v = src[i] & 0xFF;
String hv = Integer.toHexString(v);
if(hv.length() < 2){
stringBuilder.append(0);
}
stringBuilder.append(hv);
}
return stringBuilder.toString();
}
public static String encrypt(String content,String password){
try{
if(StringUtils.isEmpty(content))
return "";
KeyGenerator kgen = KeyGenerator.getInstance("AES");
SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
random.setSeed(password.getBytes());
kgen.init(128,random);
SecretKey secretKey = kgen.generateKey();
byte[] enCodeFormat = secretKey.getEncoded();
SecretKeySpec key = new SecretKeySpec(enCodeFormat,"AES");
Cipher cipher = Cipher.getInstance("AES");
byte[] byteContent = content.getBytes("utf-8");
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] result = cipher.doFinal(byteContent);
String str = Base64.getEncoder().encodeToString(result);
return str;
} catch (NoSuchAlgorithmException e) {
} catch (NoSuchPaddingException e){
} catch (InvalidKeyException e) {
} catch (UnsupportedEncodingException e){
} catch (IllegalBlockSizeException e){
} catch (BadPaddingException e) {
}
return null;
}
public static String decrypt(String str,String password){
try{
byte[] content = Base64.getDecoder().decode(str);
KeyGenerator kgen = KeyGenerator.getInstance("AES");
SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
secureRandom.setSeed(password.getBytes());
kgen.init(128,secureRandom);
SecretKey secretKey = kgen.generateKey();
byte[] enCodeFormat = secretKey.getEncoded();
SecretKeySpec key = new SecretKeySpec(enCodeFormat,"AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE,key);
byte[] result = cipher.doFinal(content);
return new String(result,"UTF-8");
} catch (NoSuchAlgorithmException e){
} catch (NoSuchPaddingException e){
} catch (InvalidKeyException e){
} catch (IllegalBlockSizeException e){
} catch (BadPaddingException e){
} catch (Exception e){
}
return "";
}
public static String insertDataToSgmw(String url,JSONObject reqContent,String authKey,String authPWD){
//接口调用
String reqGUID = BaseUtil.getUUID();
JSONObject reqHead = new JSONObject();
reqHead.put("authKey",authKey);
reqHead.put("encryptMode","AES");
reqHead.put("reqGUID",reqGUID);
reqHead.put("reqSign",encodeMD5(authKey+"|"+authPWD+"|"+reqGUID));
JSONObject reqBody = new JSONObject();
reqBody.put("reqContent",encrypt(reqContent.toString(),authPWD));
JSONObject fullJson = new JSONObject();
fullJson.put("reqHead",reqHead);
fullJson.put("reqBody",reqBody);
JSONObject jsonObject = httpInterfaceForJson(url,
"POST", fullJson);
if(jsonObject == null || jsonObject.get("rspHead") == null || JSONObject.parseObject(jsonObject.get("rspHead").toString()).get("rspCode") == null){
return "0";
}
return JSONObject.parseObject(jsonObject.get("rspHead").toString()).get("rspCode").toString();
}
}