请大牛将一段JAVA改成PHP实现

题目描述

JAVA MD5加密返回字符串 改PHP实现

题目来源及自己的思路

想要一个PHP 函数实现这段又臭又长又古老的JAVA代码 有没有同时熟悉JAVA和PHP的大大啊

相关代码

入口文件

public class AppletLogin {  
  public static void main(String[] paramArrayOfString) {
    String aaa = "AAAAABBBBB";    //这是字符串aaa
        char[] arrayOfChar = { 
        '!', 'E', '4', '\022', ' ', ', '!', ', 
        '\020', ' ', 
        '4', '#', '4', ', '\037', '\023', 
        '\035', '-', 'L', '3', 
        '9', ', ',', '4', 
        ', ', 'O', ', ', 'E', 
        ':', '};
    String str = String.valueOf(new String(arrayOfChar)) + aaa;
    MD5 mD5 = new MD5();
    mD5.Init();
    mD5.Update(str);
    String bbb = mD5.asHex();   //这是字符串bbb
  }
}

字符有点问题 附上函数图片
微信截图_20200419160933.png
MD5.class类文件

public class MD5 {
  MD5State state;
  
  MD5State finals;
  
  static byte[] padding = new byte[] { Byte.MIN_VALUE };
  
  public synchronized void Init() {
    this.state = new MD5State();
    this.finals = null;
  }
  
  public MD5() {
    Init();
  }
  
  public MD5(Object paramObject) {
    this();
    Update(paramObject.toString());
  }
  
  private int rotate_left(int paramInt1, int paramInt2) {
    return paramInt1 << paramInt2 | paramInt1 >>> 32 - paramInt2;
  }
  
  private int uadd(int paramInt1, int paramInt2) {
    long l1 = paramInt1 & 0xFFFFFFFFL;
    long l2 = paramInt2 & 0xFFFFFFFFL;
    l1 += l2;
    return (int)(l1 & 0xFFFFFFFFL);
  }
  
  private int uadd(int paramInt1, int paramInt2, int paramInt3) {
    return uadd(uadd(paramInt1, paramInt2), paramInt3);
  }
  
  private int uadd(int paramInt1, int paramInt2, int paramInt3, int paramInt4) {
    return uadd(uadd(paramInt1, paramInt2, paramInt3), paramInt4);
  }
  
  private int FF(int paramInt1, int paramInt2, int paramInt3, int paramInt4, int paramInt5, int paramInt6, int paramInt7) {
    paramInt1 = uadd(paramInt1, paramInt2 & paramInt3 | (paramInt2 ^ 0xFFFFFFFF) & paramInt4, paramInt5, paramInt7);
    return uadd(rotate_left(paramInt1, paramInt6), paramInt2);
  }
  
  private int GG(int paramInt1, int paramInt2, int paramInt3, int paramInt4, int paramInt5, int paramInt6, int paramInt7) {
    paramInt1 = uadd(paramInt1, paramInt2 & paramInt4 | paramInt3 & (paramInt4 ^ 0xFFFFFFFF), paramInt5, paramInt7);
    return uadd(rotate_left(paramInt1, paramInt6), paramInt2);
  }
  
  private int HH(int paramInt1, int paramInt2, int paramInt3, int paramInt4, int paramInt5, int paramInt6, int paramInt7) {
    paramInt1 = uadd(paramInt1, paramInt2 ^ paramInt3 ^ paramInt4, paramInt5, paramInt7);
    return uadd(rotate_left(paramInt1, paramInt6), paramInt2);
  }
  
  private int II(int paramInt1, int paramInt2, int paramInt3, int paramInt4, int paramInt5, int paramInt6, int paramInt7) {
    paramInt1 = uadd(paramInt1, paramInt3 ^ (paramInt2 | paramInt4 ^ 0xFFFFFFFF), paramInt5, paramInt7);
    return uadd(rotate_left(paramInt1, paramInt6), paramInt2);
  }
  
  private int[] Decode(byte[] paramArrayOfbyte, int paramInt1, int paramInt2) {
    int[] arrayOfInt = new int[16];
    for (byte b2 = 0, b1 = b2; b2 < paramInt1; b1++, b2 += 4)
      arrayOfInt[b1] = paramArrayOfbyte[b2 + paramInt2] & 0xFF | (
        paramArrayOfbyte[b2 + 1 + paramInt2] & 0xFF) << 8 | (
        paramArrayOfbyte[b2 + 2 + paramInt2] & 0xFF) << 16 | (
        paramArrayOfbyte[b2 + 3 + paramInt2] & 0xFF) << 24; 
    return arrayOfInt;
  }
  
  private void Transform(MD5State paramMD5State, byte[] paramArrayOfbyte, int paramInt) {
    int i = paramMD5State.state[0];
    int j = paramMD5State.state[1];
    int k = paramMD5State.state[2];
    int m = paramMD5State.state[3];
    int[] arrayOfInt = Decode(paramArrayOfbyte, 64, paramInt);
    i = FF(i, j, k, m, arrayOfInt[0], 7, -680876936);
    m = FF(m, i, j, k, arrayOfInt[1], 12, -389564586);
    k = FF(k, m, i, j, arrayOfInt[2], 17, 606105819);
    j = FF(j, k, m, i, arrayOfInt[3], 22, -1044525330);
    i = FF(i, j, k, m, arrayOfInt[4], 7, -176418897);
    m = FF(m, i, j, k, arrayOfInt[5], 12, 1200080426);
    k = FF(k, m, i, j, arrayOfInt[6], 17, -1473231341);
    j = FF(j, k, m, i, arrayOfInt[7], 22, -45705983);
    i = FF(i, j, k, m, arrayOfInt[8], 7, 1770035416);
    m = FF(m, i, j, k, arrayOfInt[9], 12, -1958414417);
    k = FF(k, m, i, j, arrayOfInt[10], 17, -42063);
    j = FF(j, k, m, i, arrayOfInt[11], 22, -1990404162);
    i = FF(i, j, k, m, arrayOfInt[12], 7, 1804603682);
    m = FF(m, i, j, k, arrayOfInt[13], 12, -40341101);
    k = FF(k, m, i, j, arrayOfInt[14], 17, -1502002290);
    j = FF(j, k, m, i, arrayOfInt[15], 22, 1236535329);
    i = GG(i, j, k, m, arrayOfInt[1], 5, -165796510);
    m = GG(m, i, j, k, arrayOfInt[6], 9, -1069501632);
    k = GG(k, m, i, j, arrayOfInt[11], 14, 643717713);
    j = GG(j, k, m, i, arrayOfInt[0], 20, -373897302);
    i = GG(i, j, k, m, arrayOfInt[5], 5, -701558691);
    m = GG(m, i, j, k, arrayOfInt[10], 9, 38016083);
    k = GG(k, m, i, j, arrayOfInt[15], 14, -660478335);
    j = GG(j, k, m, i, arrayOfInt[4], 20, -405537848);
    i = GG(i, j, k, m, arrayOfInt[9], 5, 568446438);
    m = GG(m, i, j, k, arrayOfInt[14], 9, -1019803690);
    k = GG(k, m, i, j, arrayOfInt[3], 14, -187363961);
    j = GG(j, k, m, i, arrayOfInt[8], 20, 1163531501);
    i = GG(i, j, k, m, arrayOfInt[13], 5, -1444681467);
    m = GG(m, i, j, k, arrayOfInt[2], 9, -51403784);
    k = GG(k, m, i, j, arrayOfInt[7], 14, 1735328473);
    j = GG(j, k, m, i, arrayOfInt[12], 20, -1926607734);
    i = HH(i, j, k, m, arrayOfInt[5], 4, -378558);
    m = HH(m, i, j, k, arrayOfInt[8], 11, -2022574463);
    k = HH(k, m, i, j, arrayOfInt[11], 16, 1839030562);
    j = HH(j, k, m, i, arrayOfInt[14], 23, -35309556);
    i = HH(i, j, k, m, arrayOfInt[1], 4, -1530992060);
    m = HH(m, i, j, k, arrayOfInt[4], 11, 1272893353);
    k = HH(k, m, i, j, arrayOfInt[7], 16, -155497632);
    j = HH(j, k, m, i, arrayOfInt[10], 23, -1094730640);
    i = HH(i, j, k, m, arrayOfInt[13], 4, 681279174);
    m = HH(m, i, j, k, arrayOfInt[0], 11, -358537222);
    k = HH(k, m, i, j, arrayOfInt[3], 16, -722521979);
    j = HH(j, k, m, i, arrayOfInt[6], 23, 76029189);
    i = HH(i, j, k, m, arrayOfInt[9], 4, -640364487);
    m = HH(m, i, j, k, arrayOfInt[12], 11, -421815835);
    k = HH(k, m, i, j, arrayOfInt[15], 16, 530742520);
    j = HH(j, k, m, i, arrayOfInt[2], 23, -995338651);
    i = II(i, j, k, m, arrayOfInt[0], 6, -198630844);
    m = II(m, i, j, k, arrayOfInt[7], 10, 1126891415);
    k = II(k, m, i, j, arrayOfInt[14], 15, -1416354905);
    j = II(j, k, m, i, arrayOfInt[5], 21, -57434055);
    i = II(i, j, k, m, arrayOfInt[12], 6, 1700485571);
    m = II(m, i, j, k, arrayOfInt[3], 10, -1894986606);
    k = II(k, m, i, j, arrayOfInt[10], 15, -1051523);
    j = II(j, k, m, i, arrayOfInt[1], 21, -2054922799);
    i = II(i, j, k, m, arrayOfInt[8], 6, 1873313359);
    m = II(m, i, j, k, arrayOfInt[15], 10, -30611744);
    k = II(k, m, i, j, arrayOfInt[6], 15, -1560198380);
    j = II(j, k, m, i, arrayOfInt[13], 21, 1309151649);
    i = II(i, j, k, m, arrayOfInt[4], 6, -145523070);
    m = II(m, i, j, k, arrayOfInt[11], 10, -1120210379);
    k = II(k, m, i, j, arrayOfInt[2], 15, 718787259);
    j = II(j, k, m, i, arrayOfInt[9], 21, -343485551);
    paramMD5State.state[0] = paramMD5State.state[0] + i;
    paramMD5State.state[1] = paramMD5State.state[1] + j;
    paramMD5State.state[2] = paramMD5State.state[2] + k;
    paramMD5State.state[3] = paramMD5State.state[3] + m;
  }
  
  public void Update(MD5State paramMD5State, byte[] paramArrayOfbyte, int paramInt1, int paramInt2) {
    byte b;
    this.finals = null;
    if (paramInt2 - paramInt1 > paramArrayOfbyte.length)
      paramInt2 = paramArrayOfbyte.length - paramInt1; 
    int i = paramMD5State.count[0] >>> 3 & 0x3F;
    paramMD5State.count[0] = paramMD5State.count[0] + (paramInt2 << 3);
    if (paramMD5State.count[0] + (paramInt2 << 3) < 
      paramInt2 << 3)
      paramMD5State.count[1] = paramMD5State.count[1] + 1; 
    paramMD5State.count[1] = paramMD5State.count[1] + (paramInt2 >>> 29);
    int j = 64 - i;
    if (paramInt2 >= j) {
      for (b = 0; b < j; b++)
        paramMD5State.buffer[b + i] = paramArrayOfbyte[b + paramInt1]; 
      Transform(paramMD5State, paramMD5State.buffer, 0);
      for (b = j; b + 63 < paramInt2; b += 64)
        Transform(paramMD5State, paramArrayOfbyte, b); 
      i = 0;
    } else {
      b = 0;
    } 
    if (b < paramInt2) {
      byte b1 = b;
      for (; b < paramInt2; b++)
        paramMD5State.buffer[i + b - b1] = paramArrayOfbyte[b + paramInt1]; 
    } 
  }
  
  public void Update(byte[] paramArrayOfbyte, int paramInt1, int paramInt2) {
    Update(this.state, paramArrayOfbyte, paramInt1, paramInt2);
  }
  
  public void Update(byte[] paramArrayOfbyte, int paramInt) {
    Update(this.state, paramArrayOfbyte, 0, paramInt);
  }
  
  public void Update(byte[] paramArrayOfbyte) {
    Update(paramArrayOfbyte, 0, paramArrayOfbyte.length);
  }
  
  public void Update(byte paramByte) {
    byte[] arrayOfByte = new byte[1];
    arrayOfByte[0] = paramByte;
    Update(arrayOfByte, 1);
  }
  
  public void Update(String paramString) {
    byte[] arrayOfByte = new byte[paramString.length()];
    paramString.getBytes(0, paramString.length(), arrayOfByte, 0);
    Update(arrayOfByte, arrayOfByte.length);
  }
  
  public void Update(int paramInt) {
    Update((byte)(paramInt & 0xFF));
  }
  
  private byte[] Encode(int[] paramArrayOfint, int paramInt) {
    byte[] arrayOfByte = new byte[paramInt];
    for (byte b2 = 0, b1 = b2; b2 < paramInt; b1++, b2 += 4) {
      arrayOfByte[b2] = (byte)(paramArrayOfint[b1] & 0xFF);
      arrayOfByte[b2 + 1] = (byte)(paramArrayOfint[b1] >>> 8 & 0xFF);
      arrayOfByte[b2 + 2] = (byte)(paramArrayOfint[b1] >>> 16 & 0xFF);
      arrayOfByte[b2 + 3] = (byte)(paramArrayOfint[b1] >>> 24 & 0xFF);
    } 
    return arrayOfByte;
  }
  
  public synchronized byte[] Final() {
    if (this.finals == null) {
      MD5State mD5State = new MD5State(this.state);
      byte[] arrayOfByte = Encode(mD5State.count, 8);
      int i = mD5State.count[0] >>> 3 & 0x3F;
      int j = (i < 56) ? (56 - i) : (120 - i);
      Update(mD5State, padding, 0, j);
      Update(mD5State, arrayOfByte, 0, 8);
      this.finals = mD5State;
    } 
    return Encode(this.finals.state, 16);
  }
  
  public static String asHex(byte[] paramArrayOfbyte) {
    StringBuffer stringBuffer = new StringBuffer(paramArrayOfbyte.length * 2);
    for (byte b = 0; b < paramArrayOfbyte.length; b++) {
      if ((paramArrayOfbyte[b] & 0xFF) < 16)
        stringBuffer.append("0"); 
      stringBuffer.append(Long.toString((paramArrayOfbyte[b] & 0xFF), 16));
    } 
    return stringBuffer.toString();
  }
  
  public String asHex() {
    return asHex(Final());
  }
}

MD5State.class类文件

class MD5State {
  int[] state;
  
  int[] count;
  
  byte[] buffer;
  
  public MD5State() {
    this.buffer = new byte[64];
    this.count = new int[2];
    this.state = new int[4];
    this.state[0] = 1732584193;
    this.state[1] = -271733879;
    this.state[2] = -1732584194;
    this.state[3] = 271733878;
    this.count[1] = 0;
    this.count[0] = 0;
  }
  
  public MD5State(MD5State paramMD5State) {
    this();
    byte b;
    for (b = 0; b < this.buffer.length; b++)
      this.buffer[b] = paramMD5State.buffer[b]; 
    for (b = 0; b < this.state.length; b++)
      this.state[b] = paramMD5State.state[b]; 
    for (b = 0; b < this.count.length; b++)
      this.count[b] = paramMD5State.count[b]; 
  }
}

你期待的结果是什么?实际看到的错误信息又是什么?

最好是一个PHP函数 或者JS函数
传入一个字符串aaa 自动返回bbb加密后的md5回来

阅读 1.7k
1 个回答

如果单纯的md5的话,没必要这么多文件,几句话就搞定了。

function encrypt( array $data, $salt){
    return md5( implode($data) . $salt);
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题