Я не писал, так как он может быть помечен как дубликат, я использую тот же сертификат и как XML, используя мой собственный извлеченный XML.

аюсь проверить цифровую подпись с помощью заданных файлов сертификатов для автономного приложения проверки aadhaar KYC.

Эта инструкция приведена в документации для проверки.

Прочитайте весь XML и отделите от него тег s = ”xxxx”.

Используйте алгоритм проверки подписи, использующий технику хеширования и шифрования на основе «SHA256withRSA».

Значение подписи, присутствующее в теге «s», оставшийся XML (без тега «s») и открытый ключ UIDAI (доступно здесь.) Необходимо подать в алгоритм для проверки цифровой подписи.

Образцы фрагментов кода C #, предоставленные организацией. (PS: который тоже не работает)

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

        /*----------------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-----------------------*/

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

        /* 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);
        bool Flag = signer.VerifySignature(expectedSig);            
        if (Flag)
        {
            Console.WriteLine("XML Validate Successfully");
        }
        else
        {
            Console.WriteLine("XML Validation Failed");
        }
    }
}
}

Я пытаюсь реализовать в Python и получить проверку XML не удалось. Я не уверен, что файл сертификата неверен или в моем коде есть какая-то ошибка.

Вот мой код Python:

import xml
import xml.etree.cElementTree as etree
from xml.etree import ElementTree
import OpenSSL
from cryptography import x509
from cryptography.hazmat.backends import default_backend
from Crypto.PublicKey import RSA
from base64 import b64encode, b64decode
from M2Crypto import BIO, RSA, EVP

xmlDoc = open('adhar.xml', 'r').read()
Tr = etree.XML(xmlDoc)
Tr.keys()
# ['s', 'r', 'a', 'g', 'm', 'e', 'd', 'i', 'n', 'v']

sign = Tr.get('s')
len(sign)
# 344

del Tr.attrib['s']

from M2Crypto import X509

x509 =X509.load_cert('ekyc_public_key.cer')
#x509 =X509.load_cert(cert4)
rsa = x509.get_pubkey().get_rsa()
pubkey = EVP.PKey()
pubkey.assign_rsa(rsa)

xmlstr = etree.tostring(Tr, encoding='utf8', method='xml')
#rstr=str(xmlstr)[45:][:-1]
#rstr = rstr.encode(encoding='utf-8')


# if you need a different digest than the default 'sha1':
pubkey.reset_context(md='sha256')
pubkey.verify_init()

# hashlib.sha256(message_without_sign).digest()
pubkey.verify_update(xmlstr)
if(pubkey.verify_final(b64decode(sign)) != 1):
    print('Digital Signeture not validated')
else: 
    print('Digital Signeture validated')  

Ответы на вопрос(3)

Ваш ответ на вопрос