Search code examples
c#asp.netsqlsql-server-2008encryption-symmetric

Encryption through C# & Decryption through SQL Server 2008 for ASP.NET app


On my front end, I have following functions:

method to encrypt:

  public static string Encrypt(string strToEncrypt)
        {

            TripleDESCryptoServiceProvider objDESCrypto = new TripleDESCryptoServiceProvider();
            MD5CryptoServiceProvider objHashMD5 = new MD5CryptoServiceProvider();
            byte[] byteHash, byteBuff;
            byteHash = objHashMD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(GetEncryptionKey));
            objHashMD5 = null;
            objDESCrypto.Key = byteHash;
            objDESCrypto.Mode = CipherMode.ECB; //CBC, CFB
            byteBuff = ASCIIEncoding.ASCII.GetBytes(strToEncrypt);
            return Convert.ToBase64String(objDESCrypto.CreateEncryptor().TransformFinalBlock(byteBuff, 0, byteBuff.Length));


        }

method to decrypt:

public static string Decrypt(string strEncrypted)
        {

            TripleDESCryptoServiceProvider objDESCrypto = new TripleDESCryptoServiceProvider();
            MD5CryptoServiceProvider objHashMD5 = new MD5CryptoServiceProvider();
            byte[] byteHash, byteBuff;
            byteHash = objHashMD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(GetEncryptionKey));
            objHashMD5 = null;
            objDESCrypto.Key = byteHash;
            objDESCrypto.Mode = CipherMode.ECB; //CBC, CFB
            byteBuff = Convert.FromBase64String(strEncrypted);
            string strDecrypted = ASCIIEncoding.ASCII.GetString(objDESCrypto.CreateDecryptor().TransformFinalBlock(byteBuff, 0, byteBuff.Length));
            objDESCrypto = null;
            return strDecrypted;

        }

and the GetEncryptionKey function is

   private static string GetEncryptionKey
    {
        get { return "#$&*(^($%"; }
    }

EDIT: Now here's my problem: so am asked to Encrypt the User's password using Encrypt function written on the front end (during registration or password change) and Decrypt the encrypted password(from front end) through a Stored Procedure( during login). How can I do so?


Solution

  • To answer your question: you can plug in .net assemblies into SqlServer to run .net functions - here is a Tutorial for .net 2.0 / SqlServer2005