|
Components/Enumerations/SecurityEnums.cs中 public enum UserPasswordFormat { ClearText = 0, MD5Hash = 1, Sha1Hash = 2, Encyrpted = 3, DVMD5 = 4//增加的 by waterboy } Components/Users.cs
public static string Encrypt(UserPasswordFormat format, string cleanString, string salt) { Byte[] clearBytes; Byte[] hashedBytes; System.Text.Encoding encoding = System.Text.Encoding.GetEncoding( AspNetForums.Configuration.ForumConfiguration.GetConfig().PasswordEncodingFormat ); if( encoding == null ) { throw new AspNetForums.Components.ForumException( ForumExceptionType.UnknownError, "An unknown encoding type (" + AspNetForums.Configuration.ForumConfiguration.GetConfig().PasswordEncodingFormat + ") was specified in the web config file for the property 'passwordEncodingFormat'"); } //clearBytes = encoding.GetBytes( salt.ToLower().Trim() + cleanString.ToLower().Trim() ); // fix to not force to lowercase, stronger encryption. // clearBytes = encoding.GetBytes( salt.ToLower().Trim() + cleanString.Trim() ); switch (format) { case UserPasswordFormat.ClearText: return cleanString; case UserPasswordFormat.Sha1Hash: // Force the string to lower case and add the salt // // clearBytes = encoding.GetBytes(salt.Length == 0 ? cleanString : salt + cleanString ); hashedBytes = ((HashAlgorithm) CryptoConfig.CreateFromName("SHA1")).ComputeHash(clearBytes); return BitConverter.ToString(hashedBytes); //return Convert.ToBase64String(hashedBytes); case UserPasswordFormat.DVMD5://增加的 by waterboy return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(cleanString,"md5").ToLower().Substring(8,16);//增加的 case UserPasswordFormat.MD5Hash: case UserPasswordFormat.Encyrpted: default: // TDD 3/16/2004 // This algorithm was changed to UTF8 which is not compatible with the existing passwords aleady stored // so I'm changing it back to use Unicode encoding like it was originally written with the addition of salt. // Force the string to lower case and add the salt // clearBytes = System.Text.Encoding.UTF8.GetBytes(salt != null && salt != String.Empty ? salt.ToLower().Trim() + cleanString.ToLower().Trim() : cleanString.ToLower().Trim() ); // hashedBytes = ((HashAlgorithm) CryptoConfig.CreateFromName("MD5")).ComputeHash(clearBytes); // clearBytes = encoding.GetBytes(salt == null ? cleanString.ToLower() : salt.ToLower().Trim() + cleanString.ToLower().Trim() ); hashedBytes = ((HashAlgorithm) CryptoConfig.CreateFromName("MD5")).ComputeHash(clearBytes); return BitConverter.ToString(hashedBytes); //return Convert.ToBase64String(hashedBytes); } }
只需要注意注释“增加的”
我已经测试通过。
|