第三方给的公钥是这样的
30819d300d06092a864886f70d010101050003818b0030818702818100a25a8be616f471fe6f2d26771aa89222f7ff01172905c747a6948f2d495e1d83a1b9d3e2aadb44a3ca97f96f3efce273ddb505314132586c979c209a473c17f0f8c1e66d745c9ce5b81084ec7d38a5f03a65b9a2cf17cc28be81373f31aab9dbbdc553959ea655565279702ae4774aa74db5717f9ac350a4484e41dfb3327a01020111如何初始化  RSAParameters,
PublicKey.Exponent = new byte[]{1,0,1}; 看到网友说大部分都是这样的---------------------
我写了如下的程序,请大家给点思路 private void Page_Load(object sender, System.EventArgs e)
{ string publicKey= "30819d300d06092a864886f70d010101050003818b0030818702818100a25a8be616f471fe6f2d26771aa89222f7ff01172905c747a6948f2d495e1d83a1b9d3e2aadb44a3ca97f96f3efce273ddb505314132586c979c209a473c17f0f8c1e66d745c9ce5b81084ec7d38a5f03a65b9a2cf17cc28be81373f31aab9dbbdc553959ea655565279702ae4774aa74db5717f9ac350a4484e41dfb3327a01020111";
byte[] pk = hexStringToByte(publicKey); RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();

//设置公钥
RSAParameters PublicKey = new RSAParameters();
PublicKey.Exponent = new byte[]{1,0,1};
//指定私钥Modulus的值
PublicKey.Modulus = pk;
RSA.ImportParameters(PublicKey); RSAPKCS1SignatureDeformatter RSADeformatter = new RSAPKCS1SignatureDeformatter(RSA);
//指定解密的时候HASH算法为MD5
RSADeformatter.SetHashAlgorithm("MD5"); //第三方给的签名数据
string signedData = "74af028d45c23ce2c8561860744fdd7c90b0361c4236d8a70739d7d8f04aac60f602e1c6ec231877bdba39d82604047414e6df3684c9942784ac4690c673424f03cf84b01f8db3f6112ab96f663a01df84f0a12311ce6a1e42f09492bd1c5e5c7d6827be46c6563268618043ec8b164617ded601ea8b472d77d2711ef46050b2";
byte[] sd = hexStringToByte(signedData); string orgData = "POSID=000000000&BRANCHID=310000000&ORDERID=45370&PAYMENT=0.01&CURCODE=01&REMARK1=&REMARK2=&SUCCESS=Y"; string md5 = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(orgData,"md5").ToLower();
byte[] hashMD5 = hexStringToByte(md5); bool ok = RSADeformatter.VerifySignature(hashMD5,sd); Response.Write(ok); } private byte[] hexStringToByte(string hex)
{
if ( hex.Length % 2 != 0)
throw new Exception("!!!"); byte[] bb = new byte[hex.Length/2]; for(int i=0; i< hex.Length/2; i++)
{
string x = hex[i*2].ToString() + hex[i*2+1].ToString();
bb[i] = Convert.ToByte(x, 16);
}
return bb; }
请帮忙啦。

解决方案 »

  1.   

    针对你的公钥,你主要要去跟提供方要他们的编码方式另外,公钥的编码不会象你的这个函数写的那么复杂
    如果你要验证签名,需要有签名数据和被签名的数据
    比如//第三方给的签名数据 string signedData 是签名数据的话,那你就少给了被签名的数据,在网络上,你只要给出被签名的数据的Hash码,我们就可以帮你验证签名了
      

  2.   

    cnming:被签名的数据 有的呀,string orgData 
    = "POSID=000000000&BRANCHID=310000000&ORDERID=45370&PAYMENT=0.01&CURCODE=01&REMARK1=&REMARK2=&SUCCESS=Y";
      

  3.   

    cnming,你说的 “他们的编码方式”。也是困惑我的一个问题,公钥编码有哪些标准的格式?
    1 base64的xml的,就是。net里面生成的那种
    2 还有就是我遇到的这种格式。谁能给点信息,谢谢
      

  4.   

    看到一个帖子说,
    http://lists.ximian.com/archives/public/mono-list/2003-September/015743.html> keyInfo.Exponent = {1, 0, 1}
    Exponent may not always be 65537. 那我是不是要问加密方要Exponent呢?
      

  5.   

    我使用byte[] pk = Convert.FromBase64String(publicKey);获取了240长度的数据使用你的byte[] pk = hexStringToByte(publicKey);获取了240长度的数据
    所以,编码是很重要的另外,hash码要给出来,否则我们无法校验
      

  6.   

    我有希望了也,那个publicstring 应该是 ASN.1 encoded。可以还原的,在看,谁有代码给我就更好了。呵呵
      

  7.   

    string publicKey= "30819d300d06092a864886f70d010101050003818b0030818702818100a25a8be616f471fe6f2d26771aa89222f7ff01172905c747a6948f2d495e1d83a1b9d3e2aadb44a3ca97f96f3efce273ddb505314132586c979c209a473c17f0f8c1e66d745c9ce5b81084ec7d38a5f03a65b9a2cf17cc28be81373f31aab9dbbdc553959ea655565279702ae4774aa74db5717f9ac350a4484e41dfb3327a01020111";byte[] PlainTextBArray = hexStringToByte(publicKey);System.Security.Cryptography.RSACryptoServiceProvider rsa=new System.Security.Cryptography.RSACryptoServiceProvider();System.Security.Cryptography.RSAParameters PublicKey = new System.Security.Cryptography.RSAParameters();
    PublicKey.Exponent = new byte[]{1,0,1};
    PublicKey.Modulus = PlainTextBArray;
    rsa.ImportParameters(PublicKey);这样的代码都没有出错的
      

  8.   

    cnming 那三个要素我都给了呀public key
    org data
    signed data非常感谢。
      

  9.   

    http://spaces.msn.com/members/staceyw/Blog/cns!1pnsZpX0fPvDxLKC6rAAhLsQ!302.entry
    我从一个网站上读出cer,然后和ie看到的pub比较,正在看如何把ie里面那个pub string(和我得到的差不多)专成Modulus
      

  10.   

    一边研究,一边等他们给我Modulus和Exponent :(
      

  11.   

    你的
    public key
    org data
    signed data是从哪里获得的?我在那个链接怎么没有见到?
      

  12.   

    以我的这个做法,结果都是失败的 string publicKey= "30819d300d06092a864886f70d010101050003818b0030818702818100a25a8be616f471fe6f2d26771aa89222f7ff01172905c747a6948f2d495e1d83a1b9d3e2aadb44a3ca97f96f3efce273ddb505314132586c979c209a473c17f0f8c1e66d745c9ce5b81084ec7d38a5f03a65b9a2cf17cc28be81373f31aab9dbbdc553959ea655565279702ae4774aa74db5717f9ac350a4484e41dfb3327a01020111";
    string signedData = "74af028d45c23ce2c8561860744fdd7c90b0361c4236d8a70739d7d8f04aac60f602e1c6ec231877bdba39d82604047414e6df3684c9942784ac4690c673424f03cf84b01f8db3f6112ab96f663a01df84f0a12311ce6a1e42f09492bd1c5e5c7d6827be46c6563268618043ec8b164617ded601ea8b472d77d2711ef46050b2";
    string orgData = "POSID=000000000&BRANCHID=310000000&ORDERID=45370&PAYMENT=0.01&CURCODE=01&REMARK1=&REMARK2=&SUCCESS=Y"; byte[] bPublicKey = hexStringToByte(publicKey);
    byte[] bSignedData = hexStringToByte(signedData);
    //byte[] bOrgData = hexStringToByte(orgData);
    System.Security.Cryptography.RSACryptoServiceProvider rsa=new System.Security.Cryptography.RSACryptoServiceProvider(); System.Security.Cryptography.RSAParameters PublicKey = new System.Security.Cryptography.RSAParameters();
    PublicKey.Exponent = new byte[]{1,0,1};
    PublicKey.Modulus = bPublicKey;
    rsa.ImportParameters(PublicKey); byte[] bHash = null; byte[] Buffer;
    System.Security.Cryptography.HashAlgorithm MD5 = System.Security.Cryptography.HashAlgorithm.Create("MD5");
    Buffer = System.Text.Encoding.GetEncoding("GB2312").GetBytes(orgData); //这里,编码可能有问题,因为其它的都是使用hexStringToByte方式编码的
    bHash = MD5.ComputeHash(Buffer); System.Security.Cryptography.RSAPKCS1SignatureDeformatter RSADeformatter = new System.Security.Cryptography.RSAPKCS1SignatureDeformatter(rsa);
    //指定解密的时候HASH算法为MD5
    RSADeformatter.SetHashAlgorithm("MD5"); if(RSADeformatter.VerifySignature(bHash,bSignedData))
    {
    MessageBox.Show("Success");
    }
    else
    {
    MessageBox.Show("Fail");
    }
      

  13.   

    感谢 cnming(cnming),
    我现在等加密方给我Modulus和Exponent,然后再试试看。
      

  14.   


    他们用的是java。 
    这个是一个java RSA 和 。net RSA的区别
    下面的文章有说
    http://www.jensign.com/JavaScience/dotnet/JKeyNet/
    先结贴了,
    如果有心得再贴出来。