Exemple #1
0
            /// <summary>
            /// RSA encryption
            /// SHA256 hash algorithm to use the key length of at least 2048
            /// </summary>
            /// <param name="encoding">Data coding</param>
            /// <param name="keySize">Key length in bits:</param>
            /// <param name="privateKey">private Key</param>
            /// <param name="publicKey">public Key</param>
            public RsaXmlUtil(Encoding encoding, 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");
                }

                if (!string.IsNullOrEmpty(privateKey))
                {
#if NET451 || NET452
                    PrivateRsa = new MsRSA {
                        KeySize = keySize
                    };
#else
                    PrivateRsa         = MsRSA.Create();
                    PrivateRsa.KeySize = keySize;
#endif
                    PrivateRsa.ImportKeyInLvccXml(privateKey);
                }

                if (!string.IsNullOrEmpty(publicKey))
                {
#if NET451 || NET452
                    PublicRsa = new MsRSA {
                        KeySize = keySize
                    };
#else
                    PublicRsa         = MsRSA.Create();
                    PublicRsa.KeySize = keySize;
#endif
                    PublicRsa.ImportKeyInLvccXml(publicKey);
                }

                DataEncoding = encoding.SafeEncodingValue();
            }
Exemple #2
0
        public static MsRSA CreateProvider()
        {
#if NET451 || NET452
            return(new MsRSA());
#else
            return(MsRSA.Create());
#endif
        }
Exemple #3
0
            /// <summary>
            /// RSAPkcs8Util
            /// </summary>
            /// <param name="dataEncoding"></param>
            /// <param name="publicKey"></param>
            /// <param name="privateKey"></param>
            /// <param name="keySize"></param>
            public RsaPkcs8Util(Encoding dataEncoding, 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))
                {
#if NET451 || NET452
                    PrivateRsa = new MsRSA {
                        KeySize = keySize
                    };
#else
                    PrivateRsa         = MsRSA.Create();
                    PrivateRsa.KeySize = keySize;
#endif
                    PrivateRsa.TouchFromPrivateKeyInPkcs8(privateKey, out var priRsap);

#if NET451 || NET452
                    PrivateRsaKeyParameter = GetPrivateKeyParameter(privateKey);
#endif

                    if (string.IsNullOrEmpty(publicKey))
                    {
#if NET451 || NET452
                        PublicRsa = new MsRSA {
                            KeySize = keySize
                        };
#else
                        PublicRsa         = MsRSA.Create();
                        PublicRsa.KeySize = keySize;
#endif
                        var pubRsap = new RSAParameters
                        {
                            Modulus  = priRsap.Modulus,
                            Exponent = priRsap.Exponent
                        };
                        PublicRsa.ImportParameters(pubRsap);

#if NET451 || NET452
                        PublicRsaKeyParameter = GetPublicKeyParameter(publicKey);
#endif
                    }
                }

                if (!string.IsNullOrEmpty(publicKey))
                {
#if NET451 || NET452
                    PublicRsa = new MsRSA {
                        KeySize = keySize
                    };
#else
                    PublicRsa         = MsRSA.Create();
                    PublicRsa.KeySize = keySize;
#endif
                    PublicRsa.TouchFromPublicKeyInPkcs8(publicKey, out _);

#if NET451 || NET452
                    PublicRsaKeyParameter = GetPublicKeyParameter(publicKey);
#endif
                }

                DataEncoding = dataEncoding.SafeEncodingValue();
            }