0

While learning C# in school we needed to make a username-password system, for some extra points I wanted to use MD5 encryption, but after some research I found out usual md5 codes were easy to decipher. I wanted to make it a bit harder so wanted to add some strings to the begin and end of the string that was going to be encrypted. Problem is that I'm getting the "not all code paths return a value" and not sure how to fix it. (sorry for the noob question) The MD5 part works but I don't know why the encrypt part doesn't.

private string encrypt(string toEncrypt)
    {
        string step1 = MD5(toEncrypt + "example");
        string step2 = MD5("example" + step1);
        string final = MD5("example" + step2 + "example");
    }

public string MD5(string input)
    {
        //use string hash = MD5('STRING');
        //to call md5 encryption
        MD5 md5 = System.Security.Cryptography.MD5.Create();
        byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(input);
        byte[] hash = md5.ComputeHash(inputBytes);
        StringBuilder sb = new StringBuilder();
        for(int i=0; i < hash.Length; i++)
        {
            sb.Append(hash[i].ToString("x2"));
        }
        return sb.ToString();

    }
1
  • 1
    You should definitely consider an IDE like visual studio free or monodevelop while learning--they can often point out exactly where simple problems like this are occuring. Commented Sep 23, 2015 at 21:29

2 Answers 2

3

You forgot to return the final string.

private string encrypt(string toEncrypt)
{
    string step1 = MD5(toEncrypt + "example");
    string step2 = MD5("example" + step1);
    string final = MD5("example" + step2 + "example");

    return final;
}
Sign up to request clarification or add additional context in comments.

Comments

0

In your encrypt method you must return the final string.

return final;

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.