test.java 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package com.iamberry.rst.utils;
  2. import com.alibaba.dubbo.common.json.JSON;
  3. import com.auth0.jwt.internal.org.apache.commons.codec.binary.Base64;
  4. import com.auth0.jwt.internal.org.apache.commons.codec.binary.StringUtils;
  5. import net.sf.json.JSONArray;
  6. import net.sf.json.JSONObject;
  7. //import org.apache.kafka.clients.consumer.ConsumerRecord;
  8. import java.io.File;
  9. import java.io.FileInputStream;
  10. import java.security.Key;
  11. import java.util.Map;
  12. import javax.crypto.Cipher;
  13. import javax.crypto.spec.SecretKeySpec;
  14. import java.security.Key;
  15. public class test {
  16. private static final String AES = "AES";
  17. public static String decrypt(String encryptedData, String secretKey) throws Exception {
  18. Key key = new SecretKeySpec(secretKey.getBytes(), AES);
  19. Cipher c = Cipher.getInstance(AES);
  20. c.init(2, key);
  21. byte[] decodedValue = Base64.decodeBase64(encryptedData);
  22. byte[] decValue = c.doFinal(decodedValue);
  23. String decryptedValue = StringUtils.newStringUtf8(decValue);
  24. return decryptedValue;
  25. }
  26. /* public static void main(String[] args) throws Exception {
  27. ConsumerRecord<String, String> record = new ConsumerRecord<String, String>("4", 1, 1, "data", "{\"data\":\"7uiBfrOFcdy/EDWxcT6SlSH10bmyKW4BoILZ7f4LQygzN9scB2To4W3gRf0nzI+kHOKWzrv5DHEfJNt9/QrlfTk6VcHGwiPGycwiPvDkoTe4eJOW8Qqwm/1H4nNX+1Ed/CTueaJe3E/BNsFxe7rF+qYIHqZnL4b2n9Padc5ySenpG7G0a5xepWP0KZYDN4KeCTCNBf4XIxuHSB/BGVZpATEykU1vILnX89GzoJlV+10=\"}");
  28. String appKey = "5kkyurvvtt58bbuxueee";//填APP KEY
  29. String secretKey = "rhj6na6u3y6uhy6qrbb3944mg5uqqpbb";//APP SECRET
  30. String data = decrypt(JSONObject.fromObject(record.value()).getString("data"),
  31. secretKey.substring(8, 24));//解析后的真正数据
  32. JSONObject jasonObject = JSONObject.fromObject(data);
  33. System.out.println(jasonObject);
  34. if(jasonObject.has("dps")){
  35. JSONArray dps = jasonObject.getJSONArray("dps");
  36. boolean flog = false;
  37. for(int i = 0; i < dps.size(); i++){
  38. Map maps = (Map) JSON.parse(dps.getString(i));
  39. for (Object obj : maps.keySet()){
  40. if(obj.equals("DEVECE_CONTROL_MILK")){
  41. String milkPowder = maps.get(obj).toString();
  42. flog = true;//如果数据中包含当前字段,表示当前推送的数据为奶粉记录
  43. }
  44. if(obj.equals("t")){
  45. Long milkTime = (Long)maps.get((obj));
  46. }
  47. }
  48. }
  49. if(flog){
  50. // 根据机器ID 获取对应的用户id
  51. String devId = jasonObject.getString("devId");
  52. flog = false;
  53. }
  54. }
  55. }*/
  56. }