/// <summary> /// 加密串 /// </summary> /// <param name="buffer">密钥</param> /// <param name="len">长度=8</param> /// <param name="key">传入的明码密码</param> /// <returns></returns> public static string EncodeString(string buffer, int len, string key) { try { if (buffer == null) { throw new ArgumentNullException("buffer", "传入密钥为空"); } if (string.IsNullOrEmpty(key)) { key = string.Empty; } char[] pbuffer = new char[1024]; char[] pkey = new char[80]; HisEncryption encryption = new HisEncryption(); buffer.ToCharArray().CopyTo(pbuffer, 0); key.ToCharArray().CopyTo(pkey, 0); encryption.Secret(pbuffer, len, pkey); byte[] encryptBytes = CharArray2ByteArray(len, encryption); return(Convert.ToBase64String(encryptBytes)); } catch (Exception ex) { throw ex; } }
/// <summary> /// 加密字符串sql用,形式为Char(xx)+Char(xx)... /// </summary> /// <param name="buffer">密钥</param> /// <param name="len">长度=8</param> /// <param name="key">传入的明码密码</param> /// <returns></returns> public static string EncodeStringSql(string buffer, int len, string key) { if (buffer == null) { throw new ArgumentNullException("buffer", "传入密钥为空"); } if (string.IsNullOrEmpty(key)) { key = string.Empty; } char[] pbuffer = new char[1024]; char[] pkey = new char[80]; string s = ""; char[] ckey; HisEncryption encryption = new HisEncryption(); buffer.ToCharArray().CopyTo(pbuffer, 0); key.ToCharArray().CopyTo(pkey, 0); encryption.Secret(pbuffer, len, pkey); ckey = encryption.EncryptPassword.ToCharArray(); s = "char(" + Convert.ToByte(ckey[0]).ToString() + ")"; for (int i = 1; i < len; i++) { s = s + "+char(" + Convert.ToByte(ckey[i]).ToString() + ")"; } s = s + "+char(0)"; return(s); }
private static byte[] CharArray2ByteArray(int len, HisEncryption encryption) { byte[] ret = new byte[len]; for (int i = 0; i < len; i++) { ret[i] = Convert.ToByte(encryption.pass[i]); } return(ret); }
/// <summary> /// 验证密码是否正确 /// </summary> /// <param name="password">密码字符串</param> /// <returns>正确返回true,否则返回false</returns> public bool ComparePassword(string password) { try { string encryptPasswordBase64 = HisEncryption.EncodeString(_passwordKey, HisEncryption.PasswordLength, password); return(encryptPasswordBase64 == m_Password); } catch (Exception ex) { throw new Exception(ex.Message); } }
/// <summary> /// 改变用户密码 /// </summary> /// <param name="user">User对象</param> /// <param name="oldPassword">原密码</param> /// <param name="newPassword">新密码</param> public void ChangeUserPassword(Users user, string oldPassword, string newPassword) { try { if (user == null) { throw new ArgumentNullException("user", Resources.UserInfoIsNull); } if (user.ComparePassword(oldPassword)) { DateTime now = DateTime.Now; string encryptDateTime = now.ToString("yyyyMMdd") + now.ToString("T"); string encryptNewPassword = HisEncryption.EncodeString( encryptDateTime, HisEncryption.PasswordLength, newPassword); //***********************************Modified By wwj 2011-06-07************************************* //DataAccessFactory.DefaultDataAccess.ExecuteNoneQuery(string.Format(UpdateCZRYK, encryptNewPassword, encryptDateTime, user.ID)); SqlParameter[] sqlParam = new SqlParameter[] { new SqlParameter("@ID", SqlDbType.VarChar), new SqlParameter("@Passwd", SqlDbType.VarChar), new SqlParameter("@RegDate", SqlDbType.VarChar) }; sqlParam[0].Value = user.Id; sqlParam[1].Value = encryptNewPassword; sqlParam[2].Value = encryptDateTime; sql_helper.ExecuteNoneQuery("usp_UpdateUserPassword", sqlParam, CommandType.StoredProcedure); //*************************************************************************************************** user.Password = newPassword; user.PasswordKey = encryptNewPassword; } else { throw new InvalidUserPasswordException("原密码不正确"); } } catch (Exception ex) { throw new Exception(ex.Message); } }
/// <summary> /// 加密串 /// </summary> /// <param name="buffer">密钥</param> /// <param name="len">长度=8</param> /// <param name="key">传入的明码密码</param> /// <returns>返回字符数组</returns> public static byte[] EncodeString2ByteArray(string buffer, int len, string key) { if (buffer == null) { throw new ArgumentNullException("buffer", "传入密钥为空"); } if (string.IsNullOrEmpty(key)) { key = string.Empty; } char[] pbuffer = new char[1024]; char[] pkey = new char[80]; HisEncryption encryption = new HisEncryption(); buffer.ToCharArray().CopyTo(pbuffer, 0); key.ToCharArray().CopyTo(pkey, 0); encryption.Secret(pbuffer, len, pkey); return(CharArray2ByteArray(len, encryption)); }