在java中生成CHAP响应

时间:2017-06-29 09:50:49

标签: javascript java php

目前我正在尝试在java中生成一个chap响应。它适用于PHP ...但我们的后端需要在java中完成。

var challenge = "cda9af6faa83d0883d694fa58d2e88wh";
var password = "password123";
var hexchal = challenge.packHex();
var newchal = hexchal;
var response = md5('\0' + password + newchal)

我设法找到了一些javascript代码,但响应却被一些字符拒绝了。

function getPapPassword(){
           //function to add value padding to a string of x length default : null padding
           String.prototype.pad = function (length, padding) {
               var padding = typeof padding === 'string' && padding.length > 0 ? padding[0] : '\x00'
                       , length = isNaN(length) ? 0 : ~~length;
               return this.length < length ? this + Array(length - this.length + 1).join(padding) : this;
           };

           //function to convert hex to ascii characters
           String.prototype.packHex = function () {
               var source = this.length % 2 ? this + '0' : this
                       , result = '';

               for (var i = 0; i < source.length; i = i + 2) {
                   result += String.fromCharCode(parseInt(source.substr(i, 2), 16));
               }
               return result;
           };

           var challenge = "cda9af6faa83d0883d694fa58d2e88wh";
           var password = "password123";
           var hexchal = challenge.packHex();
           var newchal = hexchal;
           var response = md5('\0' + password + newchal);
           }
           return response;
           }

有人能指出我正确的方向或协助吗? 感谢

好的,我所做的就是尝试在java中将十六进制转换为ascii ...这是我的代码。

public class App{

  public String convertHexToString(String hex){

      StringBuilder sb = new StringBuilder();
      StringBuilder temp = new StringBuilder();

      //49204c6f7665204a617661 split into two characters 49, 20, 4c...
      for( int i=0; i<hex.length()-1; i+=2 ){

          //grab the hex in pairs
          String output = hex.substring(i, (i + 2));
          //convert hex to decimal
          int decimal = Integer.parseInt(output, 16);
          //convert the decimal to character
          sb.append((char)decimal);

          temp.append(decimal);
      }
      System.out.println("Decimal : " + temp.toString());

      return sb.toString();
  }

  public static void main(String[] args) {

      App app = new App();
      System.out.println("ASCII : " + app.convertHexToString("9f9585f4e88305fde280c762925f37af"));
  }
}

十六进制挑战输出的php是:Ÿ•...ôèƒýâ€Çb'_7¯

十六进制挑战的java输出是:ôèýâÇb_7¯

所以我发现javascript和java完全一样。在PHP中正在发生一些改变它的事情。当我用那个chap响应连接到UAM时,php版本工作得很好。

0 个答案:

没有答案