Beispiel #1
0
  public static String bytesToHex(byte[] data) {
    if (data == null) {
      return null;
    }

    int len = data.length;
    String str = "";
    for (int i = 0; i < len; i++) {
      if ((data[i] & 0xFF) < 16) str = str + "0" + java.lang.Integer.toHexString(data[i] & 0xFF);
      else str = str + java.lang.Integer.toHexString(data[i] & 0xFF);
    }
    return str;
  }
  /**
   * 加密(使用DES算法)
   *
   * @param txt 需要加密的文本
   * @param key 密钥
   * @return 成功加密的文本
   * @throws InvalidKeySpecException
   * @throws InvalidKeyException
   * @throws NoSuchPaddingException
   * @throws IllegalBlockSizeException
   * @throws BadPaddingException
   */
  private static String enCrypto(String txt, String key)
      throws InvalidKeySpecException, InvalidKeyException, NoSuchPaddingException,
          IllegalBlockSizeException, BadPaddingException {
    StringBuffer sb = new StringBuffer();
    DESKeySpec desKeySpec = new DESKeySpec(key.getBytes());
    SecretKeyFactory skeyFactory = null;
    Cipher cipher = null;
    try {
      skeyFactory = SecretKeyFactory.getInstance("DES");
      cipher = Cipher.getInstance("DES");
    } catch (NoSuchAlgorithmException e) {
      e.printStackTrace();
    }
    SecretKey deskey = skeyFactory != null ? skeyFactory.generateSecret(desKeySpec) : null;
    if (cipher != null) {
      cipher.init(Cipher.ENCRYPT_MODE, deskey);
    }
    byte[] cipherText = cipher != null ? cipher.doFinal(txt.getBytes()) : new byte[0];
    for (int n = 0; n < cipherText.length; n++) {
      String stmp = (java.lang.Integer.toHexString(cipherText[n] & 0XFF));

      if (stmp.length() == 1) {
        sb.append("0" + stmp);
      } else {
        sb.append(stmp);
      }
    }
    return sb.toString().toUpperCase();
  }
Beispiel #3
0
 /**
  * 二行制转字符串
  *
  * @param b
  * @return
  */
 public static String byte2hex(byte[] b) {
   String hs = "";
   String stmp = "";
   for (int n = 0; n < b.length; n++) {
     stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
     if (stmp.length() == 1) hs = hs + "0" + stmp;
     else hs = hs + stmp;
   }
   return hs.toUpperCase();
 }
Beispiel #4
0
  /*
   * 二行制转字符串
   */
  private static String byte2hex(byte[] b) {

    StringBuffer hs = new StringBuffer();
    String stmp = "";
    for (int n = 0; n < b.length; n++) {
      stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
      if (stmp.length() == 1) hs.append("0").append(stmp);
      else hs.append(stmp);
    }
    return hs.toString().toUpperCase();
  }
Beispiel #5
0
 private static String byte2hex(byte[] b) {
   StringBuilder hs = new StringBuilder();
   for (int n = 0; n < b.length; n++) {
     String stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
     if (stmp.length() == 1) {
       hs.append("0");
     }
     hs.append(stmp);
   }
   return hs.toString();
 }
Beispiel #6
0
  /**
   * 将字节转换为16进制字符串显示. Turns an array of bytes into a String representing each byte as an unsigned
   * hex number.
   *
   * @param hash 待转换字节数组
   * @return 16进制表示的字符串
   */
  public static final String toHex(byte hash[]) {
    StringBuffer buf = new StringBuffer(hash.length * 2);
    String stmp = "";

    for (int i = 0; i < hash.length; i++) {
      stmp = (java.lang.Integer.toHexString(hash[i] & 0XFF));
      if (stmp.length() == 1) {
        buf.append(0).append(stmp);
      } else {
        buf.append(stmp);
      }
    }
    return buf.toString();
  }
Beispiel #7
0
 public String byte2hex(byte[] b) { // ������ת�ַ�
   String hs = "";
   String stmp = "";
   for (int n = 0; n < b.length; n++) {
     stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
     if (stmp.length() == 1) {
       hs = hs + "0" + stmp;
     } else {
       hs = hs + stmp;
     }
     if (n < b.length - 1) {
       hs = hs;
     }
   }
   return hs;
 }