Translate

> > C# Data Encryption

C# Data Encryption

Posted on Sunday, May 1, 2011 | No Comments

 Using System.Security.Cryptography


        private static TripleDESCryptoServiceProvider cryptDES3 = new TripleDESCryptoServiceProvider();
        private static MD5CryptoServiceProvider cryptMD5Hash = new MD5CryptoServiceProvider();


        #region crypto functions

        /// <summary>
        /// function to decrypt a phrase
        /// </summary>
        public static string Decrypt(string phrase)
        {
            string encodedPhrase;
            byte[] Buff;

            cryptDES3.Key = cryptMD5Hash.ComputeHash(ASCIIEncoding.ASCII.GetBytes(phrase));
            cryptDES3.Mode = CipherMode.ECB;
            ICryptoTransform desdencrypt = cryptDES3.CreateDecryptor();       
            Buff = Convert.FromBase64String(phrase);
            encodedPhrase = ASCIIEncoding.ASCII.GetString(desdencrypt.TransformFinalBlock(Buff, 0, Buff.Length));

            return encodedPhrase;
        }

        /// <summary>
        /// function to encrypt a phrase
        /// </summary>
        public static string Encrypt(string phrase)
        {
            string decodedPhrase;
            byte[] Buff;

            cryptDES3.Key = cryptMD5Hash.ComputeHash(ASCIIEncoding.ASCII.GetBytes(phrase));
            cryptDES3.Mode = CipherMode.ECB;
            ICryptoTransform desdencrypt = cryptDES3.CreateDecryptor();
            Buff = ASCIIEncoding.ASCII.GetBytes(phrase);
            decodedPhrase = Convert.ToBase64String(desdencrypt.TransformFinalBlock(Buff, 0, Buff.Length));

            return decodedPhrase;
        }

        #endregion

Leave a Reply

Powered by Blogger.