what is the equivalent of this in python

for a bank transaction implementation, I have a private key and some data and I need to sign that data with the private key.

The example code the bank has given is in C# .NET and I can't find any equivalent to it in python.

The key is in the following format :


And the example code is this :

RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
rsa.FromXmlString(" <RSAKeyValue><Modulus>oQRshGhLf2Fh... ");
string data = "#" + merchantCode + "#" + terminalCode + "#"
+invoiceNumber + "#" + invoiceDate + "#" + amount + "#" +redirectAddress
+ "#" + action + "#" + timeStamp + "#";
byte[] signMain = rsa.SignData(Encoding.UTF8.GetBytes(data), new
sign = Convert.ToBase64String(signMain);

Now I have not found any good equivalent for this in python and I need to do the exact thing in order to not raise any exceptions. I once asked this question in another literature and it got put on hold. I don't know why they did that but I have been struggling this for weeks and I have not yet found any solutions. so please help me in any way you can. thanks in advance.

You will need to convert your .NET XML format key to a .PEM in order for it to be handled by any Python modules. (See this question)

Once you have that, you could use the RSA or PyCrypto modules.

Check out this example code for signing data using PyCrypto:

def sign_data(private_key_loc, data):
    param: private_key_loc Path to your private key
    param: package Data to be signed
    return: base64 encoded signature
    from Crypto.PublicKey import RSA 
    from Crypto.Signature import PKCS1_v1_5 
    from Crypto.Hash import SHA256 
    from base64 import b64encode, b64decode 
    key = open(private_key_loc, "r").read() 
    rsakey = RSA.importKey(key) 
    signer = PKCS1_v1_5.new(rsakey) 
    digest = SHA256.new() 
    # It's being assumed the data is base64 encoded, so it's decoded before updating the digest 
    sign = signer.sign(digest) 
    return b64encode(sign)

Obviously, it will need to be adapted to your needs (Use import SHA instead of import SHA256 if you need SHA-1, for example), but it should give you a good head-start.

c# - what is the equivalent of this in python, NET and I can't find any equivalent to it in python. The key is in the following format : <RSAKeyValue> <Modulus></Modulus> <Exponent></Exponent> <P >. Python's equivalent of && (logical-and) in an if-statement. 0 votes . 1 view. asked Jul 26, 2019 in Python by selena (1.6k points) Here's my code: def front_back(a, b

Notice that example code use SHA1 ,not SHA256.

Also you can use an online RSA Key Converter tool to convert RSA xml format to .pem format if you want to do it once.

You can use this method:

   import json
   from Crypto.PublicKey import RSA
   from Crypto.Signature import PKCS1_v1_5
   from Crypto.Hash import SHA1
   import base64 import       

   def get_sign(data, private_key):
        rsa_key = RSA.importKey(base64.b64decode(private_key.encode("utf-8")))
        signer = PKCS1_v1_5.new(rsa_key)
        digest = SHA1.new()
        sign = signer.sign(digest)
        return base64.b64encode(sign).decode('utf-8')

   # usage example 
   pem_private_key= """
   payload_json = json.dumps({"is_example":True})
   sign = get_sign(pem_private_key, payload_json.encode('utf-8'))

Python and Java - Comparisons and Java, Java has no special syntax that is similar to Python's elif . Java Syntax is just like C. do { } while loop, No Python equivalent, do { statement; . . . statement; }� Python Reference Python Overview Python Built-in Functions Python String Methods Python List Methods Python Dictionary Methods Python Tuple Methods Python Set Methods Python File Methods Python Keywords Python Exceptions Python Glossary Module Reference Random Module Requests Module Math Module cMath Module Python How To

https://pypi.python.org/pypi/rsa is an RSA implementation. I don't know what the heck is happening in that rsa.FromXmlString (https://msdn.microsoft.com/en-us/library/vstudio/system.security.cryptography.rsa.fromxmlstring(v=vs.90) ), so that will take some investigation on your part.

http://stuvel.eu/files/python-rsa-doc/reference.html#rsa.sign shows you how to sign a message in RSA, you'll want the SHA-1 hash method. Then base64 encode the results https://docs.python.org/2/library/base64.html

Object-Oriented Programming in Python vs Java – Real Python, In Python, you both declare and define attributes inside the class __init__() , which is the equivalent of Java's constructor: 1 def __init__(self, color, model, year):� Python Bitwise Operators. Bitwise operator works on bits and performs bit by bit operation. Assume if a = 60; and b = 13; Now in the binary format their values will be 0011 1100 and 0000 1101 respectively.

Descriptor HowTo Guide — Python 3.8.5 documentation, Shows how each works by giving a pure Python equivalent and a sample application. Learning about descriptors not only provides access to a larger toolset,� Python provides built-in composite data types called list, tuple, dict, and set. These are “container” types that contain other objects. An object of one of these types is considered false if it is empty and true if it is non-empty. The examples below demonstrate this for the list type. (Lists are defined in Python with square brackets.)

9. Classes — Python 3.8.5 documentation, Python classes provide all the standard features of Object Oriented In general, calling a method with a list of n arguments is equivalent to calling the� Is there a Python equivalent function of the which command? This is my code. cmd = ["which","abc"] p = subprocess.Popen(cmd, stdout=subprocess.PIPE) res = p.stdout.readlines() if len(res) == 0: return False return True

9.9. operator — Standard operators as functions — Python 2.7.18 , add(x, y) is equivalent to the expression x+y . The function names are those used for special class methods; variants without leading and trailing� The code should be pretty self-explanatory. There is, however, one piece of code that is needed for Python 3 compatibility, which deals with the differences between Python 2 and 3 with respect to how modules are imported. This is explained here. The code should now also work both with Python 2 and 3.

  • thanks a bunch, nobody said python doesn't support that format. but how can I convert it?
  • In the question I linked above, there are a few methods listed for the conversion.