C#验签验证java生成的data、sign、publicKey

本文介绍如何使用C#进行签名验证,针对Java生成的data、sign和publicKey进行验签。通过解析并处理JSON数据,实现了参数的排序与字符串拼接,最终完成RSA签名验证。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 第一次用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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值