我正尝试加密一个字符串以将其保存在sql server数据库中,但显示"The name 'ASCIIEncoding‘it not exist in the current context“。如何在ASP.Net网页框架中加密字符串?以下是我的代码:
@using System.Security.Cryptography;
@{
String s = "hello";
String s2 = "hello";
s = Encrypt(s,_key);
s2 = Encrypt(s2, _key);
}
@functions{
private static readonly byte[] _key = "myVeryStrongPsw";
public static string Encrypt(string strToEncrypt, 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 = ASCIIEncoding.ASCII.GetBytes(strToEncrypt);
return Convert.ToBase64String(objDESCrypto.CreateEncryptor().
TransformFinalBlock(byteBuff, 0, byteBuff.Length));
}
catch (Exception ex)
{
return "Wrong Input. " + ex.Message;
}
}
}发布于 2014-07-02 04:07:39
使用using语句引用System.Text
using System.Text ;或完全限定引用:
System.Text.ASCIIEncoding但你应该意识到System.Text.ASCIIEncoding.ASCII is...redundant。就说
`System.Text.Encoding.ASCII`发布于 2014-07-02 04:05:16
ASCIIEncoding类包含在System.Text命名空间中。您需要做以下两件事之一:
在using System.Text)
using语句来限定类名(如在byteBuff = System.Text.ASCIIEncoding.ASCII.GetBytes(strToEncrypt);)中
有关更多信息,请查看here。
https://stackoverflow.com/questions/24518341
复制相似问题