I'd use the cryptography
project here. It has full RSA signing support. See the cryptography section of the Hitchhiker's Guide to Python for alternative options.
The .NET RSACryptoServiceProvider
object doesn't let you set a padding algorithm, it will always use PKCS1v15, so select that padding in your Python code.
First, I'd convert that XML document to a PEM format so you can load the public key from that format. I'm not aware of any Python libraries that can load the .NET / XKMS 2.0 XML format directly:
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import serialization
private_key_pem = b'-----BEGIN {format}-----\n...\n-----END {format}-----'
private_key = serialization.load_pem_private_key(
private_key_pem,
password=None,
backend=default_backend()
)
Alternatively, with a Python XML parser you can probably feed the data from that XML document to a RSAPublicNumbers()
and RSAPrivateNumbers()
pair, then use the RSAPrivateNumbers.private_key()
method to produce a RSA private key object to sign with. You'll have to base64-decode the XML data and convert from bytes to integer using big endian byte ordering.
You can then use the private key to do the signing:
from base64 import b64encode
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.asymmetric import padding
data = "something"
signature = private_key.sign(data.encode('utf8'), padding.PKCS1v15(), hashes.SHA1())
signature_base64 = b64encode(signature)