/// <summary>
        /// Decrypts the specified encrypted.
        /// </summary>
        /// <param name="encrypted">The encrypted.</param>
        /// <param name="key">The key.</param>
        /// <param name="encoding">The encoding.</param>
        /// <returns></returns>
        public static string Decrypt(string encrypted, string key, Encoding encoding)
        {
            var isRsa       = "FALSE";
            var isTripleDes = "TRUE";

            try
            {
                isRsa       = RegistryConfigurationAdapter.Read("IsRSAEnabled", "false").ToUpper();
                isTripleDes = RegistryConfigurationAdapter.Read("IsTripleDES", "true").ToUpper();
            }
            catch { }

            if (isRsa.ToUpper() == "TRUE")
            {
                if (x509Certificate == null)
                {
                    x509Certificate = new X509Certificate2(RegistryConfigurationAdapter.Read("RSAPrivateKeyPath", ""), RegistryConfigurationAdapter.Read("PrivateKeyPassword", ""));
                }

                if (x509Certificate.NotAfter < DateTime.Now)
                {
                    throw new CryptographicException("Certificate Expired");
                }

                if (x509Certificate.NotBefore > DateTime.Now)
                {
                    throw new CryptographicException("Invalid Certificate date");
                }

                if (!x509Certificate.HasPrivateKey)
                {
                    throw new CryptographicException("Not a valid Certificate (No Private key available)");
                }

                var rsaCryptoServiceProvider = (RSACryptoServiceProvider)x509Certificate.PrivateKey;

                byte[] nbuff           = Convert.FromBase64String(encrypted);
                var    encryptedString = Encoding.Default.GetString(nbuff);

                var buff = Encoding.Default.GetBytes(encryptedString);
                var encryptedMessagesInBytes = rsaCryptoServiceProvider.Decrypt(buff, true);
                encrypted = Encoding.Default.GetString(encryptedMessagesInBytes);
            }
            if (isTripleDes.ToUpper() == "TRUE")
            {
                byte[] buff = Convert.FromBase64String(encrypted);
                byte[] kb   = Encoding.Default.GetBytes(key);
                return(encoding.GetString(Decrypt(buff, kb)));
            }
            return(encrypted);
        }
Example #2
0
        protected override void OnStart(string[] args)
        {
            LogManager.WriteLog("Start Service Initiated", LogManager.enumLogLevel.Info);

            DBHelper dbHelper = new DBHelper();

            string regulatoryType      = string.Empty;
            bool   isRegulatoryEnabled = false;

            regulatoryType      = dbHelper.GetSettingFromDB("RegulatoryType").ToUpper();
            isRegulatoryEnabled = Convert.ToBoolean(dbHelper.GetSettingFromDB("IsRegulatoryEnabled"));

            LogManager.WriteLog(string.Format("{0} - {1}, {2} - {3}", "Regulatory Enabled", isRegulatoryEnabled.ToString().ToUpper(),
                                              "Regulatory Type", regulatoryType.ToUpper()), LogManager.enumLogLevel.Info);

            if (isRegulatoryEnabled == true && regulatoryType == "AAMS")
            {
                LogManager.WriteLog("Checking Site Status...", LogManager.enumLogLevel.Info);

                if (dbHelper.CheckSiteStatus())
                {
                    LogManager.WriteLog("Site is in enabled state.", LogManager.enumLogLevel.Info);
                }
                else
                {
                    LogManager.WriteLog("Site is in disable state. Cannot start service.", LogManager.enumLogLevel.Info);
                    throw new Exception("Site is in disable state. Cannot start service.");
                }
            }

            try
            {
                SiteMessageProcessor.EnterpriseUrl = RegistryConfigurationAdapter.Read("LFMWebService", "");
                LogManager.WriteLog("Enterprise URL : " + SiteMessageProcessor.EnterpriseUrl, LogManager.enumLogLevel.Info);

                if (string.IsNullOrEmpty(SiteMessageProcessor.EnterpriseUrl))
                {
                    throw new InvalidCredentialException("Web Service is Null or Empty");
                }
                _siteMessageProcessor.StartEngine();
            }
            catch (Exception exception)
            {
                ExceptionManager.Publish(exception);
                throw exception;
            }
            LogManager.WriteLog("Service Started", LogManager.enumLogLevel.Info);
        }
        /// <summary>
        /// Encrypts the specified original.
        /// </summary>
        /// <param name="original">The original.</param>
        /// <param name="key">The key.</param>
        /// <returns></returns>
        public static string Encrypt(string original, string key)
        {
            var isRsa       = "FALSE";
            var isTripleDes = "TRUE";

            try
            {
                isRsa       = RegistryConfigurationAdapter.Read("IsRSAEnabled", "FALSE").ToUpper();
                isTripleDes = RegistryConfigurationAdapter.Read("IsTripleDES", "TRUE").ToUpper();
            }
            catch { }

            if (isRsa.ToUpper() == "TRUE")
            {
                if (x509Certificate2PublicKey == null)
                {
                    x509Certificate2PublicKey = new X509Certificate2(RegistryConfigurationAdapter.Read("RSAPublicKeyPath", ""));
                }

                if (x509Certificate2PublicKey.NotAfter < DateTime.Now)
                {
                    throw new CryptographicException("Certificate Expired");
                }

                if (x509Certificate2PublicKey.NotBefore > DateTime.Now)
                {
                    throw new CryptographicException("Invalid Certificate date");
                }

                var rsaCryptoServiceProvider = (RSACryptoServiceProvider)x509Certificate2PublicKey.PublicKey.Key;

                var buffer = Encoding.Default.GetBytes(original);
                var encryptedMessagesInBytes = rsaCryptoServiceProvider.Encrypt(buffer, true);
                var encryptedMessage         = Encoding.Default.GetString(encryptedMessagesInBytes);
                var nbuff = Encoding.Default.GetBytes(encryptedMessage);
                original = Convert.ToBase64String(nbuff);
            }

            if (isTripleDes.ToUpper() == "TRUE")
            {
                byte[] buff = Encoding.Default.GetBytes(original);
                byte[] kb   = Encoding.Default.GetBytes(key);
                return(Convert.ToBase64String(Encrypt(buff, kb)));
            }
            return(original);
        }