Skip to content

Instantly share code, notes, and snippets.

@alastairmccormack
Last active January 16, 2018 11:55
Show Gist options
  • Save alastairmccormack/346ab526676305e3636dfeff6e600c8b to your computer and use it in GitHub Desktop.
Save alastairmccormack/346ab526676305e3636dfeff6e600c8b to your computer and use it in GitHub Desktop.
Pycrypto: Raw RSA verify output
# When using raw RSA key calculations, it sometimes necessary to see the
# verification detail created as a result of a .sign() process (or encryption
# using the private key, which Java let's you do!)
# Pycrypto .verify() method only allows you to check a known value, where as
# openssl let's you see the verification response. I.e
# > openssl rsautl -inkey pub_key.pem -raw -verify -pubin -in mysig.txt
# The following code was found by digging into the Pycrypto innards:
from Crypto.PublicKey import RSA
from Crypto.Util import number
key = RSA.importKey(private_key_str, key_password_str)
# The message must be packed as a long first.
secret_message_long = number.bytes_to_long(secret_message_bytes)
verify_long = pow(encrypted_session_key_long, key.e, key.n)
verify_bytes = number.long_to_bytes(result_long)
# Convert message back to a str (Unicode str in Py2).
# Replace 'utf-8' with the correct encoding for *your* message!!!!!
verify_str = verify_bytes.decode('utf-8')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment