0
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
rsa.FromXmlString(PrivateKey);

string data = "SAMPLE TEXT.";

byte[] signedData = rsa.SignData(Encoding.UTF8.GetBytes(data), new
SHA1CryptoServiceProvider());
string signedString = Convert.ToBase64String(signedData);

The code above signs a string with a PrivateKey loaded from an xml.

Would you help me rewrite it in python?

I wrote this code myself but it produce a different signature!

from Crypto.PublicKey import RSA
from Crypto.Util.number import bytes_to_long, long_to_bytes
from hashlib import sha1
import base64

msg = "SAMPLE TEXT."
hashed = sha1(msg.encode('utf-8')).digest()
pubkey = RSA.construct((m,e))
encrypted = pubkey.encrypt(hashed, b'X')[0]
print("Signature: {0}".format(base64.b64encode(encrypted)))
Artjom B.
  • 61,146
  • 24
  • 125
  • 222
mahyard
  • 1,230
  • 1
  • 13
  • 34
  • I haven't found proper documentation of what `SignData(byte[], object)` does and looking into the reference source of .Net didn't answer this. I suspect it's a simple textbook RSA invocation, but I'm not sure. It can also be RSA signature with PKCS#1 v1.5 padding or PSS padding. – Artjom B. Apr 17 '17 at 21:37
  • 1
    The SignData method on RSACryptoServiceProvider which doesn't take an RSASignaturePadding does PKCS#1 v1.5 signature padding. The sample here in python appears to encrypt, which may be using PKCS#1 v1.5 encryption padding (different beast) or raw (differenter beast); and it's using a public key operation whereas signing is a private key operation. – bartonjs Apr 18 '17 at 05:23
  • @bartonjs Thank you very much, such a useful comment, so I learned that encrypting is different from signing and some google taught me signing will be done using a private key. but still I don't get my lovely output so can you explain more about signature padding and how can I force pycrypto to work this way? – mahyard Apr 18 '17 at 06:58
  • I wrote a new snippet you can see here https://pastebin.com/Gv2VY5Za, but it doesn't work fine yet ( the output differs from what C#.NET gives me ) – mahyard Apr 18 '17 at 08:10

1 Answers1

-1

There is no builtin RSA library in Python, that I know of, but take a look at this one (just named rsa) which supports signing.

# importing rsa
import rsa

# generating a key
(pubkey, privkey) = rsa.newkeys(512)

# message
message = 'Go left at the blue tree'

# signing
signature = rsa.sign(message, privkey, 'SHA-1')

# verifying
rsa.verify(message, signature, pubkey) # True
Hans
  • 2,354
  • 3
  • 25
  • 35
  • 1
    Thank you for your help, I can use external libraries like _**PyCryto**_ – mahyard Apr 17 '17 at 15:24
  • 1
    Not quite a link-only answer, but you could provide more information. Consider describing the library so that, if the link becomes dead, a user can google using your description and find the new location of the library. – President James K. Polk Apr 17 '17 at 15:40
  • You'd better tell us more about that library. putting the link is not enough ! Also the op asked about rewriting the code in python like doing some math operations to generate ! anyhow the library you mentioned is not doing much ! –  Apr 18 '17 at 08:25