示例#1
0
        public RsaCryptoHelper(PKCSType pkcsType, string publicKey, string privateKey)
        {
            this.pkcsType = pkcsType;
            publicKey     = publicKey.Trim('\n');
            privateKey    = privateKey.Trim('\n');

            if (publicKey.StartsWith("----"))
            {
                int fidx = publicKey.IndexOf("\n");
                int lidx = publicKey.LastIndexOf("\n");
                publicKey = publicKey.Substring(fidx + 1, lidx - 1 - fidx).Replace("\n", "");
            }
            this.publicKey = Convert.FromBase64String(publicKey);

            if (privateKey.StartsWith("----"))
            {
                int fidx = privateKey.IndexOf("\n");
                int lidx = privateKey.LastIndexOf("\n");
                privateKey = privateKey.Substring(fidx + 1, lidx - 1 - fidx).Replace("\n", "");
            }
            this.privateKey = Convert.FromBase64String(privateKey);
        }
示例#2
0
文件: RSA.cs 项目: leigoCQ/ASF
        public static RSAParameters DecodePkcsPrivateKey(string privateKey)
        {
            if (string.IsNullOrEmpty(privateKey))
            {
                throw new ArgumentNullException("pemFileConent", "This arg cann't be empty.");
            }
            try
            {
                privateKey = privateKey.Replace("-----BEGIN RSA PRIVATE KEY-----", "").Replace("-----END RSA PRIVATE KEY-----", "").Replace("\n", "").Replace("\r", "");
                var privateKeyData = Convert.FromBase64String(privateKey);

                //解析Pkcs证书
                PKCSType type = GetPrivateKeyType(privateKeyData.Length);
                if (type == PKCSType.PKCS_8_1024 || type == PKCSType.PKCS_8_2048)
                {
                    //Pkcs#8秘钥需要特殊处理
                    privateKeyData = DecodePkcs8PrivateKey(privateKeyData);
                }
                var    rsaParams = new RSAParameters();
                byte   bt        = 0;
                ushort twobytes  = 0;
                //转换为二进制值
                using (var binr = new BinaryReader(new MemoryStream(privateKeyData)))
                {
                    twobytes = binr.ReadUInt16();
                    if (twobytes == 0x8130)
                    {
                        binr.ReadByte();
                    }
                    else if (twobytes == 0x8230)
                    {
                        binr.ReadInt16();
                    }
                    else
                    {
                        throw new ArgumentException("Unexpected value read )");
                    }

                    twobytes = binr.ReadUInt16();
                    if (twobytes != 0x0102)
                    {
                        throw new ArgumentException("Unexpected version");
                    }

                    bt = binr.ReadByte();
                    if (bt != 0x00)
                    {
                        throw new ArgumentException("Unexpected value read ");
                    }

                    //转换XML
                    rsaParams.Modulus  = binr.ReadBytes(GetIntegerSize(binr));
                    rsaParams.Exponent = binr.ReadBytes(GetIntegerSize(binr));
                    rsaParams.D        = binr.ReadBytes(GetIntegerSize(binr));
                    rsaParams.P        = binr.ReadBytes(GetIntegerSize(binr));
                    rsaParams.Q        = binr.ReadBytes(GetIntegerSize(binr));
                    rsaParams.DP       = binr.ReadBytes(GetIntegerSize(binr));
                    rsaParams.DQ       = binr.ReadBytes(GetIntegerSize(binr));
                    rsaParams.InverseQ = binr.ReadBytes(GetIntegerSize(binr));
                }
                return(rsaParams);
            }
            catch (Exception ex)
            {
                throw new ArgumentException("此私钥证书无效", ex);
            }
        }
示例#3
0
 public static void Init(string appid, PKCSType pkcsType, string publicKey, string privateKey)
 {
     OpenApiClient.appid = appid;
     rsaCryptoHelper     = new RsaCryptoHelper(pkcsType, publicKey, privateKey);
 }