银行接口的加密是使用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进行跳转,然后总是报错误:
自己已经解决
我遇到了同样的问题,请问是怎么解决的,谢谢。微信wjc25257758
@warren小吴: 这个基本就是签名的问题呢