加密和解密

1. 用Asp.Net自帶方法實現Web.Config中字符串的加密和解密,代碼如下:

  //執行 加密操作
    protected void btnEncrypt_Click(object sender, EventArgs e)
    {
        //Configuration對象
        Configuration config = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
        //獲取配置節點
        ConfigurationSection section = config.GetSection("appSettings");
        //判斷節點是否等於空
        if (section != null && !section.SectionInformation.IsProtected)
        {
            //保護所指定的節點
            section.SectionInformation.ProtectSection("RsaProtectedConfigurationProvider");
            //保存
            config.Save();
            RegisterStartupScript("", "<script>alert('加密成功!')</script>");
        }
        Response.Write(ConfigurationManager.AppSettings["con"].ToString());
    }
    //執行解密操作
    protected void btnDecrypt_Click(object sender, EventArgs e)
    {
        Configuration config = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
        ConfigurationSection section = config.GetSection("appSettings");
        if (section != null && section.SectionInformation.IsProtected)
        {
            //移除保護指定的節點
            section.SectionInformation.UnprotectSection();
            config.Save();
            RegisterStartupScript("", "<script>alert('解密成功!')</script>");
        }
        Response.Write(ConfigurationManager.AppSettings["con"].ToString());
    }


 

發佈了26 篇原創文章 · 獲贊 3 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章