Пример #1
0
        /// <summary>
        /// RSA encryption
        /// SHA256 hash algorithm to use the key length of at least 2048
        /// </summary>
        /// <param name="dataEncoding">Data Encoding</param>
        /// <param name="keySize">Key length in bits</param>
        /// <param name="privateKey">Private Key</param>
        /// <param name="publicKey">Public Key</param>
        public RsaXmlUtil(Encoding dataEncoding, string publicKey, string privateKey = null, int keySize = 2048)
        {
            if (string.IsNullOrEmpty(privateKey) && string.IsNullOrEmpty(publicKey))
            {
                throw new ArgumentException("Public and private keys must not be empty at the same time");
            }
            else
            {
                RSAParameters pubRsap;

                if (!string.IsNullOrEmpty(privateKey))
                {
                    PrivateRsa = RSA.Create();

                    PrivateRsa.KeySize = keySize;

                    RSAParameters priRsap = CreateRsapFromPrivateKey(privateKey);

                    PrivateRsa.ImportParameters(priRsap);

                    if (string.IsNullOrEmpty(publicKey))
                    {
                        PublicRsa = RSA.Create();

                        PublicRsa.KeySize = keySize;

                        pubRsap = new RSAParameters
                        {
                            Modulus = priRsap.Modulus,

                            Exponent = priRsap.Exponent
                        };

                        PublicRsa.ImportParameters(pubRsap);
                    }
                }

                if (!string.IsNullOrEmpty(publicKey))
                {
                    PublicRsa = RSA.Create();

                    pubRsap = CreateRsapFromPublicKey(publicKey);

                    PublicRsa.KeySize = keySize;

                    PublicRsa.ImportParameters(pubRsap);
                }
            }

            DataEncoding = dataEncoding ?? Encoding.UTF8;
        }
Пример #2
0
        public RsaPkcs1Util(Encoding encoding, string publicKey, string privateKey = null, int keySize = 2048)
        {
            if (string.IsNullOrEmpty(privateKey) && string.IsNullOrEmpty(publicKey))
            {
                throw new Exception("Public and private keys must not be empty at the same time");
            }


            if (!string.IsNullOrEmpty(privateKey))
            {
                PrivateRsa         = RSA.Create();
                PrivateRsa.KeySize = keySize;
                var priRsap = CreateRsapFromPrivateKey(privateKey);
                PrivateRsa.ImportParameters(priRsap);

                if (string.IsNullOrEmpty(publicKey))
                {
                    PublicRsa         = RSA.Create();
                    PublicRsa.KeySize = keySize;
                    var pubRasp = new RSAParameters
                    {
                        Modulus  = priRsap.Modulus,
                        Exponent = priRsap.Exponent
                    };
                    PublicRsa.ImportParameters(pubRasp);
                }
            }

            if (!string.IsNullOrEmpty(publicKey))
            {
                PublicRsa         = RSA.Create();
                PublicRsa.KeySize = keySize;
                PublicRsa.ImportParameters(CreateRsapFromPublicKey(publicKey));
            }

            DataEncoding = encoding ?? Encoding.UTF8;
        }