Password encryption/decryption code in .NET

后端 未结 9 1064
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-29 00:37

I want simple encryption and decryption of password in C#. How to save the password in encrypted format in database and retrieve as original format by decryption?

9条回答
  •  误落风尘
    2020-11-29 01:32

    You can use the managed .Net cryptography library, then save the encrypted string into the database. When you want to verify the password you can compare the stored database string with the hashed value of the user input. See here for more info about SHA512Managed

    using System.Security.Cryptography;

        public static string EncryptSHA512Managed(string password)
        {
            UnicodeEncoding uEncode = new UnicodeEncoding();
            byte[] bytPassword = uEncode.GetBytes(password);
            SHA512Managed sha = new SHA512Managed();
            byte[] hash = sha.ComputeHash(bytPassword);
            return Convert.ToBase64String(hash);
        }
    

提交回复
热议问题