Проверка XML не удалась с RSA256 с алгоритмом RSA - PullRequest
0 голосов
/ 29 декабря 2018

Я пытаюсь проверить автономный XML-файл aadhar ekyc с примером кода.Но проверка не проходит, я не уверен, что это ошибка кода или ошибка в файле .cer.

Я использую алгоритм SHA256 с RSA для проверки XML-файла, как указано в документации и образце кода.

То же самое происходит с кодом Python.

Существует ли какая-либо специальная подача кодированияв функцию проверки?

using System;
using System.Security.Cryptography.X509Certificates;
using System.Xml;


namespace test
{
class MainClass
{


    public static void Main(string[] args)
    {
        // link -> 
//https://drive.google.com/file/d/1aSv3HJUFf5_42Z-FqpdVHEk5b3VA3T3D/view


        string XMLFilePath = "offlineaadhaar.xml"; //Get the XML file

// link -> 
//https://drive.google.com/file/d/1FW4ciIhZqJuelOcGF2x6VaBCSDO9J-gM/view


string KeyFilePath = "okyc-publickey.cer"; //Get the public key certificate file

        XmlDocument ObjXmlDocument = new XmlDocument();

        ObjXmlDocument.Load(XMLFilePath); //Load the XML

        XmlAttributeCollection SignatureElement = ObjXmlDocument.DocumentElement.Attributes; //Get the all XML attribute

        string SignatureValue = SignatureElement.GetNamedItem("s").InnerXml; // Get Signature value




        SignatureElement.RemoveNamedItem("s");//Remove the signature "s" attribute from XML and get the new XML to validate

        //Console.WriteLine(SignatureElement);

        /*----------------Read and parse the public key as string-----------------------*/
        X509Certificate2 ObjX509Certificate2 = new X509Certificate2(KeyFilePath, "public"); //Initialize the public ket certificate file


        Org.BouncyCastle.X509.X509Certificate objX509Certificate;
        Org.BouncyCastle.X509.X509CertificateParser objX509CertificateParser = new Org.BouncyCastle.X509.X509CertificateParser();

        objX509Certificate = objX509CertificateParser.ReadCertificate(ObjX509Certificate2.GetRawCertData());
        /*----------------End-----------------------*/


        //Console.WriteLine(objX509Certificate);

        /* Init alg */
        Org.BouncyCastle.Crypto.ISigner signer = Org.BouncyCastle.Security.SignerUtilities.GetSigner("SHA256withRSA");

        //Console.WriteLine(signer);
        /* Populate key */
        signer.Init(false, objX509Certificate.GetPublicKey());


        /* Get the signature into bytes */
        var expectedSig = Convert.FromBase64String(SignatureValue);



        /* Get the bytes to be signed from the string */
        var msgBytes = System.Text.Encoding.UTF8.GetBytes(ObjXmlDocument.InnerXml);


        /* Calculate the signature and see if it matches */
        signer.BlockUpdate(msgBytes, 0, msgBytes.Length);


        Console.WriteLine(msgBytes.Length);


        bool Flag = signer.VerifySignature(expectedSig);

        if (Flag)
        {
            Console.WriteLine("XML Validate Successfully");

        }
        else
        {
            Console.WriteLine("XML Validation Failed");


        }
    }
}
}
...