License key protection with RSA key

Late, I'm tired and probably pretty tight ....

I wrote an application that I need to protect, so it will only work on the machines for which I create the key. What I am doing now is to get the BIOS serial number and generate a hash from this, I then encrypt it using the RSA RSA private key. Then I sign the XML to make sure it is not tampered with. I try to pack the public key to decrypt and verify the signature, but every time I try to execute the code as a different user than the one that generated the signature, I get a signature failure.

Most of my code is modified from the sample code I found, as I am not so familiar with the RSA encryption that I would like to be. Below is the code I used and the code I thought I needed to use in order to get the right work ...

Any feedback would be greatly appreciated since I was completely lost at this point the source code that I worked with was like this, this code works fine as long as the user running the program is the same who originally signed the document ...

 CspParameters cspParams = new CspParameters();
            cspParams.KeyContainerName = "XML_DSIG_RSA_KEY";
            cspParams.Flags = CspProviderFlags.UseMachineKeyStore;

            // Create a new RSA signing key and save it in the container. 
            RSACryptoServiceProvider rsaKey = new RSACryptoServiceProvider(cspParams)
            {
                PersistKeyInCsp = true,
            };

This code is what I believe I should do, but it cannot verify the signature no matter what I do, regardless of whether it is the same user or another ...

RSACryptoServiceProvider rsaKey = new RSACryptoServiceProvider();
            //Load the private key from xml file
            XmlDocument xmlPrivateKey = new XmlDocument();
            xmlPrivateKey.Load("KeyPriv.xml");
            rsaKey.FromXmlString(xmlPrivateKey.InnerXml);

, - ( , , ). , , ....

cspParams.KeyContainerName = "XML_DSIG_RSA_KEY";

/ XML , / ? , , . , , .

? ?

, .

+5
2

XML, , XML , .dll . , , , ..

, : ( - , , Licensing.Private.Private.xml + + )

public static void SignDocument(XmlDocument xmldoc)
{
    //Get the XML content from the embedded XML privatekey.
    Stream s = null;
    string xmlkey = string.Empty;
    try
    {
        s = typeof(Sign).Assembly.GetManifestResourceStream("Licensing.Private.Private.xml");

        // Read-in the XML content.
        StreamReader reader = new StreamReader(s);
        xmlkey = reader.ReadToEnd();
        reader.Close();
    }
    catch (Exception e)
    {
        throw new Exception("Error: could not import key:",e);
    }

    // Create an RSA crypto service provider from the embedded
    // XML document resource (the private key).
    RSACryptoServiceProvider csp = new RSACryptoServiceProvider();
    csp.FromXmlString(xmlkey);
    //Creating the XML signing object.
    SignedXml sxml = new SignedXml(xmldoc);
    sxml.SigningKey = csp;

    //Set the canonicalization method for the document.
    sxml.SignedInfo.CanonicalizationMethod = SignedXml.XmlDsigCanonicalizationUrl; // No comments.

    //Create an empty reference (not enveloped) for the XPath transformation.
    Reference r = new Reference("");

    //Create the XPath transform and add it to the reference list.
    r.AddTransform(new XmlDsigEnvelopedSignatureTransform(false));

    //Add the reference to the SignedXml object.
    sxml.AddReference(r);

    //Compute the signature.
    sxml.ComputeSignature();

    // Get the signature XML and add it to the document element.
    XmlElement sig = sxml.GetXml();
    xmldoc.DocumentElement.AppendChild(sig);
}

private.xml public.xml. , private.xml .

RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
File.WriteAllText(@"C:\privateKey.xml", rsa.ToXmlString(true));  // Private Key
File.WriteAllText(@"C:\publicKey.xml", rsa.ToXmlString(false));  // Public Key
+5

, , , 1- ( : ).

0

All Articles