Question

I’m working on a mvc application over .Net, to secure my sensitive information in web.config I’ve got two functions in my model that encrypts and decrypts information using Triple DES, however I’m new to this and succeeded to reach till here by the help of a friend and asking some on line help here.

But now when I m trying to execute the code in my PC am getting this error:

Cannot resolve symbol GetSHA256String

in both encrypt and decrypt functions where this method is used.

Am i missing a directory here? kindly help me out with this.

public static string Encrypt(string Message, string Passphrase, out byte[] iv)
    {
        byte[] Results;
        System.Text.UTF8Encoding UTF8 = new System.Text.UTF8Encoding();
        MD5CryptoServiceProvider HashProvider = new MD5CryptoServiceProvider();
        byte[] TDESKey = HashProvider.ComputeHash(UTF8.GetBytes(GetSHA256String(Passphrase)));
        TripleDESCryptoServiceProvider TDESAlgorithm = new TripleDESCryptoServiceProvider();
        TDESAlgorithm.Key = TDESKey;
        TDESAlgorithm.Mode = CipherMode.ECB;
        TDESAlgorithm.Padding = PaddingMode.PKCS7;
        // Capture the randomly generated IV
        iv = TDESAlgorithm.IV;
        byte[] DataToEncrypt = UTF8.GetBytes(Message);
        try
        {
            ICryptoTransform Encryptor = TDESAlgorithm.CreateEncryptor();
            Results = Encryptor.TransformFinalBlock(DataToEncrypt, 0, DataToEncrypt.Length);
        }
        finally
        {
            TDESAlgorithm.Clear();
            HashProvider.Clear();
        }
        return Convert.ToBase64String(Results);
        //return Encoding.UTF8.GetString(Results);
    }

    public static string Decrypt(string Message, string Passphrase, byte[] iv)
    {
        byte[] Results;
        System.Text.UTF8Encoding UTF8 = new System.Text.UTF8Encoding();
        MD5CryptoServiceProvider HashProvider = new MD5CryptoServiceProvider();
        byte[] TDESKey = HashProvider.ComputeHash(UTF8.GetBytes(GetSHA256String(Passphrase)));
        TripleDESCryptoServiceProvider TDESAlgorithm = new TripleDESCryptoServiceProvider();
        TDESAlgorithm.Key = TDESKey;
        // Apply the same IV used during encryption
        TDESAlgorithm.IV = iv;
        TDESAlgorithm.Mode = CipherMode.ECB;
        TDESAlgorithm.Padding = PaddingMode.PKCS7;
        try
        {
            byte[] DataToDecrypt = Convert.FromBase64String(Message);
            //byte[] DataToDecrypt = UTF8.GetBytes(Message);
            //byte[] DataToDecrypt = Encoding.UTF8.GetBytes(Message);
            ICryptoTransform Decryptor = TDESAlgorithm.CreateDecryptor();
            Results = Decryptor.TransformFinalBlock(DataToDecrypt, 0, DataToDecrypt.Length); // << ERROR is here.
        }
        finally
        {
            TDESAlgorithm.Clear();
            HashProvider.Clear();
        }
        return UTF8.GetString(Results);
    }
Was it helpful?

Solution

I believe you need something similar to this method added to your class:

private string GetSHA256String(string text)
{
    var UE = new UnicodeEncoding();
    var message = UE.GetBytes(text);

    var hashString = new SHA256Managed();
    var hex = string.Empty;

    var hashValue = hashString.ComputeHash(message);
    foreach (byte b in hashValue)
    {
        hex += String.Format("{0:x2}", b);
    }
    return hex;
}

Let me know if this does the trick.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top