<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="TestXEncFF.aspx.vb" Inherits="TestWebApp.TestXEncFF" ValidateRequest="false"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Lizard Labs</title>
</head>
<body>
    <form id="form1" runat="server">
    <script language=javascript>

    function installXEncXpi() { xpi={'application/xenclite':'./LizardLabsXEncLiteFireFox.xpi'}; InstallTrigger.install(xpi); } 

        function testencrypt() 
        {
            try 
            {
			netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");

			const cid = "@lizardl.com/xenclite;1";
		
			var oXEncXmlEncryptionComponent = Components.classes[cid].createInstance();
			
			if ( oXEncXmlEncryptionComponent == null)
			{
			    alert("Error loading XEnc Lite for Firefox");
			    return false;
			}
			
			oXEncXmlEncryptionComponent = oXEncXmlEncryptionComponent.QueryInterface(Components.interfaces.ILizardLabsXEncLiteFireFox);

                //oXEncXmlEncryptionComponent.Test("Test"); // test if control is loaded

                alert(document.getElementById("hiddenXml").value);

                oXEncXmlEncryptionComponent.UseLocalMachineStore = false; // Use users store (default)
                oXEncXmlEncryptionComponent.SystemSotreName = "My"; // Use persobnal store (default). Other possible values: My, Root, CA, OtherPeople...

                oXEncXmlEncryptionComponent.SelectCertificate();
                //oSignComponent.SelectCertificateByThumbprint("ae 3c 32 52 79 a8 eb 1d fd 8c ae 90 80 0f e2 57 7a d4 f2 6f");

                if (oXEncXmlEncryptionComponent.IsCertificateSelected) {
                    if (oXEncXmlEncryptionComponent.Encrypt(document.getElementById("hiddenXml").value, "PaymentRq")) // Parameters: XML, ElementName (if you want to encrypt specific element within XML).
                    {
                        alert(oXEncXmlEncryptionComponent.EncryptedXml);
                        document.getElementById("hiddenEncryptedXml").value = oXEncXmlEncryptionComponent.EncryptedXml;

                        // Decrypt the document (this sample uses the private key for the same certificate that you have selected for encryption)
                        oXEncXmlEncryptionComponent.Decrypt(oXEncXmlEncryptionComponent.EncryptedXml); 
                        alert(oXEncXmlEncryptionComponent.DecryptedXml);

                        return true;
                    }
                    else alert("Error");
                }
                alert("Please select certificate!");
            }
            catch (e) {
                alert("JavaScript Error: " + e.message);
            }
            return false;
        }
    </script>
    
    <div>
        <h1>
            Lizard Labs: Test XEnc for Firefox (XML encryption component)</h1>
        <input id="hiddenXml" type="hidden" runat=server />
        <input id="hiddenEncryptedXml" type="hidden"  runat=server/>
    <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="~\Default.aspx">Back</asp:HyperLink>
    <p>
    <ul>
       <li>This sample works only in Firefox</li>
       <li>To enable use of unsigned XPCOM Enable <strong>codebase_principal_support</strong> in browser preference In Firefox location bar, type <strong>about:config</strong>. When the new window comes up, type "signed.applets.codebase_principal_support" in the filter bar. The property named: "<b>signed.applets.codebase_principal_support"</b> will be displayed. Select the property and then "right click" on it, select toggle to convert (the boolean) value to <strong>"true"</strong>. This option adds support for interactive security configuration for signed applets and JavaScript in your browser.</li>
       <li>To enable instalation and usage of signed XPCOM you have to install Lizard Labs 
           CA Certificate in Firefx Certificate store (trusted certificates)</li>
       <li>
            To install the XEnc Lite plugin for the FireFox browser open this URL in the FireFox browser and :<br />
                <a href="javascript:installXEncXpi();"> Click here</a> or
                <a href="./LizardLabsXEncLiteFireFox.xpi"> download the .xpi</a> (right click and Save as). <br /> After downloading .xpi, open it with the FireFox browser.<br />
       </li>
       <li>To test the XEnc Lite for Firefox click on the "Test" button demonstrating how to encrypt XML documents using JavaScript in Firefox. Examine the source code of this page to look for a sample code. </li>
   </ul>

<asp:Button ID="Button1" runat="server" Text="Test" OnClientClick="return testencrypt();" />

    </p>
    
        <p>
            <strong>XML:</strong></p>
        <asp:Label ID="lblXml" runat="server"></asp:Label>
        <p>
            <strong>Encrypted XML:</strong>
        </p>
        <asp:Label ID="lblEncryptedXml" runat="server"></asp:Label>
        <p>
            <strong>Decrypted result:</strong></p>
        <asp:Label ID="lblDecryptedXml" runat="server"></asp:Label>
    </div>
    </form>
</body>
</html>
