/**
 * 产生摘要
 * @param aValue
 * @param aKey
 * @return
 */
public static String hmacSign(String aValue, String aKey) {
byte k_ipad[] = new byte[64];
byte k_opad[] = new byte[64];
byte keyb[];
byte value[];
try {
keyb = aKey.getBytes(encodingCharset);
value = aValue.getBytes(encodingCharset);
} catch (UnsupportedEncodingException e) {
keyb = aKey.getBytes();
value = aValue.getBytes();
} Arrays.fill(k_ipad, keyb.length, 64, (byte) 54);
Arrays.fill(k_opad, keyb.length, 64, (byte) 92);
for (int i = 0; i < keyb.length; i++) {
k_ipad[i] = (byte) (keyb[i] ^ 0x36);
k_opad[i] = (byte) (keyb[i] ^ 0x5c);
} MessageDigest md = null;
try {
md = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) { return null;
}
md.update(k_ipad);
md.update(value);
byte dg[] = md.digest();
md.reset();
md.update(k_opad);
md.update(dg, 0, 16);
dg = md.digest();
return toHex(dg);
} public static String toHex(byte input[]) {
if (input == null)
return null;
StringBuffer output = new StringBuffer(input.length * 2);
for (int i = 0; i < input.length; i++) {
int current = input[i] & 0xff;
if (current < 16)
output.append("0");
output.append(Integer.toString(current, 16));
} return output.toString();
}

解决方案 »

  1.   

    谢谢楼上,不知道怎么用的。我这里有一个合作人给的JAVA开发包,不会用,只好改为JAVA。有些地方读不懂。
      

  2.   

    Java与.NET   的Web   Services相互调用     
          
      一:简介     
        
      本文介绍了Java与.NET开发的Web   Services相互调用的技术。本文包括两个部分,第一部分介绍了如何用.NET做客户端调用Java写的Web   Services,第二部分介绍了如何用Java做客户端调用.NET开发的Web   Services。     
        
        
        
      二:项目需要的工具     
        
      Windows2000   Server(IIS)     
        
      Jbuilder9.0(   含有Tomcat   ,   axis)     
        
      JDK1.4+Java   Web   Services   Develop     
        
      VS.Net   2003     
        
      备注:如果没有JBuilder的话就需要自己下载安装Tomcat4.1   以及   Axis的开发包,并做相应配置。由于网上有很多这样的文章,这里就不一一说明了。     
        
        
        
      三:用.NET做客户端调用Java写的Web   Services     
        
      1.   生成一个Java   Web   Services     
        
      使用JBuilder   生成一个Web   Services是非常简单的,我完全是按照它的帮助做的,只要一步步做     
        
      下去就可以了。具体路径是Developing   Web   Services   ->   tutorials   :   Web   Services(Axis)   ->   Creating   a   simple   Web   Services     
        
      2.   发布这个Web   Services并得到它的WSDL     
        
      Web   Services生成好之后,F9运行这个项目。然后,单击View浏览现有的Web   服务,单击Bean1的WSDL连接,我们可以在浏览器中察看它的WSDL描述。在浏览器地址栏复制WSDL地址,为下一步使用.NET开发客户端调用程序做准备。     
        
      3.   用VS.NET生成一个.NET的客户端     
        
      新建一个项目(WinForm,ASP.net都可以),我在这里使用的是一个Asp.Net项目。在起始页面上放置一个文本输入框用来显示调用Web   Services的结果,放置一个按钮,用来单击调用Web   Services。然后,选择添加Web   引用,在WSDL一栏中把刚才得到的WSDL地址复制过来,Web   引用的名称输入JavaService,单击添加引用按钮就可以了。此时,我们可以在VS.net   的Solution   Explore中看到这个Web   引用。     
        
      在按钮的单击事件中输入下列代码:     
        
      JavaService.Bean1   bean   =   new   JavaService.Bean1();     
        
      TextBox1.Text   =   bean.getSample.ToString   ();     
        
        
        
      这样,一个.NET客户端就完成了,测试一下,工作正常,OK.     
        
        
        
      四:用Java做客户端调用.NET写的   Web   Services     
        
      有了上面的成功,使我以为用Java做客户端调用也是一件十分容易的事情,可实际情况却耗费了我两天时间才得以实现。     
        
      1.   用VS.NET新建一个Asp   Web   Services工程,添加一个web   服务,命名为SumService.asmx。新增一个web   method,代码如下:     
        
      [WebMethod]     
        
      public   int   IntAdd(int   a,int   b)     
        
      {     
        
      return   a+b   ;     
        
      }     
        
      然后运行它,并利用IE进行测试成功。     
        
      2.   打开jbuilder9.0,新建一个项目,添加一个java   class   ,命名为TestNetService,输入下列代码:     
        
      package   MyWebServiceJavaClient;     
        
      import   java.util.Date;     
        
      import   java.text.DateFormat;     
        
      import   java.util.Date;     
        
      import   java.text.DateFormat;     
        
      import   org.apache.axis.client.Call;     
        
      import   org.apache.axis.client.Service;     
        
      import   javax.xml.namespace.QName;     
        
      import   java.lang.Integer;     
        
      import   javax.xml.rpc.ParameterMode;     
        
        
        
      /**     
        
      *   <p>Title:   </p>     
        
      *   <p>Description:   </p>     
        
      *   <p>Copyright:   Copyright   (c)   2004</p>     
        
      *   <p>Company:   </p>     
        
      *   @author   not   attributable     
        
      *   @version   1.0     
        
      */     
        
        
        
      public   class   TestNetService   {     
        
      public   TestNetService()   {     
        
      }     
        
        
        
      public   static   void   main(String[]   args)   {     
        
      try   {     
        
      Integer   i   =   new   Integer(1);     
        
      Integer   j   =   new   Integer(2);     
        
      String   endpoint="http://localhost/MyServices/WebServiceTest/SumService.asmx";     
        
        
        
      Service   service   =   new   Service();     
        
      Call   call   =   (Call)service.createCall();     
        
        
        
      call.setTargetEndpointAddress(new   java.net.URL(endpoint));     
        
      call.setOperationName(new   QName("http://www.my.com/SU","IntAdd"));     
        
        
        
      call.addParameter("a",org.apache.axis.encoding.XMLType.XSD_DATE,javax.xml.rpc.ParameterMode.IN);     
        
      call.addParameter("b",org.apache.axis.encoding.XMLType.XSD_DATE,javax.xml.rpc.ParameterMode.IN);     
        
      call.setReturnType(org.apache.axis.encoding.XMLType.XSD_INT);     
        
      call.setUseSOAPAction(true);     
        
      call.setSOAPActionURI("http://www.my.com/Rpc");     
        
        
        
        
        
      Integer   k   =   (Integer)call.invoke(new   Object[]{i,j});     
        
        
        
      System.out.println(   "result   is   "   +   k.toString()   +   ".");     
        
      }     
        
      catch   (Exception   e)   {System.err.println(e.toString());}     
        
      }     
        
        
        
      }     
        
        
        
      运行上面的java客户端程序,你会发现系统会抛出一个SoapAction异常。奇怪,怎么会错误呢?想到了我开发的asp   web   services没有指定SoapAction,于是在SumService.asmx中的   [web   method]   的上一行添加下列代码:     
        
      [SoapRpcMethod(Action="http://www.my.com/Rpc",RequestNamespace="http://www.my.com/SU",ResponseNamespace="http://www.my.com/SU")]     
        
      重新编译运行asp   web   services后,在执行java程序,这时会发现输出了正确的结果3。     
        
      这样,用java调用.net生成的web   services也算初步完成了,好像也不是很复杂是吧。其实,在实际工作中,我一开始并没有在网上找到一个很好的代码例子,全部是依靠jbuilder   和   axis的帮助完成的,   可总是提示什么找不到相应的SoapAction。而实际上,我在运行了asp   web   services后是可以在IE浏览器中看到那个SoapAction的。我试验过将默认的SoapAction拷贝到java代码中,但是java客户端依然抛出同样的异常,这要我也很迷惑。     
        
        
        
      五:总结     
        
      经过两天的试验,终于从技术上明确了通过Web   Services实现.net   与   java的互通是可能的。当然有几点是需要注意的:     
        
      1.   在提供Web   Services的时候,尽量使用xml   schema中支持的变量类型做参数。如果使用.net   中的dataset这种类型,对于java来说解析起来将是一个灾难,当然,理论上是可以解析的。但是从效率角度来说,在Web   Services与客户端交换信息的过程中,始终有一个序列化和反序列化的问题。如果使用dataset这种类型,系统还需要对它进行序列化操作,这将是一个很耗费资源的过程。而使用string类型将简单很多。     
        
      2.   如果使用了soap   header等扩展功能,例如使用了微软提供的WSE技术,它们之间的相互通信需要作特殊处理。
      

  3.   

    谢谢楼上,有时间一定好好看看,但我现在这里没有webService啊,只有这几个java文件,而且我的环境里也没有tomcat和JDK......能解下燃眉之急么?上面的代码是C#中MD5加密的。encodingCharset=“UTF-8”,研究了一个多小时了...想今天完工..
      

  4.   


     public static string EncryptBy32MD5(string strProclaimed)
            {
                MD5 md5 = MD5.Create();
                string strCryptograph = "";
                byte[] bytCode = md5.ComputeHash(Encoding.UTF8.GetBytes(strProclaimed));
                for (int i = 0; i < bytCode.Length; i++)
                {
                    // 将得到的字符串使用十六进制类型格式。格式后的字符是小写的字母,如果使用大写(X)则格式后的字符是大写字符 
                    strCryptograph = strCryptograph + bytCode[i].ToString("x");
                }
                return strCryptograph;
            }   public static string EncryptBy16MD5(string strProclaimed)
            {
                MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
                string strCryptograph = BitConverter.ToString(md5.ComputeHash(UTF8Encoding.Default.GetBytes(strProclaimed)), 4, 8);
                return strCryptograph;
            }希望有用
      

  5.   

    谢谢阿呆...
    我知道MD5的32位加密可以是
    System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(myString, "md5").ToLower();
    而且我也知道这段JAVA代码是返回了一个32位的密文,可是我不知道 aValue和 aKey组成了一个什么样的字符串...
    恩恩,我再研究研究..
      

  6.   

    public static String hmacSign(String aValue, String aKey) { 
    byte k_ipad[] = new byte[64]; 
    byte k_opad[] = new byte[64]; 
    byte keyb[]; 
    byte value[]; 
    try { 
    keyb = aKey.getBytes(encodingCharset); 
    value = aValue.getBytes(encodingCharset); 
    } catch (UnsupportedEncodingException e) { 
    keyb = aKey.getBytes(); 
    value = aValue.getBytes(); 

    //上边获取byte数组Arrays.fill(k_ipad, keyb.length, 64, (byte) 54); 
    Arrays.fill(k_opad, keyb.length, 64, (byte) 92); 
    //填充到64位的数组中(分别)
    for (int i = 0; i < keyb.length; i++) { 
    k_ipad[i] = (byte) (keyb[i] ^ 0x36); 
    k_opad[i] = (byte) (keyb[i] ^ 0x5c); 

    //填充数据,用key自动补位valueMessageDigest md = null; 
    try { 
    md = MessageDigest.getInstance("MD5"); 
    } catch (NoSuchAlgorithmException e) { return null; 

    md.update(k_ipad); 
    md.update(value); 
    byte dg[] = md.digest(); 
    md.reset(); 
    md.update(k_opad); 
    md.update(dg, 0, 16); 
    dg = md.digest(); 
    return toHex(dg); 
      

  7.   

    前面部分我改为了 
          byte[] k_ipad = new byte[64]; 
    byte[] k_opad = new byte[64]; 
            byte[] keyb; 
            byte[] value; 
    try 
            { 
                keyb = Encoding.GetEncoding("UTF-8").GetBytes(aValue); 
                value = Encoding.GetEncoding("UTF-8").GetBytes(aKey); 

            catch 
            { 
                keyb = Encoding.Default.GetBytes(aValue); 
                value = Encoding.Default.GetBytes(aKey); 

            for (int i = keyb.Length; i < 64; i++) 
            { 
                k_ipad[i] = (byte)54; 
                k_opad[i] = (byte)92; 
            } 
            for (int i = 0; i < keyb.Length; i++) 
            { 
                k_ipad[i] = (byte)(keyb[i] ^ 0x36); 
                k_opad[i] = (byte)(keyb[i] ^ 0x5c);         } 
    不知道对不对,但后面部分真不知道什么意思..主要是不知道JAVA版的什么意思
      

  8.   

    public static string hmacSign(string aValue, string aKey)
            {
                // 第一次HASH时补白的数组
                byte[] k_ipad = new byte[64];
                // 第一次HASH时补白的数组
                byte[] k_opad = new byte[64];
                // aValue经过编码转换后的字节数组
                byte[] keyb = null;
                // aKey经过编码转换后的字节数组
                byte[] value = null;            if (!string.IsNullOrEmpty(aValue) && !string.IsNullOrEmpty(aKey))
                {
                    //执行编码转换,encodingCharset为某个Encoding,如UTF8
                    encodingCharset.GetBytes(aKey);
                    value = encodingCharset.GetBytes(aValue);
                }
                else
                {
                    keyb = new byte[0];
                    value = new byte[0];
                }
                // keyb不足64的时候,初始化k_ipad和k_opad补白的字节
                for (int i = keyb.Length; i < 64; i++)
                {
                    k_ipad[i] = 54;
                    k_opad[i] = 92;
                }
                
                // 这个应该是消除连续相同字节吧
                for (int i = 0; i < keyb.Length; i++)
                {
                    k_ipad[i] = (byte)(keyb[i] ^ 0x36);
                    k_opad[i] = (byte)(keyb[i] ^ 0x5c);
                }            MD5 md = MD5.Create();
                // 把一次需要HASH的数据存到一个数组中
                byte[] input = new byte[k_ipad.Length + value.Length];
                Buffer.BlockCopy(k_ipad, 0, input, 0, k_ipad.Length);
                Buffer.BlockCopy(value, 0, input, k_ipad.Length, value.Length);
                byte[] dg = md.ComputeHash(input);            // 把一次需要HASH的数据存到一个数组中
                input = new byte[k_ipad.Length + 16];
                Buffer.BlockCopy(k_opad, 0, input, 0, k_opad.Length);
                Buffer.BlockCopy(dg, 0, input, k_opad.Length, 16);
                dg = md.ComputeHash(input);            return toHex(dg);
            }        public static string toHex(byte[] input)
            {
                if (input == null)
                    return null;
                StringBuilder output = new StringBuilder(input.Length * 2);
                for (int i = 0; i < input.Length; i++)
                {
                    int current = input[i] & 0xff;
                    if (current < 16) output.Append("0");
                    output.Append(current.ToString("X"));
                }            return output.ToString();
            }
      

  9.   

    恩。谢谢楼上,不过加密出来的字符串不一样,可能是string格式不对吧,一会就结贴.