0

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);
    }
Maven
  • 14,587
  • 42
  • 113
  • 174
  • So you're saying you can compile the application (`Ctrl+Shift+b`) but you get this error at runtime? – Only Bolivian Here Jul 08 '12 at 15:44
  • 1
    You are building code with methods that you aren't even aware of? Obviously there is method of that name that you need but don't have. – President James K. Polk Jul 08 '12 at 16:50
  • 1
    @GregS I think he has been using code found from somewhere that references a method but does not provide the source for said method. I have added a method that I think he needs as my answer let us see if it is what he requires. – Mr. Mr. Jul 09 '12 at 00:52

1 Answers1

2

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.

Mr. Mr.
  • 4,257
  • 3
  • 27
  • 42
  • thankyou so much for the kind help, my bad i was under the impression that it is some call to a builtin library function. thankyou :) – Maven Jul 10 '12 at 14:59