简体   繁体   中英

Encryption & Decryption

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

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 ;)

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM