首页新闻找找看学习计划

RSA加密后在进行签名,总是报(签名错误:Signature length not correct: got 249 but was expecting 256)

0
悬赏园豆:10 [已关闭问题] 关闭于 2017-05-26 14:26

银行接口的加密是使用2048位的,所以我的加密代码是:

public static string GetEncryptedMsg(string xmlPublicKey, string xml)
{
byte[] encryptedData;
using (System.Security.Cryptography.RSACryptoServiceProvider rsa = new RSACryptoServiceProvider())
{
rsa.FromXmlString(xmlPublicKey);
var plainData = Encoding.UTF8.GetBytes(xml);
using (var plaiStream = new MemoryStream(plainData))
{
using (var crypStream = new MemoryStream())
{
var offSet = 0;
var inputLen = plainData.Length;
for (var i = 0; inputLen - offSet > 0; offSet = i * 244)
{
if (inputLen - offSet > 244)
{
var buffer = new Byte[244];
plaiStream.Read(buffer, 0, 244);
var cryptograph = rsa.Encrypt(buffer, false);
crypStream.Write(cryptograph, 0, cryptograph.Length);
}
else
{
var buffer = new Byte[inputLen - offSet];
plaiStream.Read(buffer, 0, inputLen - offSet);
var cryptograph = rsa.Encrypt(buffer, false);
crypStream.Write(cryptograph, 0, cryptograph.Length);
}
++i;
}
crypStream.Position = 0;
encryptedData = crypStream.ToArray();
}
}
}
return BitConverter.ToString(encryptedData).Replace("-", string.Empty);
}

然后将返回的加密信息,拼接到参数里,然后去进行签名,签名的代码如下:

public static String signData(string xml,string pfxpath,string pfxpassword)
{
X509Certificate2 objx5092 = new X509Certificate2(pfxpath, pfxpassword, X509KeyStorageFlags.MachineKeySet); //本地存储,服务器测试(windows server2008)要使用这个

RSACryptoServiceProvider rsa = objx5092.PrivateKey as RSACryptoServiceProvider;
byte[] data = Encoding.GetEncoding("UTF-8").GetBytes(xml);
byte[] hashValue = rsa.SignData(data, "MD5");
string msg = Convert.ToBase64String(hashValue); //Convert.ToBase64String(hashValue); //ByteToString(hashValue);
return msg;
}

将返回的签名进行httppost进行跳转,然后总是报错误:

 

EApple的主页 EApple | 初学一级 | 园豆:139
提问于:2017-05-13 14:53
< >
分享
所有回答(1)
0

自己已经解决

EApple | 园豆:139 (初学一级) | 2017-05-26 14:25

我遇到了同样的问题,请问是怎么解决的,谢谢。微信wjc25257758 

支持(0) 反对(0) warren小吴 | 园豆:200 (初学一级) | 2017-09-04 14:22

@warren小吴: 这个基本就是签名的问题呢

支持(0) 反对(0) EApple | 园豆:139 (初学一级) | 2017-09-20 14:49
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册