Esempio n. 1
0
        public static bool VerifyData(string publicKey, string data, string signData)
        {
            var md5 = RSAHelper.GenerateMD5Byte(data);

            using (var rsa = RSA.Create())
            {
                rsa.ImportRSAPublicKey(Convert.FromBase64String(publicKey), out int bytesRead);
                return(rsa.VerifyData(md5, Convert.FromBase64String(signData), HashAlgorithmName.SHA1, RSASignaturePadding.Pkcs1));
            }
        }
Esempio n. 2
0
        public static string SignData(string privateKey, string data)
        {
            var md5 = RSAHelper.GenerateMD5Byte(data);

            using (var rsa = RSA.Create())
            {
                rsa.ImportPkcs8PrivateKey(Convert.FromBase64String(privateKey), out int bytesRead);
                var rs = rsa.SignData(md5, HashAlgorithmName.SHA1, RSASignaturePadding.Pkcs1);
                return(Convert.ToBase64String(rs));
            }
        }
Esempio n. 3
0
        /// <summary>
        /// 签名校验
        /// </summary>
        /// <param name="publicKey"></param>
        /// <param name="signData"></param>
        /// <returns></returns>
        public static bool VerifyData <T>(string publicKey, T data, string signData) where T : new()
        {
            var str = Newtonsoft.Json.JsonConvert.SerializeObject(data);
            var md5 = RSAHelper.GenerateMD5Byte(str);

            using (var rsa = RSA.Create())
            {
                rsa.ImportRSAPublicKey(Convert.FromBase64String(publicKey), out int bytesRead);
                return(rsa.VerifyData(md5, Convert.FromBase64String(signData), HashAlgorithmName.SHA1, RSASignaturePadding.Pkcs1));
            }
        }
Esempio n. 4
0
        /**
         * 使用场景
         *  1.计算T的hash
         *  2.使用private对数据加密
         *  3.使用公钥对private加密的数据解密,验证
         */

        /// <summary>
        /// 对一段数据进行签名
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="data"></param>
        /// <returns></returns>
        public static string SignData <T>(string privateKey, T data) where T : new()
        {
            var str = Newtonsoft.Json.JsonConvert.SerializeObject(data);
            var md5 = RSAHelper.GenerateMD5Byte(str);

            using (var rsa = RSA.Create())
            {
                rsa.ImportPkcs8PrivateKey(Convert.FromBase64String(privateKey), out int bytesRead);
                var rs = rsa.SignData(md5, HashAlgorithmName.SHA1, RSASignaturePadding.Pkcs1);
                return(Convert.ToBase64String(rs));
            }
        }