第一次用C#验签 验证java生成的data、sign、publicKey。测试过了可以用。
JObject jo = (JObject)JsonConvert.DeserializeObject(“java提供的json字符串”);
string sing = jo["sign"].ToString();//获取签名
var reqData = ((JContainer)(jo["reqData"]));//获取想要的数据
string str = info(reqData);//a=1&b=2&c=3//拼接字符串
var result = VerifyHash(publicKey, str, sing);
if (result)
{
//验证成功
}
///用来做字母排序使用的类
public class FamilyModel
{
public string Name { set; get; }
}
/// <summary>
/// 拼接参数
/// </summary>
/// <param name="reqData"></param>
/// <returns></returns>
public string info(JContainer reqData)
{
string result = "";
List<FamilyModel> listFamily = new List<FamilyModel>();
foreach (var i in reqData)
{
var name = ((JProperty)i).Name;
listFamily.Add(new FamilyModel { Name = name });
}
listFamily.Sort((x, y) => string.Compare(x.Name, y.Name));
var newList = listFamily.OrderBy(x => x.Name).ToList();
for (int i = 0; i < newList.Count; i++)
{
result += newList[i].Name + "=" + reqData[newList[i].Name] + "&";
}
result = result.Substring(0, result.Length - 1);
return result;
}
#region 验签(rsa)
public static bool VerifyHash(string publicKey, string data, string sign)
{
byte[] signedData = Encoding.UTF8.GetBytes(data);
byte[] signature = Convert.FromBase64String(sign);
RSACryptoServiceProvider rsaCSP = CreateRsaProviderFromPublicKey(publicKey);
SHA1Managed hash = new SHA1Managed();
byte[] hashedData;
rsaCSP.VerifyData(signedData, "SHA1", signature);//"SHA1":用来创建数据哈希值的哈希算法的名称
hashedData = hash.ComputeHash(signedData);
return rsaCSP.VerifyHash(hashedData, CryptoConfig.MapNameToOID("SHA1"), signature);
}
public static bool CompareBytearrays(byte[] a, byte[] b)
{
if (a.Length != b.Length)
return false;
int i = 0;
foreach (byte c in a)
{
if (c != b[i])
return false;
i++;
}
return true;
}
public static RSACryptoServiceProvider CreateRsaProviderFromPublicKey(string publicKeyString)
{
byte[] SeqOID = { 0x30, 0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x01, 0x05, 0x00 };
byte[] x509key;
byte[] seq = new byte[15];
int x509size;
x509key = Convert.FromBase64String(publicKeyString);
x509size = x509key.Length;
using (MemoryStream mem = new MemoryStream(x509key))
{
using (BinaryReader binr = new BinaryReader(mem))
{
byte bt = 0;
ushort twobytes = 0;
twobytes = binr.ReadUInt16();
if (twobytes == 0x8130)
binr.ReadByte();
else if (twobytes == 0x8230)
binr.ReadInt16();
else
return null;
seq = binr.ReadBytes(15);
if (!CompareBytearrays(seq, SeqOID))
return null;
twobytes = binr.ReadUInt16();
if (twobytes == 0x8103)
binr.ReadByte();
else if (twobytes == 0x8203)
binr.ReadInt16();
else
return null;
bt = binr.ReadByte();
if (bt != 0x00)
return null;
twobytes = binr.ReadUInt16();
if (twobytes == 0x8130)
binr.ReadByte();
else if (twobytes == 0x8230)
binr.ReadInt16();
else
return null;
twobytes = binr.ReadUInt16();
byte lowbyte = 0x00;
byte highbyte = 0x00;
if (twobytes == 0x8102)
lowbyte = binr.ReadByte();
else if (twobytes == 0x8202)
{
highbyte = binr.ReadByte();
lowbyte = binr.ReadByte();
}
else
return null;
byte[] modint = { lowbyte, highbyte, 0x00, 0x00 };
int modsize = BitConverter.ToInt32(modint, 0);
int firstbyte = binr.PeekChar();
if (firstbyte == 0x00)
{
binr.ReadByte();
modsize -= 1;
}
byte[] modulus = binr.ReadBytes(modsize);
if (binr.ReadByte() != 0x02)
return null;
int expbytes = (int)binr.ReadByte();
byte[] exponent = binr.ReadBytes(expbytes);
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
RSAParameters RSAKeyInfo = new RSAParameters();
RSAKeyInfo.Modulus = modulus;
RSAKeyInfo.Exponent = exponent;
RSA.ImportParameters(RSAKeyInfo);
return RSA;
}
}
}
#endregion