Example #1
0
File: RSA.cs Project: xh91900/Src
        /// <summary>
        /// 校验
        /// </summary>
        public bool VerifyData(string sourceStr, string signatureStr, string public_key)
        {
            bool retVal = false;

            try
            {
                public_key = public_key.Trim('/');

                var currentKeyType    = KeyType.Content;
                var currentKeyContent = public_key;

                if (RegExpHelper.IsUrl(public_key) || RegExpHelper.IsRelativePath(public_key) || RegExpHelper.IsPhysicalPath(public_key))
                {
                    currentKeyType = KeyType.Path;
                }

                if (currentKeyType == KeyType.Path)
                {
                    currentKeyContent = FileHelper.GetFileContent(public_key);
                }

                if (!string.IsNullOrEmpty(currentKeyContent))
                {
                    var rsa = new RSACryptoServiceProvider();
                    rsa.PersistKeyInCsp = false;
                    LoadPublicKeyPEM(rsa, currentKeyContent);
                    retVal = rsa.VerifyData(this.DefaultEncode.GetBytes(sourceStr), new SHA1CryptoServiceProvider(), Convert.FromBase64String(signatureStr));
                }
            }
            catch { }
            return(retVal);
        }
Example #2
0
File: RSA.cs Project: xh91900/Src
        /// <summary>
        /// 签名
        /// </summary>
        public string Sign(string str, string private_key)
        {
            string retVal = string.Empty;

            try
            {
                private_key = private_key.Trim('/');

                var currentKeyType    = KeyType.Content;
                var currentKeyContent = private_key;

                if (RegExpHelper.IsUrl(private_key) || RegExpHelper.IsRelativePath(private_key) || RegExpHelper.IsPhysicalPath(private_key))
                {
                    currentKeyType = KeyType.Path;
                }

                if (currentKeyType == KeyType.Path)
                {
                    currentKeyContent = FileHelper.GetFileContent(private_key);
                    currentKeyContent = RemoveHeaderFooter("RSA PRIVATE KEY", currentKeyContent);
                }

                if (!string.IsNullOrEmpty(currentKeyContent))
                {
                    var    rsaCsp         = DecodeRSAPrivateKey(Convert.FromBase64String(currentKeyContent));
                    byte[] signatureBytes = rsaCsp.SignData(this.DefaultEncode.GetBytes(str), new SHA1CryptoServiceProvider());
                    retVal = Convert.ToBase64String(signatureBytes);
                }
            }
            catch { }
            return(retVal);
        }
Example #3
0
File: RSA.cs Project: xh91900/Src
        /// <summary>
        /// 加密
        /// </summary>
        public string Encrypt(string str, string public_key)
        {
            string retVal = string.Empty;

            try
            {
                public_key = public_key.Trim('/');

                var currentKeyType    = KeyType.Content;
                var currentKeyContent = public_key;

                if (RegExpHelper.IsUrl(public_key) || RegExpHelper.IsRelativePath(public_key) || RegExpHelper.IsPhysicalPath(public_key))
                {
                    currentKeyType = KeyType.Path;
                }

                if (currentKeyType == KeyType.Path)
                {
                    currentKeyContent = FileHelper.GetFileContent(public_key);
                }

                if (!string.IsNullOrEmpty(currentKeyContent))
                {
                    var rsa = new RSACryptoServiceProvider();
                    rsa.PersistKeyInCsp = false;
                    LoadPublicKeyPEM(rsa, currentKeyContent);

                    byte[] dataBuffer   = this.DefaultEncode.GetBytes(str);
                    int    maxBlockSize = rsa.KeySize / 8 - 11; //加密块最大长度限制
                    if (dataBuffer.Length > maxBlockSize)
                    {
                        MemoryStream plaiStream = new MemoryStream(dataBuffer);
                        MemoryStream crypStream = new MemoryStream();
                        Byte[]       buffer     = new Byte[maxBlockSize];
                        int          blockSize  = plaiStream.Read(buffer, 0, maxBlockSize);
                        while (blockSize > 0)
                        {
                            Byte[] toEncrypt = new Byte[blockSize];
                            Array.Copy(buffer, 0, toEncrypt, 0, blockSize);
                            Byte[] cryptograph = rsa.Encrypt(toEncrypt, false);
                            crypStream.Write(cryptograph, 0, cryptograph.Length);
                            blockSize = plaiStream.Read(buffer, 0, maxBlockSize);
                        }
                        retVal = Convert.ToBase64String(crypStream.ToArray(), Base64FormattingOptions.None);
                    }
                    else
                    {
                        retVal = Convert.ToBase64String(rsa.Encrypt(dataBuffer, false));
                    }
                }
            }
            catch { }
            return(retVal);
        }
Example #4
0
File: RSA.cs Project: xh91900/Src
        /// <summary>
        /// 解密
        /// </summary>
        public string Decrypt(string str, string private_key)
        {
            string retVal = string.Empty;

            try
            {
                private_key = private_key.Trim('/');

                var currentKeyType    = KeyType.Content;
                var currentKeyContent = private_key;

                if (RegExpHelper.IsUrl(private_key) || RegExpHelper.IsRelativePath(private_key) || RegExpHelper.IsPhysicalPath(private_key))
                {
                    currentKeyType = KeyType.Path;
                }

                if (currentKeyType == KeyType.Path)
                {
                    currentKeyContent = FileHelper.GetFileContent(private_key);
                    currentKeyContent = RemoveHeaderFooter("RSA PRIVATE KEY", currentKeyContent);
                }

                if (!string.IsNullOrEmpty(currentKeyContent))
                {
                    var    rsaCsp       = DecodeRSAPrivateKey(Convert.FromBase64String(currentKeyContent));
                    byte[] dataBuffer   = Convert.FromBase64String(str);
                    int    maxBlockSize = rsaCsp.KeySize / 8; //解密块最大长度限制
                    if (dataBuffer.Length > maxBlockSize)
                    {
                        MemoryStream crypStream = new MemoryStream(dataBuffer);
                        MemoryStream plaiStream = new MemoryStream();
                        Byte[]       buffer     = new Byte[maxBlockSize];
                        int          blockSize  = crypStream.Read(buffer, 0, maxBlockSize);
                        while (blockSize > 0)
                        {
                            Byte[] toDecrypt = new Byte[blockSize];
                            Array.Copy(buffer, 0, toDecrypt, 0, blockSize);
                            Byte[] cryptograph = rsaCsp.Decrypt(toDecrypt, false);
                            plaiStream.Write(cryptograph, 0, cryptograph.Length);
                            blockSize = crypStream.Read(buffer, 0, maxBlockSize);
                        }
                        retVal = this.DefaultEncode.GetString(plaiStream.ToArray());
                    }
                    else
                    {
                        retVal = this.DefaultEncode.GetString(rsaCsp.Decrypt(dataBuffer, false));
                    }
                }
            }
            catch { }
            return(retVal);
        }
Example #5
0
        /// <summary>
        /// Integer multiply
        /// </summary>
        /// <param name="num1"></param>
        /// <param name="num2"></param>
        /// <returns></returns>
        public static string IntegerMultiply(string num1, string num2)
        {
            if (!RegExpHelper.IsNumeric(num1))
            {
                throw new ArgumentException(nameof(num1) + $" ={num1} have non-integer numeric,please check.");
            }
            if (!RegExpHelper.IsNumeric(num2))
            {
                throw new ArgumentException(nameof(num2) + $" ={num2} have non-integer numeric,please check.");
            }
            var oneFlag = RegExpHelper.IsLeftZeroNumeric(num1);
            var twoFlag = RegExpHelper.IsLeftZeroNumeric(num2);

            num1 = oneFlag ? num1.Substring(1) : num1;
            num2 = twoFlag ? num2.Substring(1) : num2;
            var leftZero = ((oneFlag ? 1 : 0) + (twoFlag ? 1 : 0)) % 2 == 1;
            int m = num1.Length, n = num2.Length;

            int[] pos = new int[m + n];

            for (int i = m - 1; i >= 0; i--)
            {
                for (int j = n - 1; j >= 0; j--)
                {
                    int mul = (num1[i] - '0') * (num2[j] - '0');
                    int p1 = i + j, p2 = i + j + 1;
                    int sum = mul + pos[p2];

                    pos[p1] += sum / 10;
                    pos[p2]  = (sum) % 10;
                }
            }
            StringBuilder sb = new StringBuilder();

            foreach (int p in pos)
            {
                if (!(sb.Length == 0 && p == 0))
                {
                    sb.Append(p);
                }
            }
            return(sb.Length == 0 ? "0" : (leftZero ? "-" : "") + sb);
        }
Example #6
0
        /// <summary>
        /// 获取第一个模型验证错误
        /// </summary>
        protected ModelStateError GetFirstModelError()
        {
            ModelStateError retVal = null;

            if (this.CurrentFormParameters != null && this.CurrentFormParameters.Count == 1 && this.CurrentFormParameters.First().Key == "model") // 硬编码
            {
                if (this.CurrentFormParameters.FirstOrDefault().Value == null)
                {
                    retVal = new ModelStateError()
                    {
                        key = this.CurrentFormParameters.First().Key,
                        msg = ConvertHelper.GetString((int)CommonEnum.ProgErrorString.Key_999996)
                    };
                }
                else if (!this.ModelState.IsValid)
                {
                    try
                    {
                        #region "Sort"

                        var allPropertysForErrors = new Dictionary <string, int>();

                        var allPropertys = ObjectHelper.GetProperties(this.CurrentFormParameters.First().Value);
                        foreach (var item in ModelState.Keys)
                        {
                            var index = 0;
                            for (int i = 0; i < allPropertys.Length; i++)
                            {
                                if (allPropertys[i].Name == (item.Contains(".") ? item.Substring(item.LastIndexOf(".") + 1) : item))
                                {
                                    index = i;
                                    break;
                                }
                            }
                            allPropertysForErrors.Add(item, index);
                        }

                        allPropertysForErrors = allPropertysForErrors.OrderBy(item => item.Value).ToDictionary(k => k.Key, v => v.Value);

                        #endregion

                        foreach (var item in allPropertysForErrors)
                        {
                            var error = this.ModelState[item.Key].Errors.Where(value => RegExpHelper.IsAllNumber(value.ErrorMessage)).FirstOrDefault();
                            if (error != null)
                            {
                                retVal = new ModelStateError()
                                {
                                    key = (item.Key.Contains(".") ? item.Key.Substring(item.Key.LastIndexOf(".") + 1) : item.Key),
                                    msg = error.ErrorMessage
                                };
                                break;
                            }
                        }
                    }
                    catch { }
                }
            }
            return(retVal);
        }