<%@ Page Language="C#" %>
<%@ Import Namespace="System.Web.Configuration"%>
<script runat="server">
	protected void btnCripto_Click(object sender, EventArgs e) {
		try {
			System.Configuration.Configuration config = 
			WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
			config.ConnectionStrings.SectionInformation.ProtectSection(
			"DataProtectionConfigurationProvider");
			config.Save();
			btnDesCripto.Enabled = true;
			btnCripto.Enabled = false;
		}
		catch(ConfigurationErrorsException ex) {
			Response.Write(ex.Message);
		}
	}
	protected void btnDesCripto_Click(object sender, EventArgs e) {
		try {
			System.Configuration.Configuration config = 
			WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
			config.ConnectionStrings.SectionInformation.UnprotectSection();
			config.Save();
			btnDesCripto.Enabled = false;
			btnCripto.Enabled = true;
		}
		catch (ConfigurationErrorsException ex) {
			Response.Write(ex.Message);
		}
	}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
	<head runat="server">
		<title>Criptografa elementos do Web.config</title>
	</head>
	<body>
		<form id="form1" runat="server">
			<div>
			<asp:Button ID="btnCripto" runat="server" OnClick="btnCripto_Click"
			Text="Criptografar" />
			<asp:Button ID="btnDesCripto" runat="server" Enabled="False" 
				Text="Descriptografar" OnClick="btnDesCripto_Click" />
			</div>
		</form>
	</body>
</html>

