3

I want to have a Encryption using SHA1. My Code is

public static string EncryptPassword(string password)
{
    try
    {
        SHA1 sha1 = new SHA1Managed();
        var bytehash = sha1.ComputeHash(new MemoryStream(new ASCIIEncoding().GetBytes(password)));
        var stringhash = new ASCIIEncoding().GetChars(bytehash).ToString();

        return stringhash;
    }
    catch (Exception ex)
    {
        // Some Exception....
    }

    return null;
}

It's not working. It only return System.Char[]. What am I doing wrong in this

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Jain
  • 126
  • 1
  • 12
  • 2
    You need to express your hash as a hex string, right? http://stackoverflow.com/questions/311165/how-do-you-convert-byte-array-to-hexadecimal-string-and-vice-versa – spender Jul 04 '14 at 09:01
  • The normal way of converting a byte array of non-character data to a string is to express it as a hex string. Treating bytes as ascii is inherently lossy and the wrong way to solve this problem. – spender Jul 04 '14 at 09:14
  • @spender: Please post your comment as an answer since the accepted answer is incorrect. – President James K. Polk Jul 04 '14 at 12:39
  • @GregS No. The question is incorrect. Until OP accepts this to be the case, I am leaving notes as comments. I would prefer to close as a dupe rather than repeating the same info of the linked answers, but that also doesn't seem quite appropriate yet. – spender Jul 04 '14 at 12:42
  • Com'on guys!!!!! I asked a specific question and got a specific answer. How the question is dulplicate???? – Jain Jul 04 '14 at 17:01
  • @Jain : the reason that this question is problematic is that you are attempting to do something that is fundamentally broken by design. As I've stated before, the correct way to turn a byte array into a string is either as a hex string (or even a base64 encoded string). Your method here will create garbage. If that's truly what you want, then fine, but it's not very likely, right? – spender Jul 08 '14 at 22:54

2 Answers2

5

Because that's what ToString() returns from an array of chars...

try

new string(new ASCIIEncoding().GetChars(bytehash));

and choose Maurice's answer, which is smarter ;)

Raphaël Althaus
  • 59,727
  • 6
  • 96
  • 122
  • ...but a binary hash isn't ascii... what's this meant to do? make a garbage string? – spender Jul 04 '14 at 08:58
  • @jain ascii encoding is values from 0 to 127, but the hash is bytes with value from 0 to 255. The idea of directly converting a hash to ascii is broken by design and you will lose information by doing this. – spender Jul 04 '14 at 09:08
  • @spender: Currently I used to do Encryption using MachineKey Class. Would like to ask: Which Encryption Method is really good in terms of performance and security? – Jain Jul 04 '14 at 09:18
3

Use GetString instead of GetChars

var stringhash = new ASCIIEncoding().GetString(bytehash);

However Spender wrote you a comment on your question with a link to another question that will help you resolve your actual problem. (@Spender thanks for this).

Myrtle
  • 5,761
  • 33
  • 47
  • Same issue. The hash will contain values that aren't ascii. What's this actually going to do? – spender Jul 04 '14 at 09:02
  • @spender I agree it is a bit useless; however this answer does what he wants; converting the bytes to an useless string. – Myrtle Jul 04 '14 at 09:08