using System;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Text;
using System.Xml;
/// <summary>
/// 将参数组成代签名的字符串
/// </summary>
/// <param name="parameters"></param>
/// <returns></returns>
public static string GetSignContent(IDictionary<string, string> parameters)
{
// 第一步:把字典按Key的字母顺序排序
IDictionary<string, string> sortedParams = new SortedDictionary<string, string>(parameters);
IEnumerator<KeyValuePair<string, string>> dem = sortedParams.GetEnumerator();
// 第二步:把所有参数名和参数值串在一起
StringBuilder query = new StringBuilder("");
while (dem.MoveNext())
{
string key = dem.Current.Key;
string value = dem.Current.Value;
if (!string.IsNullOrEmpty(key) && !string.IsNullOrEmpty(value))
{
query.Append(key).Append("=").Append(value).Append("&");
}
}
//第三步:判断参数是否有值
string content = null;
if (query != null)
{
content = query.ToString().Substring(0, query.Length - 1);
}
return content;<

这篇博客详细介绍了如何在C#中使用RSA算法进行签名和验签操作。包括将参数组合成待签名字符串、私钥加密、公钥验签的完整流程,并提供了相应的代码示例。此外,还包含了重写FromXmlString方法以加载RSA密钥。
最低0.47元/天 解锁文章
2802

被折叠的 条评论
为什么被折叠?



