自定义c++ ASP . net会员登录

Custom C++ ASP .NET Membership Login

本文关键字:登录 net c++ ASP 自定义      更新时间:2023-10-16

有人知道如何使用ASP .NET会员提供的盐密钥来散列用户的密码吗?

我正在开发一个c++ Linux应用程序,我只能访问SQL Server。

谢谢,

下面是ASP使用的编码算法。Net会员,用c#编写。

它使用System.Security,所以如果你想在linux上运行,你可能想看看MONO。

注:我不熟悉MONO。

private string EncodePassword(string pass, int passwordFormat, string salt)
{
    if (passwordFormat == 0) // MembershipPasswordFormat.Clear
        return pass;
    byte[] bIn = Encoding.Unicode.GetBytes(pass);
    byte[] bSalt = Convert.FromBase64String(salt);
    byte[] bRet = null;
    if (passwordFormat == 1)
    { // MembershipPasswordFormat.Hashed
        HashAlgorithm hm = GetHashAlgorithm();
        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);
        }
    }
    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 = EncryptPassword(bAll, _LegacyPasswordCompatibilityMode);
    }
    return Convert.ToBase64String(bRet);
}
private string GenerateSalt()
{
    byte[] buf = new byte[SALT_SIZE];
    (new RNGCryptoServiceProvider()).GetBytes(buf);
    return Convert.ToBase64String(buf);
}