本文整理汇总了C#中MD5CryptoServiceProvider类的典型用法代码示例。如果您正苦于以下问题:C# MD5CryptoServiceProvider类的具体用法?C# MD5CryptoServiceProvider怎么用?C# MD5CryptoServiceProvider使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
MD5CryptoServiceProvider类属于命名空间,在下文中一共展示了MD5CryptoServiceProvider类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetMd5Sum
public static string GetMd5Sum(string str)
{
// First we need to convert the string into bytes, which
// means using a text encoder.
Encoder enc = System.Text.Encoding.Unicode.GetEncoder();
// Create a buffer large enough to hold the string
byte[] unicodeText = new byte[str.Length * 2];
enc.GetBytes(str.ToCharArray(), 0, str.Length, unicodeText, 0, true);
// Now that we have a byte array we can ask the CSP to hash it
MD5 md5 = new MD5CryptoServiceProvider();
byte[] result = md5.ComputeHash(unicodeText);
// Build the final string by converting each byte
// into hex and appending it to a StringBuilder
StringBuilder sb = new StringBuilder();
for (int i = 0; i < result.Length; i++)
{
sb.Append(result[i].ToString("X2"));
}
// And return it
return sb.ToString();
}
示例2: In
public void In(
[FriendlyName("Key", "The string to be used to generate the hash from.")]
string Key,
[FriendlyName("MD5 Hash", "The MD5 Hash generated by the Key.")]
out string Hash
)
{
if (Key != "")
{
UTF8Encoding ue = new UTF8Encoding();
byte[] bytes = ue.GetBytes(Key);
// encrypt bytes
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
byte[] hashBytes = md5.ComputeHash(bytes);
// Convert the encrypted bytes back to a string (base 16)
string tmpHash = "";
for (int i = 0; i < hashBytes.Length; i++)
{
tmpHash += System.Convert.ToString(hashBytes[i], 16).PadLeft(2, '0');
}
Hash = tmpHash.PadLeft(32, '0');
}
else
{
uScriptDebug.Log("[Generate MD5 Hash] The Key provided was empty, returning an empty string for the MD5 Hash.", uScriptDebug.Type.Warning);
Hash = "";
}
}
示例3: Encrypt
public string Encrypt(string ToEncrypt, bool useHasing)
{
byte[] keyArray;
byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(ToEncrypt);
string Key = "malkit";
if (useHasing)
{
MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(Key));
hashmd5.Clear();
}
else
{
keyArray = UTF8Encoding.UTF8.GetBytes(Key);
}
TripleDESCryptoServiceProvider tDes = new TripleDESCryptoServiceProvider();
tDes.Key = keyArray;
tDes.Mode = CipherMode.ECB;
tDes.Padding = PaddingMode.PKCS7;
ICryptoTransform cTransform = tDes.CreateEncryptor();
byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
tDes.Clear();
return Convert.ToBase64String(resultArray, 0, resultArray.Length);
}
示例4: Decrypt
public string Decrypt(string cypherString, bool useHasing)
{
byte[] keyArray;
byte[] toDecryptArray = Convert.FromBase64String(cypherString.Replace(' ','+'));
string key = "malkit";
if (useHasing)
{
MD5CryptoServiceProvider hashmd = new MD5CryptoServiceProvider();
keyArray = hashmd.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
hashmd.Clear();
}
else
{
keyArray = UTF8Encoding.UTF8.GetBytes(key);
}
TripleDESCryptoServiceProvider tDes = new TripleDESCryptoServiceProvider();
tDes.Key = keyArray;
tDes.Mode = CipherMode.ECB;
tDes.Padding = PaddingMode.PKCS7;
ICryptoTransform cTransform = tDes.CreateDecryptor();
try
{
byte[] resultArray = cTransform.TransformFinalBlock(toDecryptArray, 0, toDecryptArray.Length);
tDes.Clear();
return UTF8Encoding.UTF8.GetString(resultArray, 0, resultArray.Length);
}
catch (Exception ex)
{
throw ex;
}
}
示例5: MD5Encode
/// <summary>
/// MD5加密
/// </summary>
/// <param name="strOriginal"></param>
/// <returns></returns>
public static string MD5Encode(string strOriginal)
{
byte[] byteInput = UTF8Encoding.UTF8.GetBytes(strOriginal);
MD5CryptoServiceProvider objMD5 = new MD5CryptoServiceProvider();
byte[] byteOutput = objMD5.ComputeHash(byteInput);
return BitConverter.ToString(byteOutput).Replace("-", "");
}
示例6: ComputeMD5
public static string ComputeMD5(string value)
{
/*
* Create the md5 crypt service provider
*/
MD5 crypt = new MD5CryptoServiceProvider();
StreamWriter pwd = new StreamWriter(new MemoryStream());
pwd.Write(value);
pwd.Flush();
/*
* Compute the hash code
*/
pwd.BaseStream.Seek(0, SeekOrigin.Begin);
byte[] cryptHash = crypt.ComputeHash(pwd.BaseStream);
/*
* Convert the result to hex
*/
string result = "";
int nLen = cryptHash.Length;
for (int nPos = 0; nPos < nLen; nPos++)
{
byte cBuff = cryptHash[nPos];
result += Convert((long)cBuff, 16);
}
return result.ToLower();
}
示例7: LogIn
protected void LogIn(object sender, EventArgs e)
{
// Colecting data
string email = TBEmail.Text;
string password = TBPass.Text;
// Hashing password
MD5 md5 = new MD5CryptoServiceProvider();
md5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(password));
password = Convert.ToBase64String(md5.Hash);
// Checking login details
Staff account = DBConnectivity.login(email, password);
if (account != null)
{
// Loging OK
Session["auth_email"] = account.email;
Session["first_name"] = account.firstName;
Session["access"] = account.access;
Response.Redirect("~/Manage-Pets");
}
else
{
// Bad details
ErrorMessage.Visible = true;
FailureText.Text = "Bad credentials. Try again.";
}
}
示例8: Encrypt
public static string Encrypt(string toEncrypt, bool useHashing)
{
byte[] keyArray;
byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt);
System.Configuration.AppSettingsReader settingsReader = new AppSettingsReader();
string key = (string)settingsReader.GetValue("search", typeof(string));
if (useHashing)
{
MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
hashmd5.Clear();
}
else
keyArray = UTF8Encoding.UTF8.GetBytes(key);
TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
tdes.Key = keyArray;
tdes.Mode = CipherMode.ECB;
tdes.Padding = PaddingMode.PKCS7;
ICryptoTransform cTransform = tdes.CreateEncryptor();
byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
tdes.Clear();
return Convert.ToBase64String(resultArray, 0, resultArray.Length);
}
示例9: Decrypt
/// <summary>
/// Decrypt the given string using the specified key.
/// </summary>
/// <param name="strEncrypted">The string to be decrypted.</param>
/// <param name="strKey">The decryption key.</param>
/// <returns>The decrypted string.</returns>
public static string Decrypt(string strEncrypted, string strKey)
{
try
{
TripleDESCryptoServiceProvider objDESCrypto = new TripleDESCryptoServiceProvider();
MD5CryptoServiceProvider objHashMD5 = new MD5CryptoServiceProvider();
byte[] byteHash, byteBuff;
string strTempKey = strKey;
byteHash = objHashMD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(strTempKey));
objHashMD5 = null;
objDESCrypto.Key = byteHash;
objDESCrypto.Mode = CipherMode.ECB; //CBC, CFB
byteBuff = Convert.FromBase64String(strEncrypted);
string strDecrypted = ASCIIEncoding.ASCII.GetString(objDESCrypto.CreateDecryptor().TransformFinalBlock(byteBuff, 0, byteBuff.Length));
objDESCrypto = null;
return strDecrypted;
}
catch (Exception ex)
{
return "Wrong Input. " + ex.Message;
}
}
示例10: In
public void In(
[FriendlyName("Key", "The string to be used to check against the provided MD5 hash.")]
string Key,
[FriendlyName("MD5 Hash", "The MD5 Hash to check the key against.")]
string Hash
)
{
if (Key != "" && Hash != "")
{
UTF8Encoding ue = new UTF8Encoding();
byte[] bytes = ue.GetBytes(Key);
// encrypt bytes
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
byte[] hashBytes = md5.ComputeHash(bytes);
// Convert the encrypted bytes back to a string (base 16)
string tmpHash = "";
for (int i = 0; i < hashBytes.Length; i++)
{
tmpHash += System.Convert.ToString(hashBytes[i], 16).PadLeft(2, '0');
}
string finalHash = tmpHash.PadLeft(32, '0');
if (finalHash == Hash)
{
m_GoodHash = true;
}
}
}
示例11: Encrypt
public static string Encrypt(string plainText )
{
string encrypted = null;
try
{
byte[] inputBytes = ASCIIEncoding.ASCII.GetBytes(plainText);
byte[] pwdhash = null;
MD5CryptoServiceProvider hashmd5;
//generate an MD5 hash from the password.
//a hash is a one way encryption meaning once you generate
//the hash, you cant derive the password back from it.
hashmd5 = new MD5CryptoServiceProvider();
pwdhash = hashmd5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(key));
hashmd5 = null;
// Create a new TripleDES service provider
TripleDESCryptoServiceProvider tdesProvider = new TripleDESCryptoServiceProvider();
tdesProvider.Key = pwdhash;
tdesProvider.Mode = CipherMode.ECB;
byte [] edata = tdesProvider.CreateEncryptor().TransformFinalBlock(inputBytes, 0, inputBytes.Length);
encrypted = Convert.ToBase64String( edata );
}
catch(Exception e)
{
string str = e.Message;
throw ;
}
return encrypted;
}
示例12: md5method
public static string md5method(string textoPlano)
{
byte[] data = System.Text.UTF8Encoding.ASCII.GetBytes(textoPlano);
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
byte[] hashbyte = md5.ComputeHash(data, 0, data.Length);
return BitConverter.ToString(hashbyte);
}
示例13: Com
private static string Com(Stream stream)
{
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
md5.ComputeHash(stream);
byte[] hash = md5.Hash;
if (hash == null)
{
_ecode = MD5ErrorCode.E_ComputeHash;
return string.Empty;
}
StringBuilder sb = new StringBuilder();
foreach (byte byt in hash)
{
sb.Append(String.Format("{0:X1}", byt));
}
string ret = sb.ToString();
sb = null;
return ret;
}
示例14: MD5
public static string MD5(string text)
{
string md5Hash;
StringBuilder sb;
byte[] textBytes;
byte[] encodedBytes;
MD5CryptoServiceProvider md5Provider;
if (text == null) {
throw new ArgumentNullException("text");
}
sb = new StringBuilder();
md5Provider = new MD5CryptoServiceProvider();
textBytes = ASCIIEncoding.Default.GetBytes(text);
encodedBytes = md5Provider.ComputeHash(textBytes);
for (int i = 0; i < encodedBytes.Length; i++) {
sb.AppendFormat("{0:x2}", encodedBytes[i]);
}
md5Hash = sb.ToString();
return md5Hash;
}
示例15: HMACMD5
public HMACMD5 (byte[] key) {
m_hashName = "MD5";
m_hash1 = new MD5CryptoServiceProvider();
m_hash2 = new MD5CryptoServiceProvider();
HashSizeValue = 128;
base.InitializeKey(key);
}