Exemple #1
0
        /// <summary>
        /// 验证字符串str的签名是否是sgin,并指定hash算法(如:SHA256)
        /// </summary>
        public bool Verify(string hash, string sgin, string str)
        {
            var byts = RSA_Unit.Base64DecodeBytes(sgin);

            if (byts == null)
            {
                return(false);
            }
            return(Verify(hash, byts, Encoding.UTF8.GetBytes(str)));
        }
Exemple #2
0
        /// <summary>
        /// 解密字符串(utf-8),解密异常返回null
        /// </summary>
        public string DecodeOrNull(string str)
        {
            if (String.IsNullOrEmpty(str))
            {
                return(null);
            }
            var byts = RSA_Unit.Base64DecodeBytes(str);

            if (byts == null)
            {
                return(null);
            }
            var val = DecodeOrNull(byts);

            if (val == null)
            {
                return(null);
            }
            return(Encoding.UTF8.GetString(val));
        }
Exemple #3
0
        /// <summary>
        /// 用PEM格式密钥对创建RSA,支持PKCS#1、PKCS#8格式的PEM
        /// </summary>
        public static RSACryptoServiceProvider FromPEM(string pem)
        {
            var rsaParams = new CspParameters();

            rsaParams.Flags = CspProviderFlags.UseMachineKeyStore;
            var rsa = new RSACryptoServiceProvider(rsaParams);

            var param = new RSAParameters();

            var base64 = _PEMCode.Replace(pem, "");
            var data   = RSA_Unit.Base64DecodeBytes(base64);

            if (data == null)
            {
                throw new Exception("PEM内容无效");
            }
            var idx = 0;

            //读取长度
            Func <byte, int> readLen = (first) => {
                if (data[idx] == first)
                {
                    idx++;
                    if (data[idx] == 0x81)
                    {
                        idx++;
                        return(data[idx++]);
                    }
                    else if (data[idx] == 0x82)
                    {
                        idx++;
                        return((((int)data[idx++]) << 8) + data[idx++]);
                    }
                    else if (data[idx] < 0x80)
                    {
                        return(data[idx++]);
                    }
                }
                throw new Exception("PEM未能提取到数据");
            };
            //读取块数据
            Func <byte[]> readBlock = () => {
                var len = readLen(0x02);
                if (data[idx] == 0x00)
                {
                    idx++;
                    len--;
                }
                var val = data.sub(idx, len);
                idx += len;
                return(val);
            };
            //比较data从idx位置开始是否是byts内容
            Func <byte[], bool> eq = (byts) => {
                for (var i = 0; i < byts.Length; i++, idx++)
                {
                    if (idx >= data.Length)
                    {
                        return(false);
                    }
                    if (byts[i] != data[idx])
                    {
                        return(false);
                    }
                }
                return(true);
            };



            if (pem.Contains("PUBLIC KEY"))
            {
                /****使用公钥****/
                //读取数据总长度
                readLen(0x30);
                if (!eq(_SeqOID))
                {
                    throw new Exception("PEM未知格式");
                }
                //读取1长度
                readLen(0x03);
                idx++;                //跳过0x00
                //读取2长度
                readLen(0x30);

                //Modulus
                param.Modulus = readBlock();

                //Exponent
                param.Exponent = readBlock();
            }
            else if (pem.Contains("PRIVATE KEY"))
            {
                /****使用私钥****/
                //读取数据总长度
                readLen(0x30);

                //读取版本号
                if (!eq(_Ver))
                {
                    throw new Exception("PEM未知版本");
                }

                //检测PKCS8
                var idx2 = idx;
                if (eq(_SeqOID))
                {
                    //读取1长度
                    readLen(0x04);
                    //读取2长度
                    readLen(0x30);

                    //读取版本号
                    if (!eq(_Ver))
                    {
                        throw new Exception("PEM版本无效");
                    }
                }
                else
                {
                    idx = idx2;
                }

                //读取数据
                param.Modulus  = readBlock();
                param.Exponent = readBlock();
                param.D        = readBlock();
                param.P        = readBlock();
                param.Q        = readBlock();
                param.DP       = readBlock();
                param.DQ       = readBlock();
                param.InverseQ = readBlock();
            }
            else
            {
                throw new Exception("pem需要BEGIN END标头");
            }

            rsa.ImportParameters(param);
            return(rsa);
        }