[pycrypto] Got different PKCS1_v1_5 signature result on the same message & same rsa key pair

wanggang wanggang at goldenapptechnology.com
Tue Mar 11 20:22:14 PDT 2014

Hi pycrypto,

This may be a simple problem since I am a newbiee to crypto.
I am not sure why I got different result from PKCS1_v1_5.new(RSA.construct(lst)).sign(SHA.new(a2b(m))) , 
I have mentioned the 'stateful' comment in API doc, but still can't fix the code below.
besides I have used DES for a lot, & never got stuck in such situation.

if __name__ == "__main__":
    t = (modulus, publicExponent, privateExponent, prime1, prime2, coefficient)
    lst = [long(x,16) for x in t]

    k = '1234'
    e1 = RSA.construct(lst).encrypt(m, k)
    e2 = RSA.construct(lst).encrypt(m, k)
    assert e1 == e2 # passed
    signature1 = PKCS1_v1_5.new(RSA.construct(lst)).sign(SHA.new(a2b(m)))
    print binascii.b2a_hex(signature1)
    signature2 = PKCS1_v1_5.new(RSA.construct(lst)).sign(SHA.new(a2b(m)))
    print binascii.b2a_hex(signature2)
    assert signature1==signature2 # why this assertion failed ?

Thank you in advance!    
 Wang Gang

More information about the pycrypto mailing list