title: Convert decrypted RSA value to string
body: <p>I am trying to build a module for a CTF I am planning.  The idea is that you are given an encrypted file and an RSA public key and have to decrypt the file. </p>
  
  <p>I am intentionally using a weak RSA key (128bit) so that it can be factored and the private exponent deduced.  I am encrypting a file using <code>openssl rsautl</code> with the <code>-raw</code> padding option like so:</p>
  
  <p><code>openssl rsautl -encrypt -raw -inkey public_key.pem -pubin -in test.txt -out test.enc</code></p>
  
  <p>I am then attempting to walk through the exercise, taking the public key and deducing the private exponent from there.  Since I have the actual private key, I am able to confirm that I have done this part correctly and have the right primes / private exponent.   </p>
  
  <p>In my code I read in the encrypted file, convert it to hex and then perform the decrypt operation on each hex value.  This leaves me with a very large long, e.g. <code>133742007812331115643627009906598839927</code> for each decrypted value. Trying to use <code>chr()</code> to convert these to ascii obviously fails with a <code>OverflowError: Python int too large to convert to C long</code> error. </p>
  
  <p>My plaintext that I am encrypting: <code>this is a test!</code></p>
  
  <p>The resulting encrypted value(screenshot to preserve special chars): <a href="https://i.stack.imgur.com/q7FA7.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/q7FA7.png" alt="encrypted value"></a></p>
  
  <p>The hexdump value of the encrypted file: </p>
  
  <pre><code>hexdump -C test.enc
  00000000  36 eb 43 3d 1d 0d 68 50  3e b7 7b c0 0e 33 52 12  |6.C=..hP&gt;.
  {..3R.|
  00000010
  </code></pre>
  
  <p>My code: </p>
  
  <pre><code>def testdecrypt():
    a = 16
    e = 65537
    p = 15308793451280999927
    n = 225614505179301198794693425143200819953
    q = 14737575883906276439
    phi = (p-1)*(q-1)
    d = multiplicative_inverse(e, phi)
  
    with open('test.enc', 'rb') as file:
      input = file.read()
      print('input as read from file: {0}'.format(input))
      input = binascii.hexlify(input)
      input = input.replace('\n', '').replace('\r', '')
      m = [hex(pow(int(input[i:i+a], 16), d, n)).rstrip("L") for i in range(0, len(input), a)]
      print(m)
  
      for mm in m:
          str = []
          mm = mm.strip('0x')
          for i in range(0, len(mm), 2):
              mm_chr = chr(int(mm[i:i+2], 16))
              str.append(mm_chr) #put in a list for easier viewing
          print(str)
  </code></pre>
  
  <p>Which produces the following output: </p>
  
  <pre><code>input as read from file: 6ëC=
  hP&gt;·{À3R
  input in hex: 36eb433d1d0d68503eb77bc00e335212
  133742007812331115643627009906598839927
  73331587639103218432501997963763741611
  ... 14 more longs
  </code></pre>
  
  <p>My question is how am I supposed to convert these long values back to ascii? Or am I supposed to do some sort of translation before the decryption operation which wouldn't leave me with these longs?  Note that I have tried reading the file in 64bits at a time, but that leaves me with 2 large longs that I still don't know how to convert.</p>
  
  <p>I have looked at <a href="https://crypto.stackexchange.com/questions/24804/decrypted-rsa-64-bit-encryption-using-modulus-and-private-exponent-got-gobbledy">this post quite a bit</a> and believe it probably would have helped me solve my problem if the author hadn't apparently deleted some of their comments. I didn't post this on crypto because it seems to be outside their scope since the issue isn't directly with the cryptography.</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>I did not see <a href="https://stackoverflow.com/questions/29461923/python-code-that-decrypts-rsa-encrypted-file-in-chunks-of-64-bit-using-a-private?rq=1">this question</a> when posting (swear i looked).  However even after reviewing that question and modifying my code to break the decryption into 64-bit chunks I am still left with two hex values, which when converted to ascii using <code>chr()</code> do not equal the original plain text: </p>
  
  <pre><code>['0x4e4b0c3d7b0f232461adcc78364fef76', '0x652ac1281861415bf4d157947791b419']
  ['N', 'K', '\x0c', '=', '{', '\x0f', '#', '$', 'a', '\xad', '\xcc', 'x', '6', 'O', '\xef', 'v']
  ['e', '*', '\xc1', '(', '\x18', 'a', 'A', '[', '\xf4', '\xd1', 'W', '\x94', 'w', '\x91', '\xb4', '\x19']
  </code></pre>