python - Error in RSA algorithm output when dealing with large integers -
i wrote code generate pseudo-random prime numbers, public/private keys, etc. sake of implementing rsa encryption (for personal amusement , nothing more). able encode text, generate public/private key, encrypt, decrypt, , decode when encoded text integer ~10^12 or less. e.g.
original message: hello plaintext equivalent: 448378203247 public key: (540594823829, 65537) private key: (540594823829, 261111754433) ciphertext: 63430225682
decrypting ciphertext returns original plaintext.
however, when encoded text larger integer, process fails. e.g.
original message: man plan canal panama plaintext equivalent: 39955594125525792198857762901926727877852838348601974063966023009 public key: (662173326571, 65537) private key: (662173326571, 29422219265) ciphertext: 429717871098
in case ciphertext much smaller plaintext, makes 1 suspect in encryption process went wrong. , sure enough, decrypt ciphertext , 58514793315 (clearly not original plaintext).
i'm thinking issue how python implements large numbers/calculations large numbers, , fact i'm not aware of how deal that. it's worth code encrypt/decrypt simply
pow(m, e, n) # plaintext, encryption exponent, modulus pow(c, d, n) # ciphertext, decryption exponent, modulus
and code encoding text http://gist.github.com/barrysteyn/4184435#file_convert_text_to_decimal.py
how ensure calculations these large integers carried out wish (and not result in truncated/incorrect answers)?
without breaking messages blocks, can encrypt messages smaller modulus rsa. if you're doing programming exercise, don't try encrypt larger modulus. if you're doing actual cryptography, don't. crypto library. doing own crypto recipe disaster.
Comments
Post a Comment