Click here to Skip to main content
15,888,816 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am having three text boxes name as "txtenter","txtencrypt" and "txtdecrypt" and two buttons name as "ENCRYPT","DECRYPT"..If i enter any value in "txtenter" text box then click on "ENCRYPT" button i want to convert data in "txtenter" in to encrypted formated data display in "txtencrypt" and click on "DECRYPT" again decrypt the data display in "txtdecrypt".



ASP.NET
<asp:TextBox id="txtenter" runat="server"/>
<asp:TextBox id="txtencrypt" runat="server"/>
<asp:TextBox id="txtdecrypt" runat="server"/>
<asp:Button id="ENCRYPT" runat="sever"/>
<asp:Button id="DECRYPT" runat="sever"/>



Regards
sarath.
Posted
Updated 3-Jul-13 3:51am
v3
Comments
[no name] 3-Jul-13 9:52am    
Okay, your question and/or problem is what exactly?
Mohamed Mitwalli 3-Jul-13 9:55am    
Try Google
sarath from cst 3-Jul-13 10:05am    
both decrypt and encrypt.
phil.o 3-Jul-13 10:42am    
Technically not a question ; just som requirements you submit to us, but it does not seem like you have done the analyze-job yourself.

Your actual question doesn't mention passwords, but on the off chance that you are planning on using the code to encrypt and decrypt passwords (as the subject line implies) then I will say this: Don't.

See here: Password Storage: How to do it.[^]
 
Share this answer
 
Try this,,,,:)

C#
using System;
using System.IO;
using System.Text;
using System.Security.Cryptography;
 
namespace Paddedwall.CryptoLib
{
   
    public class Crypto
    {
        #region enums, constants & fields
        //types of symmetric encyption
        public enum CryptoTypes
        {
            encTypeDES = 0,
            encTypeRC2,
            encTypeRijndael,
            encTypeTripleDES
        }
 
        private const string CRYPT_DEFAULT_PASSWORD = "CB06cfE507a1";
        private const CryptoTypes CRYPT_DEFAULT_METHOD = CryptoTypes.encTypeRijndael;
 
        private byte[] mKey = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24};
        private byte[] mIV = {65, 110, 68, 26, 69, 178, 200, 219};
        private byte[] SaltByteArray  = {0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76};
        private CryptoTypes mCryptoType = CRYPT_DEFAULT_METHOD;
        private string mPassword = CRYPT_DEFAULT_PASSWORD;
        #endregion
 
        #region Constructors
 
        public Crypto()
        {
            calculateNewKeyAndIV();
        }
 
        public Crypto(CryptoTypes CryptoType)
        {
            this.CryptoType = CryptoType;
        }
        #endregion
 
        #region Props
 
      
        public CryptoTypes CryptoType
        {
            get
            {
                return mCryptoType;
            }
            set
            {
                if (mCryptoType != value)
                {
                    mCryptoType = value;
                    calculateNewKeyAndIV();
                }
            }
        }
 
        /// <summary>
        ///     Passsword Key Property.
        ///     The password key used when encrypting / decrypting
        /// </summary>
        public string Password
        {
            get
            {
                return mPassword;
            }
            set
            {
                if (mPassword != value)
                {
                    mPassword = value;
                    calculateNewKeyAndIV();
                }
            }
        }
        #endregion
 
        #region Encryption
 
      
        public string Encrypt(string inputText)
        {
            //declare a new encoder
            UTF8Encoding UTF8Encoder = new UTF8Encoding();
            //get byte representation of string
            byte[] inputBytes = UTF8Encoder.GetBytes(inputText);
 
            //convert back to a string
            return Convert.ToBase64String(EncryptDecrypt(inputBytes,true));
        }
 
      
        public string Encrypt(string inputText, string password)
        {
            this.Password = password;
            return this.Encrypt(inputText);
        }
 
       
        public string Encrypt(string inputText, string password, CryptoTypes cryptoType)
        {
            mCryptoType = cryptoType;
            return this.Encrypt(inputText,password);
        }
 
      
        public string Encrypt(string inputText, CryptoTypes cryptoType)
        {
            this.CryptoType = cryptoType;
            return this.Encrypt(inputText);
        }
 
        #endregion
 
        #region Decryption
 
      
        public string Decrypt(string inputText)
        {
            //declare a new encoder
            UTF8Encoding UTF8Encoder = new UTF8Encoding();
            //get byte representation of string
            byte[] inputBytes = Convert.FromBase64String(inputText);
 
            //convert back to a string
            return UTF8Encoder.GetString(EncryptDecrypt(inputBytes,false));
        }
 
        /// <summary>
        ///     decrypts a string using a user defined password key
        /// </summary>
        /// <param name="inputText">string to decrypt</param>
        /// <param name="password">password to use when decrypting</param>
        /// <returns>a decrypted string</returns>
        public string Decrypt(string inputText, string password)
        {
            this.Password = password;
            return Decrypt(inputText);
        }
 
     
        public string Decrypt(string inputText, string password, CryptoTypes cryptoType)
        {
            mCryptoType = cryptoType;
            return Decrypt(inputText,password);
        }
 
     
        public string Decrypt(string inputText, CryptoTypes cryptoType)
        {
            this.CryptoType = cryptoType;
            return Decrypt(inputText);
        }
        #endregion
 
        #region Symmetric Engine
 
     
        private byte[] EncryptDecrypt(byte[] inputBytes, bool Encrpyt)
        {
            //get the correct transform
            ICryptoTransform transform = getCryptoTransform(Encrpyt);
 
            //memory stream for output
            MemoryStream memStream = new MemoryStream();
 
            try
            {
                //setup the cryption - output written to memstream
                CryptoStream cryptStream = new CryptoStream(memStream,transform,CryptoStreamMode.Write);
 
                //write data to cryption engine
                cryptStream.Write(inputBytes,0,inputBytes.Length);
 
                //we are finished
                cryptStream.FlushFinalBlock();
 
                //get result
                byte[] output = memStream.ToArray();
 
                //finished with engine, so close the stream
                cryptStream.Close();
 
                return output;
            }
            catch (Exception e)
            {
                //throw an error
                throw new Exception("Error in symmetric engine. Error : " + e.Message,e);
            }
        }
 
      
        private ICryptoTransform getCryptoTransform(bool encrypt)
        {
            SymmetricAlgorithm SA = selectAlgorithm();
            SA.Key = mKey;
            SA.IV = mIV;
            if (encrypt)
            {
                return SA.CreateEncryptor();
            }else
            {
                return SA.CreateDecryptor();
            }
        }
      
        private SymmetricAlgorithm selectAlgorithm()
        {
            SymmetricAlgorithm SA;
            switch (mCryptoType)
            {
                case CryptoTypes.encTypeDES:
                    SA = DES.Create();
                    break;
                case CryptoTypes.encTypeRC2:
                    SA = RC2.Create();
                    break;
                case CryptoTypes.encTypeRijndael:
                    SA = Rijndael.Create();
                    break;
                case CryptoTypes.encTypeTripleDES:
                    SA = TripleDES.Create();
                    break;
                default:
                    SA = TripleDES.Create();
                    break;
            }
            return SA;
        }
 
      
        private void calculateNewKeyAndIV()
        {
            //use salt so that key cannot be found with dictionary attack
            PasswordDeriveBytes pdb = new PasswordDeriveBytes(mPassword,SaltByteArray);
            SymmetricAlgorithm algo = selectAlgorithm();
            mKey = pdb.GetBytes(algo.KeySize / 8);
            mIV = pdb.GetBytes(algo.BlockSize / 8);
        }
 
        #endregion
    }
 
  
    {
        #region enum, constants and fields
        //types of hashing available
        public enum HashingTypes
        {
            SHA, SHA256, SHA384, SHA512, MD5
        }
        #endregion
 
        #region static members
        public static string Hash(String inputText)
        {
            return ComputeHash(inputText,HashingTypes.MD5);
        }
 
        public static string Hash(String inputText, HashingTypes hashingType)
        {
            return ComputeHash(inputText,hashingType);
        }
 
        /// <summary>
        ///     returns true if the input text is equal to hashed text
        /// </summary>
        /// <param name="inputText">unhashed text to test</param>
        /// <param name="hashText">already hashed text</param>
        /// <returns>boolean true or false</returns>
        public static bool isHashEqual(string inputText, string hashText)
        {
            return (Hash(inputText) == hashText);
        }
 
        public static bool isHashEqual(string inputText, string hashText, HashingTypes hashingType)
        {
            return (Hash(inputText,hashingType) == hashText);
        }
        #endregion
 
        #region Hashing Engine
 
        
        private static string ComputeHash(string inputText, HashingTypes hashingType)
        {
            HashAlgorithm HA = getHashAlgorithm(hashingType);
 
            //declare a new encoder
            UTF8Encoding UTF8Encoder = new UTF8Encoding();
            //get byte representation of input text
            byte[] inputBytes = UTF8Encoder.GetBytes(inputText);
 

            //hash the input byte array
            byte[] output = HA.ComputeHash(inputBytes);
 
            //convert output byte array to a string
            return Convert.ToBase64String(output);
        }
 
    
        private static HashAlgorithm getHashAlgorithm(HashingTypes hashingType)
        {
            switch (hashingType)
            {
                case HashingTypes.MD5 :
                    return new MD5CryptoServiceProvider();
                case HashingTypes.SHA :
                    return new SHA1CryptoServiceProvider();
                case HashingTypes.SHA256 :
                    return new SHA256Managed();
                case HashingTypes.SHA384 :
                    return new SHA384Managed();
                case HashingTypes.SHA512 :
                    return new SHA512Managed();
                default :
                    return new MD5CryptoServiceProvider();
            }
        }
        #endregion
 
    }
 
   
}
 
Share this answer
 
 
Share this answer
 
Comments
sarath from cst 3-Jul-13 10:07am    
nice thank u man
Nirav Prabtani 3-Jul-13 10:13am    
Welcome and mark ir as solved if it fulfill ur need...:)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900