积极答复者
请问ASPNET中membership的默认SHA1算法是怎样的?

问题
-
为什么我用SHA1算法算出来的密码与SQL Server 数据库中的密码不一样,是我的算法有问题吗?
Public Function EncodePassword(ByVal hashAlgorithmType As String, ByVal password As String, ByVal salt As String) As String
Dim passwordBuf() As Byte = System.Text.Encoding.Unicode.GetBytes(password)
Dim saltBuf() As Byte = Convert.FromBase64String(salt)Dim buf() As Byte = New Byte(passwordBuf.Length + saltBuf.Length) {}
System.Buffer.BlockCopy(saltBuf, 0, buf, 0, saltBuf.Length)
System.Buffer.BlockCopy(passwordBuf, 0, buf, saltBuf.Length, passwordBuf.Length)Dim hashAlgorithm As HashAlgorithm = hashAlgorithm.Create(hashAlgorithmType)
buf = hashAlgorithm.ComputeHash(buf)
Return Convert.ToBase64String(buf)
End Function该算法算出来的密码是:3ZaOkAda+f0qxNuSRfqNjCUqy3w=
而我数据库的存储数据是:
密码:111111
passwordsalt:gZvD+nDjPxgUZG6cDKjuxA==
password:RpBNi3kU5rF9iGR2JIloAxq7aJs=
这是为什么呢?难道Membership的默认SHA1算法不是这样的吗?- 已编辑 Jackzhu 2009年9月20日 14:24 写错了
答案
-
我终于知道原因了
原来加密时,password和passwordsalt在组合时,应该在字符串string时就开始组合了,而不是原来我以为得先化为byte字符数组再连接。
下面是该加密算法:
谢谢大家的鼎力帮助!太兴奋了!
Public Function EncodePassword(ByVal pass As String, ByVal passwordFormat As Integer, ByVal salt As String) As String
If passwordFormat = 0 Then
Return passEnd If
Dim src() As Byte = Convert.FromBase64String(salt)
Dim inArray() As Byte = NothingIf (passwordFormat = 1) Then
Dim hashAlgorithm As HashAlgorithm = hashAlgorithm.Create("SHA1")
If ((hashAlgorithm Is Nothing)) Then
Throw New Exception()
End If
inArray = hashAlgorithm.ComputeHash(Encoding.Unicode.GetBytes((Encoding.Unicode.GetString(src) & pass)))
Else
End If
Return Convert.ToBase64String(inArray)End Function
- 已标记为答案 Jackzhu 2009年9月22日 13:43
全部回复
-
public string EncodePassword(string pass, string saltBase64)
{
byte[] bytes = Encoding.Unicode.GetBytes(pass);
byte[] src = Convert.FromBase64String(saltBase64);
byte[] dst = new byte[src.Length + bytes.Length];
Buffer.BlockCopy(src, 0, dst, 0, src.Length);
Buffer.BlockCopy(bytes, 0, dst, src.Length, bytes.Length);
HashAlgorithm algorithm = HashAlgorithm.Create("SHA1");
byte[] inArray = algorithm.ComputeHash(dst);
return Convert.ToBase64String(inArray);
} -
为什么我用这个计算得到的数据是正确的?
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Security.Cryptography; namespace ConsoleApplication12 { class Program { static void Main(string[] args) { Console.Write(EncodePassword("11111",1,"+5ckXA1F1JiNEjQCk2BVWQ==")); Console.ReadKey (); } static internal string EncodePassword(string pass, int passwordFormat, string salt) { if (passwordFormat == 0) { return pass; } byte[] bytes = Encoding.Unicode.GetBytes(pass); byte[] src = Convert.FromBase64String(salt); byte[] dst = new byte[src.Length + bytes.Length]; byte[] inArray = null; Buffer.BlockCopy(src, 0, dst, 0, src.Length); Buffer.BlockCopy(bytes, 0, dst, src.Length, bytes.Length); if (passwordFormat == 1) { HashAlgorithm algorithm = HashAlgorithm.Create("SHA1"); if ((algorithm == null) ) { throw new Exception(); } inArray = algorithm.ComputeHash(dst); } else { } return Convert.ToBase64String(inArray); } } }
恭喜自己5星用户达成 -
Public Function EncodePassword(ByVal pass As String, ByVal passwordFormat As Integer, ByVal salt As String) As String
If passwordFormat = 0 Then
Return passEnd If
Dim bytes() As Byte = Encoding.Unicode.GetBytes(pass)
Dim src() As Byte = Convert.FromBase64String(salt)
Dim dst() As Byte = New Byte(src.Length + bytes.Length) {}
Dim inArray() As Byte = Nothing
System.Buffer.BlockCopy(src, 0, dst, 0, src.Length)
System.Buffer.BlockCopy(bytes, 0, dst, src.Length, bytes.Length)
If (passwordFormat = 1) Then
Dim hashAlgorithm As HashAlgorithm = hashAlgorithm.Create("SHA1")
If ((hashAlgorithm Is Nothing)) Then
Throw New Exception()
End If
inArray = hashAlgorithm.ComputeHash(dst)
Else
End If
Return Convert.ToBase64String(inArray)End Function
我的算法,我的测试是这样的
pwd
12345
salt
+5ckXA1F1JiNEjQCk2BVWQ==
resualt
kaVrUabOdCbig41WntjAnh1+9eE=
pwd
123456
salt
nH8F3pqVqtrerjNeAiYYKA==
resualt
psvbUPnujDUSLtW1P7JFOGM+Gh0=
奇了怪了 -
password passwordformat passwordsalt
u/SzRDK/f0yYQKl3N+Zsp4jKdAk= 1 nrFpCbjto1Df/2gjaLccIQ== 2R24hmNGvFYb5MMZDEfLAey8Xjw= 1 SrIKBmCqyo9ATB5SSMRrfQ== JcFFqQUU1YttoZyXmKwnw/gmwjs= 1 UlQiym0+3NTybZ801MPUsw==
以上是我数据库中的数据,密码都是:123456
可我通过
Public Function EncodePassword(ByVal pass As String, ByVal passwordFormat As Integer, ByVal salt As String) As String
If passwordFormat = 0 Then
Return passEnd If
Dim bytes() As Byte = Encoding.Unicode.GetBytes(pass)
Dim src() As Byte = Convert.FromBase64String(salt)
Dim dst() As Byte = New Byte(src.Length + bytes.Length) {}
Dim inArray() As Byte = Nothing
System.Buffer.BlockCopy(src, 0, dst, 0, src.Length)
System.Buffer.BlockCopy(bytes, 0, dst, src.Length, bytes.Length)
If (passwordFormat = 1) Then
Dim hashAlgorithm As HashAlgorithm = hashAlgorithm.Create("SHA1")
If ((hashAlgorithm Is Nothing)) Then
Throw New Exception()
End If
inArray = hashAlgorithm.ComputeHash(dst)
Else
End If
Return Convert.ToBase64String(inArray)End Function
该算法算出来的结果是:
bR77+Rsin07so5oaJxw4FWVOTsM= nrFpCbjto1Df/2gjaLccIQ==
kJAnthcV0qQdNSFAGyyjuSOt1rw= SrIKBmCqyo9ATB5SSMRrfQ==
DL7UCwTPJN68SDcBbBMkPu8vfWw= UlQiym0+3NTybZ801MPUsw== -
我终于知道原因了
原来加密时,password和passwordsalt在组合时,应该在字符串string时就开始组合了,而不是原来我以为得先化为byte字符数组再连接。
下面是该加密算法:
谢谢大家的鼎力帮助!太兴奋了!
Public Function EncodePassword(ByVal pass As String, ByVal passwordFormat As Integer, ByVal salt As String) As String
If passwordFormat = 0 Then
Return passEnd If
Dim src() As Byte = Convert.FromBase64String(salt)
Dim inArray() As Byte = NothingIf (passwordFormat = 1) Then
Dim hashAlgorithm As HashAlgorithm = hashAlgorithm.Create("SHA1")
If ((hashAlgorithm Is Nothing)) Then
Throw New Exception()
End If
inArray = hashAlgorithm.ComputeHash(Encoding.Unicode.GetBytes((Encoding.Unicode.GetString(src) & pass)))
Else
End If
Return Convert.ToBase64String(inArray)End Function
- 已标记为答案 Jackzhu 2009年9月22日 13:43