Exemple #1
0
        /// <summary>
        /// 导入ASN格式的密钥
        /// </summary>
        /// <param name="base64Key"></param>
        /// <returns></returns>
        public static KeyPair ImportASNKey(string base64Key)
        {
            var isPrivate = base64Key.Length > 500;

            var keyParser = new Cn.Ubingo.Security.Interop.AsnKeyParser(base64Key);

            RSAParameters rsaParameters = isPrivate ? keyParser.ParseRSAPrivateKey() : keyParser.ParseRSAPublicKey();

            var rsa = new RSACryptoServiceProvider();

            rsa.ImportParameters(rsaParameters);

            return(new KeyPair(rsa, KeyFormat.ASN));
        }
        private void _MakesureProvider()
        {
            if (this._provider != null) return;

            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();

            switch (this._format)
            {
                case KeyFormat.PEM:
                    {
                        this._key = this._key.Replace("-----BEGIN PUBLIC KEY-----", "").Replace("-----END PUBLIC KEY-----", "")
                                             .Replace("-----BEGIN PRIVATE KEY-----", "").Replace("-----END PRIVATE KEY-----", "")
                                             .Replace("\r", "").Replace("\n", "");
                        goto case KeyFormat.ASN;
                    }
                case KeyFormat.ASN:
                    {
                        bool isPrivate = this._key.Length > 500;

                        AsnKeyParser keyParser = new AsnKeyParser(this._key);
                        RSAParameters key = isPrivate ? keyParser.ParseRSAPrivateKey() : keyParser.ParseRSAPublicKey();
                        rsa.ImportParameters(key);

                        break;
                    }
                case KeyFormat.XML:
                    {
                        //_isPrivate = this._key.IndexOf("<D>") > -1;
                        rsa.FromXmlString(this._key);
                        break;
                    }
                default:
                    throw new Exception("no support format");
                    break;
            }

            this._provider = rsa;
        }
        /// <summary>
        /// 导入ASN格式的密钥
        /// </summary>
        /// <param name="base64Key"></param>
        /// <returns></returns>
        public static KeyPair ImportASNKey(string base64Key)
        {
            var isPrivate = base64Key.Length > 500;

            var keyParser = new Cn.Ubingo.Security.Interop.AsnKeyParser(base64Key);

            RSAParameters rsaParameters = isPrivate ? keyParser.ParseRSAPrivateKey() : keyParser.ParseRSAPublicKey();

            var rsa = new RSACryptoServiceProvider();
            rsa.ImportParameters(rsaParameters);

            return new KeyPair(rsa, KeyFormat.ASN);
        }