Saturday, September 28, 2013

Simple string encryption and decryption function in SQL

Simple Password encryption / decryption SQL function:

In the SQL Query we can easily do Encryption and Decryption. Mostly we use to encrypt password column value for the security purpose.  For the SQL Encryption the below function will help you to easy encrypt and decrypt your value.

The below function for Encrypt the user value using SQL Encryption. The ‘Your Key’ value is for the encrypt KEY. The same key you need to use for the decryption also.



CREATE FUNCTION [dbo].[FN_ENCRYPTION]
(
            @INPUT VARCHAR(MAX)
)
            RETURNS VARBINARY(MAX)
            AS BEGIN
            DECLARE @OUTPUT VARBINARY(MAX)
            SELECT @OUTPUT = ENCRYPTBYPASSPHRASE('Your Key',@INPUT)
            RETURN @OUTPUT
END

Example :
SELECT [dbo].[FN_ENCRYPTION] ('vijay')
Result: 0x010000008A2D548FFD5D193EE01FF10DA1356C7D858ADAAA2A014A4F


The below function will decrypt the encrypted binary value to normal string value. The same encryption KEY you need to pass for the decryption.



CREATE FUNCTION [dbo].[FN_DECRYPTION]
(
            @INPUT VARBINARY(MAX)
)
            RETURNS VARCHAR(MAX)
            AS BEGIN
            DECLARE @OUTPUT VARCHAR(MAX)
            SELECT @OUTPUT = DECRYPTBYPASSPHRASE(Your Key ',@INPUT)
            RETURN @OUTPUT
END


Example :
SELECT [dbo].[FN_ENCRYPTION] ('vijay')
Result: 0x010000008A2D548FFD5D193EE01FF10DA1356C7D858ADAAA2A014A4F

SELECT [dbo].[FN_DECRYPTION] (0x010000008A2D548FFD5D193EE01FF10DA1356C7D858ADAAA2A014A4F)
Result: vijay

Wednesday, September 25, 2013

How to check IIS installed or not in my windows OS using C#.Net

How to check IIS installed or not in my windows OS using C#.Net
The below C# code will help you to know IIS installed in your system or not. Some time we need to know IIS installed or not before installing any web application in the local system. If installed means we don’t need to reinstall the IIS.
//Check IIS Installed or Not in the system
        private static string _iisRegKey = @"Software\Microsoft\InetStp";
        public static bool IISInstalled()
        {
            try
            {
                using (RegistryKey iisKey = Registry.LocalMachine.OpenSubKey(_iisRegKey))
                {
                    return (int)iisKey.GetValue("MajorVersion") >= 6;
                }
            }
            catch
            {
                return false;
            }
        }

Install IIS in command prompt
The below command prompt code will help you to install the IIS in command prompt or in C#.net application. The code will enable your IIS installation in the windows OS. This will work in windows7 and later version of OS.  
Create a .Bat file and past the below code and run the batch file or directly run the below command in the command prompt.

@ECHO **********INSTALATION PROCESS STATED************
@ECHO *****************************************
@ECHO **********IIS STARTED************
start /w pkgmgr /iu:IIS-WebServerRole;IIS-WebServer;IIS-CommonHttpFeatures;IIS-StaticContent;IIS-DefaultDocument;IIS-ASPNET;IIS-DirectoryBrowsing;IIS-HttpErrors;IIS-HttpRedirect;IIS-ApplicationDevelopment;IIS-NetFxExtensibility;IIS-ASP;IIS-ISAPIExtensions;IIS-ISAPIFilter;IIS-HealthAndDiagnostics;IIS-HttpLogging;IIS-LoggingLibraries;IIS-RequestMonitor;IIS-HttpTracing;IIS-CustomLogging;IIS-Security;IIS-WindowsAuthentication;IIS-WebServerManagementTools;IIS-IIS6ManagementCompatibility;IIS-LegacyScripts;IIS-LegacySnapIn;IIS-Metabase;IIS-WMICompatibility;IIS-RequestFiltering;IIS-IPSecurity;IIS-Performance;IIS-HttpCompressionStatic;IIS-WebServerManagementTools;IIS-IIS6ManagementCompatibility;IIS-ManagementConsole;IIS-ManagementScriptingTools;IIS-ManagementService;WAS-WindowsActivationService;WAS-ProcessModel;WAS-NetFxEnvironment;WAS-ConfigurationAPI;
@ECHO **********IIS COMPLETED************
@ECHO *****************************************
@ECHO **********INSTALLATION COMPLETED SUCCESSFULLY************


Run web application as administrator writes in IIS.
Some time we need to run our web application as administrator mode. One of my application run time I want to create some file in my web application. While creating the txt file in my root folder in c drive C:\inetpub\wwwroot I got security exception error. The reason is I don’t have administrator permission to write text file in my application. For that I have added the Identity impersonate is TRUE in the web config file.

<system.web>
    <identity impersonate="true" userName="sa" password="pwd"/>


Get installed application list in windows OS using C#.Net


Find installed application in my PC using C#.Net

Some time we need to know before installing our new setup file it’s already installed or not in the computer. In the below examples I have explained how to get list of application installed in the system (OS) using C#.Net 2.0. The Example 1 will give you more quick result compare then Example 2.

  private void Form1_Load(object sender, EventArgs e)
        {
            CheckInstalledApplication();
            LoadSoftwareList();
        }

In the Example1 explained get the registory access and display the list of installed application in the windows system.

Example 1:
//Check the list of application installed in the OS
        private bool CheckInstalledApplication()
        {
            string SoftwareKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
            string strDispName;
            List<string> lsRegistory = new List<string>();
            using (RegistryKey rkRegistryKeyOne = Registry.LocalMachine.OpenSubKey(SoftwareKey))
            {
                foreach (object varName in rkRegistryKeyOne.GetSubKeyNames())
                {
                    using (RegistryKey rkRegistryKeyTwo = rkRegistryKeyOne.OpenSubKey(varName.ToString()))
                    {
                        strDispName = Convert.ToString(rkRegistryKeyTwo.GetValue("DisplayName"));
                        if (!string.IsNullOrEmpty(strDispName))
                        {
                            lsRegistory.Add(strDispName + " ---> " + Convert.ToString(rkRegistryKeyTwo.GetValue("DisplayVersion")));
                        }
                    }
                }
            }           
            lsRegistory.Sort();           
            listBox1.DataSource = lsRegistory.ToArray();
            return true;            
        }

In the Example2 explained using management object get the win32 product list from the windows OS.

Example 2:
using System.Management;
        private void LoadSoftwareList()
        {
            listBox1.Items.Clear();
            ManagementObjectCollection moReturn;
            ManagementObjectSearcher moSearch;

            moSearch = new ManagementObjectSearcher("Select * from Win32_Product");

            moReturn = moSearch.Get();
            foreach (ManagementObject mo in moReturn)
            {
                listBox1.Items.Add(mo["Name"].ToString());
            }

       
Note: Thanks to Antony