Windows Phone如何实现md5加密?
我已经添加了using System.Security.Cryptographyd;这句代码,可还是提示
未能找到类型或命名空间名称“MD5CryptoServiceProvider”(是否缺少 using 指令或程序集引用?)
[最优解释]
public class MD5CryptoServiceProvider : MD5
{
public MD5CryptoServiceProvider() : base()
{
}
}
/// <summary>
/// Summary description for MD5.
/// </summary>
public class MD5 : IDisposable
{
static public MD5 Create(string hashName)
{
if (hashName == "MD5")
return new MD5();
else
throw new NotSupportedException();
}
static public String GetMd5String(String source)
{
MD5 md = MD5CryptoServiceProvider.Create();
byte[] hash;
//Create a new instance of ASCIIEncoding to
//convert the string into an array of Unicode bytes.
UTF8Encoding enc = new UTF8Encoding();
// ASCIIEncoding enc = new ASCIIEncoding();
//Convert the string into an array of bytes.
byte[] buffer = enc.GetBytes(source);
//Create the hash value from the array of bytes.
hash = md.ComputeHash(buffer);
StringBuilder sb = new StringBuilder();
foreach (byte b in hash)
sb.Append(b.ToString("x2"));
return sb.ToString();
}
static public byte[] GetMd5ByteArray(String source)
{
MD5 md = MD5CryptoServiceProvider.Create();
byte[] hash;
//Create a new instance of ASCIIEncoding to
//convert the string into an array of Unicode bytes.
UTF8Encoding enc = new UTF8Encoding();
// ASCIIEncoding enc = new ASCIIEncoding();
//Convert the string into an array of bytes.
byte[] buffer = enc.GetBytes(source);
//Create the hash value from the array of bytes.
hash = md.ComputeHash(buffer);
return hash;
}
static public MD5 Create()
{
return new MD5();
}