none
How to sign an XML file using a private key and return the signature back? RRS feed

  • Question

  • Hello community, please help me to solve the following:

    I have a code on Java

    java code bellow:

     StringBuilder fullText;
    KeyStore p12 = KeyStore.getInstance("PKCS12");
    p12.load(new FileInputStream("FileName.p12"), "1234".toCharArray());
        
    Key key = (Key) p12.getKey("1", "1234".toCharArray());
           
    //signing
    Signature signer = Signature.getInstance("SHA1withRSA");
    
    signer.initSign((PrivateKey) key);
    signer.update(fullText.toString().getBytes());
    b`yte[] digitalSignature = signer.sign();
    
    String base64sign = new String(Base64.getEncoder().encode(digitalSignature));

    I tried to reproduce it on .Net platform.

    I create a code on .NET 3.5 platform. The code on X++ below:

    public static boolean Encrypt(str sXmlDoc)

    { boolean bSuccess = false; System.Security.Cryptography.X509Certificates.X509Certificate2 p12; System.Security.Cryptography.AsymmetricAlgorithm key; str sBase64Cert; str sBase64Xml; str sBase64Sign; str sTmp; System.Byte[] byteArray; System.Security.Cryptography.Xml.Signature signer; System.Exception ex; str sKeyPublic; System.Byte[] keyPublic; System.Int32 myInt32; int myInt; System.Byte[] byteTmp, byteTmp2; System.Text.ASCIIEncoding txtEncoder; System.Security.Cryptography.Xml.KeyInfo keyInfo; System.Security.Cryptography.Xml.SignedXml signedXml; System.Xml.XmlDocument xmlDocument; System.Xml.XmlElement xmlElement; System.Security.Cryptography.Xml.SignedInfo signedInfo; System.Security.Cryptography.Xml.Reference reference; System.Security.Cryptography.Xml.XmlDsigEnvelopedSignatureTransform env; System.Security.Cryptography.Xml.RSAKeyValue rsaKeyValue; System.Security.Cryptography.RSA rsaKey; try { p12 = new System.Security.Cryptography.X509Certificates.X509Certificate2("fileName.p12", "pass"); if (p12) { //Signature //TEST if (p12.get_HasPrivateKey()) { key = p12.get_PrivateKey(); rsaKey = p12.get_PrivateKey(); xmlDocument = new System.Xml.XmlDocument(); xmlDocument.set_PreserveWhitespace(true); //Allow white spaces xmlDocument.LoadXml(sXmlDoc); signedXml = new System.Security.Cryptography.Xml.SignedXml(xmlDocument); signedXml.set_SigningKey(key); keyInfo = new System.Security.Cryptography.Xml.KeyInfo(); rsaKeyValue = new System.Security.Cryptography.Xml.RSAKeyValue(rsaKey); keyInfo.AddClause(rsaKeyValue); signedXml.set_KeyInfo(keyInfo); // Create a reference to be signed. //System.Security.Cryptography.Xml.Reference reference; reference = new System.Security.Cryptography.Xml.Reference(); reference.set_Uri(""); // Add an enveloped transformation to the reference. env = new System.Security.Cryptography.Xml.XmlDsigEnvelopedSignatureTransform(); reference.AddTransform(env); // Add the reference to the SignedXml object. signedXml.AddReference(reference); signedXml.set_KeyInfo(keyInfo); signedXml.ComputeSignature(); xmlElement = signedXml.GetXml(); signer = new System.Security.Cryptography.Xml.Signature(); signer = signedXml.get_Signature(); signedInfo = new System.Security.Cryptography.Xml.SignedInfo(); signedInfo = signer.get_SignedInfo(); byteTmp = signer.get_SignatureValue(); sTmp = System.Convert::ToBase64String(byteTmp); sBase64Sign = "<signature>"+sTmp+"</signature>"; info(sBase64Sign); } } } catch (Exception::CLRError) { ex = ClrInterop::getLastException(); if (ex != null) { ex = ex.get_InnerException(); if (ex != null) { error(ex.ToString()); } } } return bSuccess; }

    But the result differs than I got on java. What did I do wrong?







    Thursday, February 6, 2020 1:42 PM

All replies