Exemplo n.º 1
0
        /// <summary>
        /// 密码解密
        /// </summary>
        /// <param name="pass"></param>
        /// <param name="passwordFormat">枚举PasswordFormat</param>
        /// <param name="salt"></param>
        /// <returns></returns>
        public static string UnEncodePassword(string pass, PasswordFormatOptions formatOptions, string salt)
        {
            switch (formatOptions)
            {
            case PasswordFormatOptions.Clear:     // Clear:
                return(pass);

            case PasswordFormatOptions.Hashed:     //Hashed:
                throw new ArgumentException("UnEncodePassword.Hashed");

            case PasswordFormatOptions.Aes:
                var aes = new AESEncrypt();
                return(aes.Decrypt(pass, salt));

            default:
                return(pass);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// 密码加密
        /// </summary>
        /// <param name="pass"></param>
        /// <param name="passwordFormat">枚举PasswordFormat</param>
        /// <param name="salt"></param>
        /// <returns></returns>
        public static string EncodePassword(string password, PasswordFormatOptions formatOptions, string salt)
        {
            if (formatOptions == PasswordFormatOptions.Clear)
            {
                return(password);
            }

            if (formatOptions == PasswordFormatOptions.Aes)
            {
                var aes = new AESEncrypt();
                return(aes.Encrypt(password, salt));
            }

            var hm = GetHashAlgorithm(formatOptions);

            //Hashed:不可逆,不能解密
            if (formatOptions == PasswordFormatOptions.Hashed)
            {
                byte[] bIn   = Encoding.Unicode.GetBytes(password);
                byte[] bSalt = Convert.FromBase64String(salt);
                byte[] bRet  = null;
                if (hm is KeyedHashAlgorithm)
                {
                    KeyedHashAlgorithm kha = (KeyedHashAlgorithm)hm;
                    if (kha.Key.Length == bSalt.Length)
                    {
                        kha.Key = bSalt;
                    }
                    else if (kha.Key.Length < bSalt.Length)
                    {
                        byte[] bKey = new byte[kha.Key.Length];
                        Buffer.BlockCopy(bSalt, 0, bKey, 0, bKey.Length);
                        kha.Key = bKey;
                    }
                    else
                    {
                        byte[] bKey = new byte[kha.Key.Length];
                        for (int iter = 0; iter < bKey.Length;)
                        {
                            int len = Math.Min(bSalt.Length, bKey.Length - iter);
                            Buffer.BlockCopy(bSalt, 0, bKey, iter, len);
                            iter += len;
                        }
                        kha.Key = bKey;
                    }
                    bRet = kha.ComputeHash(bIn);
                }
                else
                {
                    byte[] bAll = new byte[bSalt.Length + bIn.Length];
                    Buffer.BlockCopy(bSalt, 0, bAll, 0, bSalt.Length);
                    Buffer.BlockCopy(bIn, 0, bAll, bSalt.Length, bIn.Length);
                    bRet = hm.ComputeHash(bAll);
                }

                return(Convert.ToBase64String(bRet));
            }
            else
            {
                return(Convert.ToBase64String(hm.ComputeHash(Encoding.UTF8.GetBytes(password))));
            }
        }