C# 名称';A连续编码';在当前上下文中不存在

C# 名称';A连续编码';在当前上下文中不存在,c#,sql,webmatrix,C#,Sql,Webmatrix,我正在尝试加密字符串以将其保存在sql server数据库中,但显示“名称'ascienceoding'在当前上下文中不存在”。如何在ASP.Net网页框架中加密字符串?以下是我的代码: @using System.Security.Cryptography; @{ String s = "hello"; String s2 = "hello"; s = Encrypt(s,_key); s2 = Encrypt(s2, _key); } @functions{ private static

我正在尝试加密字符串以将其保存在sql server数据库中,但显示“名称'ascienceoding'在当前上下文中不存在”。如何在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;
    }
}
}

ascienceoding
类包含在
System.Text
命名空间中。您需要做以下两件事之一:

  • 为命名空间添加
    using
    语句(如
    using System.Text
  • 完全限定类名(如
    byteBuff=System.Text.ascienceoding.ASCII.GetBytes(strotencrypt);

查找其他信息。

使用语句,用
引用
系统文本

using System.Text ;
或完全限定参考:

System.Text.ASCIIEncoding
但是您应该意识到,
System.Text.ascienceoding.ASCII
是……冗余的。就说

`System.Text.Encoding.ASCII`