求大神将此jar文件里面的函数改成能在正常运行的啊

风情万种 发布于 2015/05/07 17:09
阅读 168
收藏 0
这个是jar文件里面的函数 怎么让他能正常啊
public static String MD5(String paramString)
  {
    while (true)
    {
      char[] arrayOfChar;
      byte[] arrayOfByte1;
      int i;
      byte[] arrayOfByte2;
      StringBuffer localStringBuffer;
      int j;
      try
      {
        MessageDigest localMessageDigest = MessageDigest.getInstance("MD5");
        arrayOfChar = paramString.toCharArray();
        arrayOfByte1 = new byte[arrayOfChar.length];
        i = 0;
        if (i >= arrayOfChar.length)
        {
          arrayOfByte2 = localMessageDigest.digest(arrayOfByte1);
          localStringBuffer = new StringBuffer();
          j = 0;
          if (j < arrayOfByte2.length)
            break label85;
          return localStringBuffer.toString();
        }
      }
      catch (Exception localException)
      {
        localException.printStackTrace();
        return "";
      }
      arrayOfByte1[i] = (byte)arrayOfChar[i];
      i++;
      continue;
      label85: int k = 0xFF & arrayOfByte2[j];
      if (k < 16)
        localStringBuffer.append("0");
      localStringBuffer.append(Integer.toHexString(k));
      j++;
    }
  }



加载中
0
莱特钉
莱特钉
classpath下创建同名源文件,重写编译,运行时可覆盖
风情万种
风情万种
谢谢大神 已测试 完全和jar里面的运行结果一样[79]
风情万种
风情万种
什么意思.....?不明白
0
cnDavidChen
cnDavidChen

只能想到下面这样了:

public static String MD5(String srcStr) {
		while (true) {
			char[] srcCharArray;
			byte[] srcByteArray;
			int i;
			byte[] bytes;
			StringBuffer result;
			int j;
			try {
				MessageDigest localMessageDigest = MessageDigest.getInstance("MD5");
				srcCharArray = srcStr.toCharArray();
				srcByteArray = new byte[srcCharArray.length];
				i = 0;
				for (; i < srcCharArray.length; ) {
					srcByteArray[i] = (byte)srcCharArray[i];
					i ++;
				}
				if (i >= srcCharArray.length) {
					bytes = localMessageDigest.digest(srcByteArray);
					result = new StringBuffer();
					j = 0;
					for (;j < bytes.length;) {
						int k = 0xFF & bytes[j];
					      if (k < 16)
					    	  result.append("0");
					      result.append(Integer.toHexString(k));
					      j++;
					      
					}
					
					return result.toString();
				}
				
			} catch (Exception localException) {
				localException.printStackTrace();
				return "";
			}

		}
	}



这样的代码还不好懂,不过是正确的。把它的class文件再反编译下,是下面这样的:

public static String MD5(String srcStr)
  {
    try
    {
      MessageDigest localMessageDigest;
      char[] srcCharArray;
      byte[] srcByteArray;
      int i;
      do
      {
        localMessageDigest = MessageDigest.getInstance("MD5");
        srcCharArray = srcStr.toCharArray();
        srcByteArray = new byte[srcCharArray.length];
        i = 0;
        while (i < srcCharArray.length) {
          srcByteArray[i] = ((byte)srcCharArray[i]);
          i++;
        }
      }
      while (i < srcCharArray.length);
      byte[] bytes = localMessageDigest.digest(srcByteArray);
      StringBuffer result = new StringBuffer();
      int j = 0;
      while (j < bytes.length) {
        int k = 0xFF & bytes[j];
        if (k < 16)
          result.append("0");
        result.append(Integer.toHexString(k));
        j++;
      }

      return result.toString();
    }
    catch (Exception localException)
    {
      localException.printStackTrace();
    }return "";
  }



嗯,这样好懂多了。不过,更好的代码是这样的:

public static String MD5(String srcStr) {
		try {
			StringBuilder result = new StringBuilder();
			MessageDigest md = MessageDigest.getInstance("MD5");
			byte[] bytes = md.digest(srcStr.getBytes("utf-8"));
			for (byte b :bytes) {
				String hex = Integer.toHexString(b&0xFF);
				if (hex.length() == 1)
					result.append("0");
				result.append(hex);
			}
			return result.toString();
		}
		catch (Exception e) {
			e.printStackTrace();
			
			return "";
			// 最好是抛出异常啦
			// throw new RuntimeException(e);
		}
	}



cnDavidChen
cnDavidChen
回复 @风情万种 : 昨天测试第一个和第三个运行的结果是一致的,第二个没试过。你再试试? 第三个的运行结果是正确的。
风情万种
风情万种
回复 @himooc : 结果不一致
风情万种
风情万种
回复 @himooc : 运行接口和他加密的结果不一致
风情万种
风情万种
好牛 我试试
返回顶部
顶部