Esempio n. 1
0
 public static void main(String[] args) {
   String content = "6789:;<=>?@AB012";
   String password = "******";
   byte[] mykey = HexDump.toByteBuffer(password).array();
   // 加密
   System.out.println("加密前:" + content);
   byte[] encryptResult = encrypt(content.getBytes(), mykey);
   System.out.println(
       "cipher text: " + HexDump.hexDumpCompact(encryptResult, 0, encryptResult.length));
   // 解密
   byte[] decryptResult = decrypt(encryptResult, mykey);
   System.out.println("解密后:" + new String(decryptResult));
 }
Esempio n. 2
0
 private int _doReceive() {
   try {
     int len = readBuffer.remaining();
     if (len == 0) {
       readBuffer.position(0);
       log.warn("缓冲区满,不能接收数据。dump=" + HexDump.hexDumpCompact(readBuffer));
       readBuffer.clear();
       len = readBuffer.remaining();
     }
     byte[] in = readBuffer.array();
     int off = readBuffer.position();
     int n = socket.getInputStream().read(in, off, len);
     if (n <= 0) return -1;
     lastReadTime = System.currentTimeMillis();
     readBuffer.position(off + n);
     readBuffer.flip();
   } catch (SocketTimeoutException te) {
     return 0;
   } catch (IOException ioe) {
     log.warn("client IO exception," + ioe.getLocalizedMessage() + ",peer=" + getPeerAddr());
     return -2;
   } catch (Exception e) {
     log.warn("client socket[" + getPeerAddr() + "] _doReceive:" + e.getLocalizedMessage(), e);
     return -3;
   }
   try {
     _handleBuffer();
   } catch (Exception e) {
     log.error(e.getLocalizedMessage(), e);
     return 0; // 处理异常,非通信异常。
   }
   return 0; // 成功
 }
Esempio n. 3
0
public class AESECB128 {
  private static final byte[] staticKey =
      HexDump.toByteBuffer("00000000000000000000000000000000").array();

  public static byte[] encrypt(byte[] plainText) {
    return encrypt(plainText, staticKey);
  }

  /**
   * 加密
   *
   * @param content 需要加密的内容
   * @param enckey 加密密码
   * @return
   */
  public static byte[] encrypt(byte[] content, byte[] enckey) {
    try {
      SecretKeySpec key = new SecretKeySpec(enckey, "AES");
      Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding"); // 创建密码器
      cipher.init(Cipher.ENCRYPT_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 byte[] decrypt(byte[] cipherText) {
    return decrypt(cipherText, staticKey);
  }
  /**
   * 解密
   *
   * @param content 待解密内容
   * @param deckey 解密密钥
   * @return
   */
  public static byte[] decrypt(byte[] content, byte[] deckey) {
    try {
      SecretKeySpec key = new SecretKeySpec(deckey, "AES");
      Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding"); // 创建密码器
      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) {
    String content = "6789:;<=>?@AB012";
    String password = "******";
    byte[] mykey = HexDump.toByteBuffer(password).array();
    // 加密
    System.out.println("加密前:" + content);
    byte[] encryptResult = encrypt(content.getBytes(), mykey);
    System.out.println(
        "cipher text: " + HexDump.hexDumpCompact(encryptResult, 0, encryptResult.length));
    // 解密
    byte[] decryptResult = decrypt(encryptResult, mykey);
    System.out.println("解密后:" + new String(decryptResult));
  }
}
Esempio n. 4
0
    private boolean handleMessage(IMessage msg) {
      /**
       * 业务处理器收到通信前置机上行报文,需要预先处理, 先检查是否存在此终端档案,存在才进行业务处理,不然只保存原始记录 任务,异常,主站请求返回处理:包括解析及回写数据库 原始报文保存
       */
      MessageZj zjmsg = null;
      MessageGw gwmsg = null;
      int rtua = 0;
      boolean result = true;
      if (msg.getMessageType() == MessageType.MSG_ZJ) {
        zjmsg = (MessageZj) msg;
        rtua = zjmsg.head.rtua;
      } else if (msg.getMessageType() == MessageType.MSG_GW_10) {
        gwmsg = (MessageGw) msg;
        rtua = gwmsg.head.rtua;
      }
      BizRtu rtu = (RtuManage.getInstance().getBizRtuInCache(rtua));
      if (rtu == null) {
        // 找不到终端则刷新数据库档案
        boolean refreshTag = manageRtu.refreshBizRtu(rtua);
        if (!refreshTag) log.warn("not find rtu in db:" + HexDump.toHex(rtua));
        else rtu = (RtuManage.getInstance().getBizRtuInCache(rtua));
      }
      if (rtu != null) {
        if (zjmsg != null) {

          // 任务   异常
          if (zjmsg.head.c_func == MessageConst.ZJ_FUNC_READ_TASK
              || zjmsg.head.c_func == MessageConst.ZJ_FUNC_EXP_ALARM
              || zjmsg.head.c_func == MessageConst.GG_FUNC_READ_TASK2
              || zjmsg.head.c_func == MessageConst.GG_FUNC_Event) {

            msgQueue.setLastHandleDataTime(System.currentTimeMillis());
            counter.add();
            // 如果超过处理队列最大值则放回缓存队列,以免丢失数据
            if (!asycService.addMessage(zjmsg)) {
              msgQueue.offer(zjmsg);
              result = false;
              log.warn("asycService.addMessage failed");
            }
            if (zjmsg.head.msta != 0x00
                && zjmsg.head.c_func == MessageConst.ZJ_FUNC_EXP_ALARM) { // zj事件主动召测进入这里,与主站交互.
              hostCommandHandler.handleExpNormalMsg(asycService, manageRtu, masterDbService, zjmsg);
            }
          } else if (zjmsg.head.c_func == MessageConst.GG_UPGRADE) {
            GgUpgradeProcessor.getInstance().onUpgradeReturn(zjmsg);
            return true;
          } else if (zjmsg.head.c_func == MessageConst.ZJ_FUNC_RELAY
              || zjmsg.head.c_func == MessageConst.ZJ_FUNC_READ_CUR
              || zjmsg.head.c_func == MessageConst.ZJ_FUNC_WRITE_ROBJ
              || zjmsg.head.c_func == MessageConst.ZJ_FUNC_WRITE_OBJ
              || zjmsg.head.c_func == MessageConst.ZJ_FUNC_READ_PROG
              || zjmsg.head.c_func == MessageConst.GG_FUNC_Action
              || zjmsg.head.c_func == MessageConst.GG_FUNC_READ_TASK1
              || zjmsg.head.c_func == MessageConst.GG_FUNC_AutoRegistered
              || zjmsg.head.c_func == MessageConst.GG_Pay_token) { // 主站请求返回					
            hostCommandHandler.handleExpNormalMsg(asycService, manageRtu, masterDbService, zjmsg);
          } else if (zjmsg.isLogin()) {
            saveConnectSituation(zjmsg);
          }
        } else if (gwmsg != null) {
          if (gwmsg.getAFN() == MessageConst.GW_FUNC_GET_DATA1
              || gwmsg.getAFN() == MessageConst.GW_FUNC_GET_DATA2) {
            if (gwmsg.head.c_prm == 1) { // 主动上送的一、二数据报文都认为是任务
              gwmsg.setTask(true); // 设置任务状态					
            } else { // 一、二类数据回复数据需要判断是否为任务漏点补招返回
              int fseq = gwmsg.getFseq();
              List<RtuCmdItem> rcis = masterDbService.getRtuComdItem(HexDump.toHex(rtua), fseq);
              for (RtuCmdItem rci : rcis) {
                if (rci.getZdzjbz() == Operator.GWLDBZ
                    || rci.getZdzjbz() == Operator.ZZRW_SJCJ) { // 国网任务漏点补招请求返回或者主站任务轮招数据采集返回							
                  gwmsg.setTask(true); // 设置任务状态
                  break;
                }
              }
            }
          } else if (gwmsg.getAFN() == MessageConst.GW_FUNC_AUTH) {
            GwUpdateKeyHandler.getInstance()
                .processUpdateKeyMsg(asycService, manageRtu, masterDbService, gwmsg);
            return true;
          } else if (gwmsg.getAFN() == MessageConst.GW_FUNC_RELAY_READ) {
            if (msgQueue.isDlmsRelay(gwmsg.getLogicalAddress())) {
              // 国网规约接dlms规约表中继
              if (DlmsEventProcessor.getInstance().postUpRelayMessage(gwmsg)) {
                return result;
              }
            }
          }
          // 如果是任务或异常事件则放入相应队列
          if (gwmsg.isTask() || gwmsg.getAFN() == MessageConst.GW_FUNC_GET_DATA3) {
            msgQueue.setLastHandleDataTime(System.currentTimeMillis());
            counter.add();
            // 如果超过处理队列最大值则放回缓存队列,以免丢失数据
            if (!asycService.addMessage(gwmsg)) {
              msgQueue.offer(gwmsg);
              result = false;
              log.warn("asycService.addMessage failed");
            }
            if (gwmsg.head.c_prm == MessageConst.DIR_DOWN) // 主站召测
            hostCommandHandler.handleExpNormalMsg(asycService, manageRtu, masterDbService, gwmsg);
          } else if (gwmsg.getAFN() == MessageConst.GW_FUNC_HEART) {
            // 当是登陆帧的时候,存储。
            if (gwmsg.isLogin()) {
              saveConnectSituation(gwmsg);
            }
          } else
            hostCommandHandler.handleExpNormalMsg(asycService, manageRtu, masterDbService, gwmsg);
        }
      }
      return result;
    }