Monday, 26 September 2011

Encryption and Decryption for Passwords

 public static string EncryptText(string strText)
        {
            return Encrypt(strText, "my salt key");
        }
       
        public static string DecryptText(string strText)
        {
            return Decrypt(strText, "my salt key");
        }

        //The function used to encrypt the text
        private static string Encrypt(string strText, string strEncrKey)
        {
            byte[] byKey = { };
            byte[] IV = { 18, 52, 86, 120, 144, 171, 205, 239 };
            try
            {
                byKey = System.Text.Encoding.UTF8.GetBytes(strEncrKey.Substring(1, 8));
                DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                byte[] inputByteArray = Encoding.UTF8.GetBytes(strText);
                MemoryStream ms = new MemoryStream();
                CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write);
                cs.Write(inputByteArray, 0, inputByteArray.Length);
                cs.FlushFinalBlock();
                return Convert.ToBase64String(ms.ToArray());
            }

            catch (Exception ex)
            {
                GlobalUtility.Logger.Error(ex);
                return ex.Message;
            }

        }

        //The function used to decrypt the text
        private static string Decrypt(string strText, string sDecrKey)
        {
            byte[] byKey = { };
            byte[] IV = { 18, 52, 86, 120, 144, 171, 205, 239 };
            byte[] inputByteArray = new byte[strText.Length + 1];
            try
            {
                byKey = System.Text.Encoding.UTF8.GetBytes(sDecrKey.Substring(1, 8));
                DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                inputByteArray = Convert.FromBase64String(strText);
                MemoryStream ms = new MemoryStream();
                CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write);
                cs.Write(inputByteArray, 0, inputByteArray.Length);
                cs.FlushFinalBlock();
                System.Text.Encoding encoding = System.Text.Encoding.UTF8;
                return encoding.GetString(ms.ToArray());
            }

            catch (Exception ex)
            {
                GlobalUtility.Logger.Error(ex);
                return ex.Message;
            }

        }
 

JavaScript to validate controls in ASP.NET

 function fnValidate()
{
            var bool = true;
-----------------for textbox ----------------------
            if ($('#txtbox').val().trim() = = '') {
                $("#divError").css("display", "block");
                $("#divError").text('Please Enter value');
                bool = false;
            }


----------------for DDL---------------------------
if ($("#<%= ddl1.ClientID %>").val() == '') {
                $("#error1").css("display", "block");
                bool = false;
            }
else{
  $("#error1").css("display", "none");
}
           if (bool == true) {
                $("#<%= btnSubmit.ClientID %>").click();---------- this is aspcontrol
            }
}
           <asp:TextBox runat="server" ID="txtbox" ></asp:TextBox>
             <div id='divError' style="display: none; color: red"></div>                             

<input type="button" value="Save" runat="server"  id="btnSub" onclick="javascript:fnValidate();" />
 <asp:Button ID="btnSubmit" runat="server" Style="display: none" OnClick="btnSubmit_Click" />
                                                   

Thursday, 15 September 2011

Alter Table

alter table tablename alter column colname uniqueidentifier not null

Allowing textbox to accepts only Numbers



          
$(document).ready(function(){
$("#txtNumbers").keydown(function(event) {
   if(event.shiftKey)
        event.preventDefault();
   if (event.keyCode == 46 || event.keyCode == 8) {
   }
   else {
        if (event.keyCode < 95) {
           
          if (event.keyCode < 48 || event.keyCode > 57) {
              if (event.keyCode >= 37 && event.keyCode <= 40) { 
               }
              else
              {
                event.preventDefault();
              }
          }
        }
        else {
              if (event.keyCode < 96 || event.keyCode > 105) {
                  event.preventDefault();
              }
        }
      }
   });
});