ImportParameters() public abstract method

public abstract ImportParameters ( RSAParameters parameters ) : void
parameters RSAParameters
return void
Esempio n. 1
0
 public override void InitSign(PrivateKey key)
 {
     try
     {
         rsa.ImportParameters(((RSAPrivateKey)key).Parameters);
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex);
         throw;
     }
 }
Esempio n. 2
0
        /// <summary>
        /// RSA导入key
        /// </summary>
        /// <param name="rsa">RSA实例<see cref="RSA"/></param>
        /// <param name="jsonString">RSA的Key序列化JSON字符串</param>
        internal static void FromJsonString(this System.Security.Cryptography.RSA rsa, string jsonString)
        {
            Check.Argument.IsNotEmpty(jsonString, nameof(jsonString));

            RSAParameters parameters = new RSAParameters();

            try
            {
                var paramsJson = JsonConvert.DeserializeObject <RSAParametersJson>(jsonString);

                parameters.Modulus = paramsJson.Modulus != null?Convert.FromBase64String(paramsJson.Modulus) : null;

                parameters.Exponent = paramsJson.Exponent != null?Convert.FromBase64String(paramsJson.Exponent) : null;

                parameters.P = paramsJson.P != null?Convert.FromBase64String(paramsJson.P) : null;

                parameters.Q = paramsJson.Q != null?Convert.FromBase64String(paramsJson.Q) : null;

                parameters.DP = paramsJson.DP != null?Convert.FromBase64String(paramsJson.DP) : null;

                parameters.DQ = paramsJson.DQ != null?Convert.FromBase64String(paramsJson.DQ) : null;

                parameters.InverseQ = paramsJson.InverseQ != null?Convert.FromBase64String(paramsJson.InverseQ) : null;

                parameters.D = paramsJson.D != null?Convert.FromBase64String(paramsJson.D) : null;
            }
            catch
            {
                throw new Exception("Invalid Json RSA key.");
            }
            rsa.ImportParameters(parameters);
        }
        public static void TouchFromPrivateKeyInPkcs1(this MsRSA rsa, string privateKey, out RSAParameters parameters)
        {
            var pr = new PemReader(new StringReader(privateKey.RemovePkcs1PrivateKeyFormat()));

            if (!(pr.ReadObject() is AsymmetricCipherKeyPair asymmetricCipherKeyPair))
            {
                throw new Exception("Private key format is incorrect");
            }

            var rsaPrivateCrtKeyParameters = (RsaPrivateCrtKeyParameters)PrivateKeyFactory.CreateKey(
                PrivateKeyInfoFactory.CreatePrivateKeyInfo(asymmetricCipherKeyPair.Private));

            parameters = new RSAParameters
            {
                Modulus  = rsaPrivateCrtKeyParameters.Modulus.ToByteArrayUnsigned(),
                Exponent = rsaPrivateCrtKeyParameters.PublicExponent.ToByteArrayUnsigned(),
                P        = rsaPrivateCrtKeyParameters.P.ToByteArrayUnsigned(),
                Q        = rsaPrivateCrtKeyParameters.Q.ToByteArrayUnsigned(),
                DP       = rsaPrivateCrtKeyParameters.DP.ToByteArrayUnsigned(),
                DQ       = rsaPrivateCrtKeyParameters.DQ.ToByteArrayUnsigned(),
                InverseQ = rsaPrivateCrtKeyParameters.QInv.ToByteArrayUnsigned(),
                D        = rsaPrivateCrtKeyParameters.Exponent.ToByteArrayUnsigned()
            };

            rsa.ImportParameters(parameters);
        }
            /// <summary>Extracts the <see cref="Key"/> from the given PKCS8 private key.</summary>
            public Initializer FromPrivateKey(string privateKey)
            {
                RSAParameters rsaParameters = Pkcs8.DecodeRsaParameters(privateKey);

                Key = (RsaKey)RSA.Create();
                Key.ImportParameters(rsaParameters);
                return(this);
            }
		public void SetUp () 
		{
			if (rsa == null) {
				rsa = RSA.Create ();
				rsa.ImportParameters (AllTests.GetRsaKey (true));
			}
			if (dsa == null)
				dsa = DSA.Create ();
		}
Esempio n. 6
0
 public byte[] sign()
 {
     SSC.RSA rsa = SSC.RSA.Create();
     rsa.ImportParameters(RSAparams);
     SSC.RSAPKCS1SignatureFormatter signer = new SSC.RSAPKCS1SignatureFormatter(rsa);
     signer.SetHashAlgorithm("SHA1");
     sc.Close();
     return(signer.CreateSignature(md.Hash));
 }
		public void SetUp () 
		{
			// generating a keypair is REALLY long and the framework
			// makes sure that we generate one (even if create an object
			// to import an existing key)
			if (key == null) {
				key = RSA.Create ();
				key.ImportParameters (AllTests.GetRsaKey (true));
			}
		}
            /// <summary>Extracts the <see cref="Key"/> from the given PKCS8 private key.</summary>
            public Initializer FromPrivateKey(string privateKey)
            {
#if NETSTANDARD
                RsaPrivateCrtKeyParameters parameters = ConvertPKCS8ToRsaPrivateCrtKeyParameters(privateKey);
                Key = new RsaStandard(parameters);
#else
                RSAParameters rsaParameters = ConvertPKCS8ToRSAParameters(privateKey);
                Key = new RSACryptoServiceProvider();
                Key.ImportParameters(rsaParameters);
#endif
                return(this);
            }
Esempio n. 9
0
        public static void ImportFromXmlString(this System.Security.Cryptography.RSA rsa, string filename)
        {
            var parameters = new RSAParameters();
            var xmlDoc     = new XmlDocument();

            xmlDoc.Load(filename);
            if (xmlDoc.DocumentElement.Name.Equals("RSAKeyValue"))
            {
                foreach (XmlNode node in xmlDoc.DocumentElement.ChildNodes)
                {
                    switch (node.Name)
                    {
                    case "Modulus":
                        parameters.Modulus = Convert.FromBase64String(node.InnerText);
                        break;

                    case "Exponent":
                        parameters.Exponent = Convert.FromBase64String(node.InnerText);
                        break;

                    case "P":
                        parameters.P = Convert.FromBase64String(node.InnerText);
                        break;

                    case "Q":
                        parameters.Q = Convert.FromBase64String(node.InnerText);
                        break;

                    case "DP":
                        parameters.DP = Convert.FromBase64String(node.InnerText);
                        break;

                    case "DQ":
                        parameters.DQ = Convert.FromBase64String(node.InnerText);
                        break;

                    case "InverseQ":
                        parameters.InverseQ = Convert.FromBase64String(node.InnerText);
                        break;

                    case "D":
                        parameters.D = Convert.FromBase64String(node.InnerText);
                        break;
                    }
                }
            }
            else
            {
                throw new Exception("Invalid XML RSA key.");
            }

            rsa.ImportParameters(parameters);
        }
        public static void TouchFromPublicKeyInPkcs8(this MsRSA rsa, string publicKey, out RSAParameters parameters)
        {
            var publicKeyBytes = BaseConv.FromBase64(publicKey.RemovePkcs8PublicKeyFormat());
            var publicKeyParam = (RsaKeyParameters)PublicKeyFactory.CreateKey(publicKeyBytes);

            parameters = new RSAParameters
            {
                Modulus  = publicKeyParam.Modulus.ToByteArrayUnsigned(),
                Exponent = publicKeyParam.Exponent.ToByteArrayUnsigned()
            };

            rsa.ImportParameters(parameters);
        }
Esempio n. 11
0
        public void LoadKey() {
            string import = "";
            using(StreamReader streamReader = new StreamReader(KeyStore))
                import = streamReader.ReadToEnd();

            publicKey = new RSACryptoServiceProvider();
            publicKey.FromXmlString(import);

            AsymmetricCipherKeyPair cipherKeyPair = DotNetUtilities.GetRsaKeyPair(publicKey.ExportParameters(true));
            privateKey = cipherKeyPair.Private;
            publicKey.ImportParameters(publicKey.ExportParameters(false));
            
            Directory.CreateDirectory(DestDir);
        }
Esempio n. 12
0
        /// <summary>
        /// Encrypts a string
        /// </summary>
        /// <param name="byteData">The data to encrypt</param>
        /// <param name="publicKey">The public key used to encrypt the data</param>
        /// <returns>A string containing the encrypted data</returns>
        public string Encrypt(byte[] byteData, System.Security.Cryptography.RSAParameters publicKey)
        {
            //Log.v("Encryption Hash={0}", MD5.GetMd5Hash(publicKey.AsXml<RSAParameters>()));

            _rsa.ImportParameters(publicKey);
            //byte[] byteData = Encoding.UTF32.GetBytes(text);
            int maxLength  = (_keySize / 8) - 42;// 214;
            int dataLength = byteData.Length;
            int iterations = dataLength / maxLength;

            var sb = new StringBuilder();

            for (int i = 0; i <= iterations; i++)
            {
                var tempBytes = new byte[(dataLength - maxLength * i > maxLength) ? maxLength : dataLength - maxLength * i];
                Buffer.BlockCopy(byteData, maxLength * i, tempBytes, 0, tempBytes.Length);

                var encbyteData = _rsa.Encrypt(tempBytes, RSAEncryptionPadding.Pkcs1);
                sb.Append(Convert.ToBase64String(encbyteData));
            }
            var result = sb.ToString();

            return(result);
        }
Esempio n. 13
0
        public static string Encrypt(string encryptString, String key)
        {
            System.Security.Cryptography.RSA rsa = System.Security.Cryptography.RSA.Create();
            RSAParameters para = new RSAParameters();

            byte[] b1 = null;
            byte[] b2 = null;
            ResolveKey(key, out b1, out b2);
            para.Exponent = b1;
            para.Modulus  = b2;
            rsa.ImportParameters(para);

            byte[] enBytes = rsa.Encrypt(Encoding.UTF8.GetBytes(encryptString), RSAEncryptionPadding.Pkcs1);
            return(bytes2hex(enBytes));
        }
        public static void TouchFromPublicKeyInPkcs1(this MsRSA rsa, string publicKey, out RSAParameters parameters)
        {
            var pr = new PemReader(new StringReader(publicKey.RemovePkcs1PublicKeyFormat()));

            if (pr.ReadObject() is not RsaKeyParameters rsaKey)
            {
                throw new Exception("Public key format is incorrect");
            }

            parameters = new RSAParameters
            {
                Modulus  = rsaKey.Modulus.ToByteArrayUnsigned(),
                Exponent = rsaKey.Exponent.ToByteArrayUnsigned()
            };

            rsa.ImportParameters(parameters);
        }
        public static void TouchFromPrivateKeyInPkcs8(this MsRSA rsa, string privateKey, out RSAParameters parameters)
        {
            var privateKeyBytes = BaseConv.FromBase64(privateKey.RemovePkcs8PrivateKeyFormat());
            var privateKeyParam = (RsaPrivateCrtKeyParameters)PrivateKeyFactory.CreateKey(privateKeyBytes);

            parameters = new RSAParameters
            {
                Modulus  = privateKeyParam.Modulus.ToByteArrayUnsigned(),
                Exponent = privateKeyParam.PublicExponent.ToByteArrayUnsigned(),
                P        = privateKeyParam.P.ToByteArrayUnsigned(),
                Q        = privateKeyParam.Q.ToByteArrayUnsigned(),
                DP       = privateKeyParam.DP.ToByteArrayUnsigned(),
                DQ       = privateKeyParam.DQ.ToByteArrayUnsigned(),
                InverseQ = privateKeyParam.QInv.ToByteArrayUnsigned(),
                D        = privateKeyParam.Exponent.ToByteArrayUnsigned()
            };

            rsa.ImportParameters(parameters);
        }
Esempio n. 16
0
        public static string DecryptFromJsEncrypt(string encryptString, String key)
        {
            #region Java RSA/ECB/PKCS1Padding加密方式
            System.Security.Cryptography.RSA rsa = System.Security.Cryptography.RSA.Create();
            RSAParameters para = new RSAParameters();
            byte[]        b1   = null;
            byte[]        b2   = null;
            ResolveKey(key, out b1, out b2);
            para.Modulus  = b2;
            para.D        = b1;
            para.Exponent = Convert.FromBase64String(GlobalConfig.exponent);
            para.DP       = Convert.FromBase64String(GlobalConfig.dp);
            para.DQ       = Convert.FromBase64String(GlobalConfig.dq);
            para.InverseQ = Convert.FromBase64String(GlobalConfig.inverseQ);
            para.P        = Convert.FromBase64String(GlobalConfig.pp);
            para.Q        = Convert.FromBase64String(GlobalConfig.q);
            rsa.ImportParameters(para);
            byte[] enBytes = rsa.Decrypt(BinaryHelper.HexStringToBinary(encryptString, 0, encryptString.Length), RSAEncryptionPadding.Pkcs1);
            return(Encoding.UTF8.GetString(enBytes));

            #endregion
        }
Esempio n. 17
0
        /// <summary>
        /// RSA导入key
        /// </summary>
        /// <param name="rsa">RSA实例<see cref="RSA"/></param>
        /// <param name="jsonString">RSA的Key序列化XML字符串</param>
        public static void FromLvccXmlString(this System.Security.Cryptography.RSA rsa, string xmlString)
        {
            RSAParameters parameters = new RSAParameters();

            XmlDocument xmlDoc = new XmlDocument();

            xmlDoc.LoadXml(xmlString);

            if (xmlDoc.DocumentElement.Name.Equals("RSAKeyValue"))
            {
                foreach (XmlNode node in xmlDoc.DocumentElement.ChildNodes)
                {
                    switch (node.Name)
                    {
                    case "Modulus": parameters.Modulus = (string.IsNullOrEmpty(node.InnerText) ? null : Convert.FromBase64String(node.InnerText)); break;

                    case "Exponent": parameters.Exponent = (string.IsNullOrEmpty(node.InnerText) ? null : Convert.FromBase64String(node.InnerText)); break;

                    case "P": parameters.P = (string.IsNullOrEmpty(node.InnerText) ? null : Convert.FromBase64String(node.InnerText)); break;

                    case "Q": parameters.Q = (string.IsNullOrEmpty(node.InnerText) ? null : Convert.FromBase64String(node.InnerText)); break;

                    case "DP": parameters.DP = (string.IsNullOrEmpty(node.InnerText) ? null : Convert.FromBase64String(node.InnerText)); break;

                    case "DQ": parameters.DQ = (string.IsNullOrEmpty(node.InnerText) ? null : Convert.FromBase64String(node.InnerText)); break;

                    case "InverseQ": parameters.InverseQ = (string.IsNullOrEmpty(node.InnerText) ? null : Convert.FromBase64String(node.InnerText)); break;

                    case "D": parameters.D = (string.IsNullOrEmpty(node.InnerText) ? null : Convert.FromBase64String(node.InnerText)); break;
                    }
                }
            }
            else
            {
                throw new Exception("Invalid XML RSA key.");
            }

            rsa.ImportParameters(parameters);
        }
Esempio n. 18
0
        internal static void GetPublicKey()
        {
            while (true)
            {
                try
                {
                    if (ServerUrl.EndsWith("/") == false)
                    {
                        ServerUrl += "/";
                    }
                    var serverContnt = Way.Lib.HttpClient.GetContent($"{ServerUrl}.well-known/openid-configuration", 8000).FromJson <Dictionary <string, object> >();
                    var keyContent   = Way.Lib.HttpClient.GetContent(serverContnt["jwks_uri"].ToString(), 8000);


                    JsonWebKeySet exampleJWKS = new JsonWebKeySet(keyContent);
                    JsonWebKey    exampleJWK  = exampleJWKS.Keys.First();


                    /* Create RSA from Elements in JWK */
                    RSAParameters rsap = new RSAParameters
                    {
                        Modulus  = WebEncoders.Base64UrlDecode(exampleJWK.N),
                        Exponent = WebEncoders.Base64UrlDecode(exampleJWK.E),
                    };
                    System.Security.Cryptography.RSA rsa = System.Security.Cryptography.RSA.Create();
                    rsa.ImportParameters(rsap);
                    rsaSecurityKey = new RsaSecurityKey(rsa);

                    Thread.Sleep(RefreshPublicKeySeconds * 1000);
                }
                catch (Exception ex)
                {
                    _logger?.LogError(ex, "");
                    Thread.Sleep(1000);
                    continue;
                }
            }
        }
        internal static void ImportKeyInJson(this MsRSA rsa, string jsonString)
        {
            if (string.IsNullOrEmpty(jsonString))
            {
                throw new ArgumentNullException(nameof(jsonString));
            }

            var parameters = new RSAParameters();

            try
            {
                var paramsJson = JsonConvert.DeserializeObject <RsaJsonParameters>(jsonString);

                parameters.Modulus = paramsJson.Modulus != null?Convert.FromBase64String(paramsJson.Modulus) : null;

                parameters.Exponent =
                    paramsJson.Exponent != null?Convert.FromBase64String(paramsJson.Exponent) : null;

                parameters.P = paramsJson.P != null?Convert.FromBase64String(paramsJson.P) : null;

                parameters.Q = paramsJson.Q != null?Convert.FromBase64String(paramsJson.Q) : null;

                parameters.DP = paramsJson.DP != null?Convert.FromBase64String(paramsJson.DP) : null;

                parameters.DQ = paramsJson.DQ != null?Convert.FromBase64String(paramsJson.DQ) : null;

                parameters.InverseQ =
                    paramsJson.InverseQ != null?Convert.FromBase64String(paramsJson.InverseQ) : null;

                parameters.D = paramsJson.D != null?Convert.FromBase64String(paramsJson.D) : null;
            }
            catch
            {
                throw new Exception("Invalid Json RSA key.");
            }

            rsa.ImportParameters(parameters);
        }
Esempio n. 20
0
        public bool verify(byte[] sig)
        {
            long i = 0;
            long j = 0;

            byte[] tmp;

            //Util.Dump("c:\\sig.bin", sig);

            if (sig[0] == 0 && sig[1] == 0 && sig[2] == 0)
            {
                long i1 = (sig[i++] << 24) & 0xff000000;
                long i2 = (sig[i++] << 16) & 0x00ff0000;
                long i3 = (sig[i++] << 8) & 0x0000ff00;
                long i4 = (sig[i++]) & 0x000000ff;
                j = i1 | i2 | i3 | i4;

                i += j;

                i1 = (sig[i++] << 24) & 0xff000000;
                i2 = (sig[i++] << 16) & 0x00ff0000;
                i3 = (sig[i++] << 8) & 0x0000ff00;
                i4 = (sig[i++]) & 0x000000ff;
                j  = i1 | i2 | i3 | i4;

                tmp = new byte[j];
                Array.Copy(sig, i, tmp, 0, j); sig = tmp;
            }
            SSC.RSA rsa = SSC.RSA.Create();
            rsa.ImportParameters(RSAparams);
            SSC.RSAPKCS1SignatureDeformatter verifier = new SSC.RSAPKCS1SignatureDeformatter(rsa);
            verifier.SetHashAlgorithm("SHA1");
            sc.Close();
            bool res = verifier.VerifySignature(md, sig);

            return(res);
        }
Esempio n. 21
0
        public static string Decrypt(string encryptString, String key)
        {
            #region Java RSA/ECB/NoPadding加密方式
            //byte[] b1;
            //byte[] b2;
            //ResolveKey(key, out b1, out b2);
            //BigInteger biE = new BigInteger(b1);
            //BigInteger biN = new BigInteger(b2);

            //BigInteger biText = new BigInteger(GetBytes(encryptString), GetBytes(encryptString).Length);
            //BigInteger biEnText = biText.modPow(biE, biN);
            //string temp = System.Text.Encoding.UTF8.GetString(biEnText.getBytes());
            //return temp;
            #endregion

            #region Java RSA/ECB/PKCS1Padding加密方式
            //encryptString = WebUtility.UrlDecode(encryptString);
            System.Security.Cryptography.RSA rsa = System.Security.Cryptography.RSA.Create();
            RSAParameters para = new RSAParameters();
            byte[]        b1   = null;
            byte[]        b2   = null;
            ResolveKey(key, out b1, out b2);
            para.Modulus  = b2;
            para.D        = b1;
            para.Exponent = Convert.FromBase64String(GlobalConfig.exponent);
            para.DP       = Convert.FromBase64String(GlobalConfig.dp);
            para.DQ       = Convert.FromBase64String(GlobalConfig.dq);
            para.InverseQ = Convert.FromBase64String(GlobalConfig.inverseQ);
            para.P        = Convert.FromBase64String(GlobalConfig.pp);
            para.Q        = Convert.FromBase64String(GlobalConfig.q);
            rsa.ImportParameters(para);
            byte[] enBytes = rsa.Decrypt(Convert.FromBase64String(encryptString), RSAEncryptionPadding.Pkcs1);
            return(Encoding.UTF8.GetString(enBytes));

            #endregion
        }
        public static void ImportKeyInLvccXml(this MsRSA rsa, string xmlString)
        {
            RSAParameters parameters = new RSAParameters();

            XmlDocument xmlDoc = new XmlDocument();

            xmlDoc.LoadXml(xmlString);

            // ReSharper disable once PossibleNullReferenceException
            if (xmlDoc.DocumentElement.Name.Equals("RSAKeyValue"))
            {
                foreach (XmlNode node in xmlDoc.DocumentElement.ChildNodes)
                {
                    switch (node.Name)
                    {
                    case "Modulus":
                        parameters.Modulus = (string.IsNullOrEmpty(node.InnerText)
                                ? null
                                : Convert.FromBase64String(node.InnerText));
                        break;

                    case "Exponent":
                        parameters.Exponent = (string.IsNullOrEmpty(node.InnerText)
                                ? null
                                : Convert.FromBase64String(node.InnerText));
                        break;

                    case "P":
                        parameters.P = (string.IsNullOrEmpty(node.InnerText)
                                ? null
                                : Convert.FromBase64String(node.InnerText));
                        break;

                    case "Q":
                        parameters.Q = (string.IsNullOrEmpty(node.InnerText)
                                ? null
                                : Convert.FromBase64String(node.InnerText));
                        break;

                    case "DP":
                        parameters.DP = (string.IsNullOrEmpty(node.InnerText)
                                ? null
                                : Convert.FromBase64String(node.InnerText));
                        break;

                    case "DQ":
                        parameters.DQ = (string.IsNullOrEmpty(node.InnerText)
                                ? null
                                : Convert.FromBase64String(node.InnerText));
                        break;

                    case "InverseQ":
                        parameters.InverseQ = (string.IsNullOrEmpty(node.InnerText)
                                ? null
                                : Convert.FromBase64String(node.InnerText));
                        break;

                    case "D":
                        parameters.D = (string.IsNullOrEmpty(node.InnerText)
                                ? null
                                : Convert.FromBase64String(node.InnerText));
                        break;
                    }
                }
            }
            else
            {
                throw new Exception("Invalid XML RSA key.");
            }

            rsa.ImportParameters(parameters);
        }
            /// <summary>Extracts the <see cref="Key"/> from the given PKCS8 private key.</summary>
            public Initializer FromPrivateKey(string privateKey)
            {
#if NETSTANDARD
                RsaPrivateCrtKeyParameters parameters = ConvertPKCS8ToRsaPrivateCrtKeyParameters(privateKey);
                Key = new RsaStandard(parameters);
#else
                RSAParameters rsaParameters = ConvertPKCS8ToRSAParameters(privateKey);
                Key = new RSACryptoServiceProvider();
                Key.ImportParameters(rsaParameters);
#endif
                return this;
            }
Esempio n. 24
0
        private static System.Security.Cryptography.RSA CreateRsaProviderFromPublicKey(string publicKeyString, string signType)
        {
            byte[] seqOid = { 0x30, 0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x01, 0x05, 0x00 };
            byte[] seq    = new byte[15];

            var x509Key = Convert.FromBase64String(publicKeyString);

            using (MemoryStream mem = new MemoryStream(x509Key))
            {
                using (BinaryReader binr = new BinaryReader(mem))
                {
                    byte   bt       = 0;
                    ushort twobytes = 0;

                    twobytes = binr.ReadUInt16();
                    if (twobytes == 0x8130)
                    {
                        binr.ReadByte();
                    }
                    else if (twobytes == 0x8230)
                    {
                        binr.ReadInt16();
                    }
                    else
                    {
                        return(null);
                    }

                    seq = binr.ReadBytes(15);
                    if (!CompareBytearrays(seq, seqOid))
                    {
                        return(null);
                    }

                    twobytes = binr.ReadUInt16();
                    if (twobytes == 0x8103)
                    {
                        binr.ReadByte();
                    }
                    else if (twobytes == 0x8203)
                    {
                        binr.ReadInt16();
                    }
                    else
                    {
                        return(null);
                    }

                    bt = binr.ReadByte();
                    if (bt != 0x00)
                    {
                        return(null);
                    }

                    twobytes = binr.ReadUInt16();
                    if (twobytes == 0x8130)
                    {
                        binr.ReadByte();
                    }
                    else if (twobytes == 0x8230)
                    {
                        binr.ReadInt16();
                    }
                    else
                    {
                        return(null);
                    }

                    twobytes = binr.ReadUInt16();
                    byte lowbyte  = 0x00;
                    byte highbyte = 0x00;

                    if (twobytes == 0x8102)
                    {
                        lowbyte = binr.ReadByte();
                    }
                    else if (twobytes == 0x8202)
                    {
                        highbyte = binr.ReadByte();
                        lowbyte  = binr.ReadByte();
                    }
                    else
                    {
                        return(null);
                    }
                    byte[] modint  = { lowbyte, highbyte, 0x00, 0x00 };
                    int    modsize = BitConverter.ToInt32(modint, 0);

                    int firstbyte = binr.PeekChar();
                    if (firstbyte == 0x00)
                    {
                        binr.ReadByte();
                        modsize -= 1;
                    }

                    byte[] modulus = binr.ReadBytes(modsize);

                    if (binr.ReadByte() != 0x02)
                    {
                        return(null);
                    }
                    int    expbytes = binr.ReadByte();
                    byte[] exponent = binr.ReadBytes(expbytes);

                    System.Security.Cryptography.RSA rsa = System.Security.Cryptography.RSA.Create();
                    rsa.KeySize = signType == "RSA" ? 1024 : 2048;
                    RSAParameters rsaKeyInfo = new RSAParameters
                    {
                        Modulus  = modulus,
                        Exponent = exponent
                    };
                    rsa.ImportParameters(rsaKeyInfo);

                    return(rsa);
                }
            }
        }