pycrypto: unable to decrypt file
<p>I am using PKCS1_OAEP crypto algorithm to encrypt a file. The file is encrypted successfully but unable to decrypt file, getting the error "Ciphertext with incorrect length."</p>

<p>Encryption Algorithm is here: </p>

<pre><code>#!/usr/bin/python
from Crypto.Cipher import PKCS1_OAEP
from Crypto.PublicKey import RSA
import zlib
import base64


fd = open('test.doc', 'rb')
message = fd.read()
fd.close()

print "[*] Original File Size: %d" % len(message)

#message = 'To be encrypted'
key = RSA.importKey(open('pubkey.der').read())
cipher = PKCS1_OAEP.new(key)

compressed = zlib.compress(message)
print "[*] Compressed File Size: %d" % len(compressed)

chunk_size = 128

ciphertext = ""
offset = 0

while offset &lt; len(compressed):
    chunk = compressed[offset:offset+chunk_size]

    if len(chunk) % chunk_size != 0:
        chunk += " " * (chunk_size - len(chunk)) # Padding with spaces

    ciphertext += cipher.encrypt(chunk)
    offset += chunk_size

print "[*] Encrypted File Size: %d" % len(ciphertext)

encoded = ciphertext.encode("base64")

print "[*] Encoded file size: %d" % len(encoded)

fd = open("enc.data", 'wb')
fd.write(encoded)
fd.close()

print "[+] File saved successfully!"
</code></pre>

<p>Decryption Algorithm is here:</p>

<pre><code>#!/usr/bin/python
from Crypto.Cipher import PKCS1_OAEP
from Crypto.PublicKey import RSA
import zlib
import base64

key = RSA.importKey(open('privkey.der').read())
cipher = PKCS1_OAEP.new(key)

fd = open('enc.data', 'rb')
encoded = fd.read().strip('\n')
fd.close()

decoded = encoded.decode("base64")


chunk_size = 128
offset = 0
plaintext = ""

while offset &lt; len(decoded):
    plaintext += cipher.decrypt(decoded[offset:offset+chunk_size])
    offset += chunk_size

#plaintext = cipher.decrypt(decoded)

decompress = zlib.decompress(plaintext)


fd = open('decr.doc', 'wb')
fd.write(decompress)
fd.close()
</code></pre>

<p>Using the following script to generate key</p>

<pre><code>from Crypto.PublicKey import RSA 

new_key = RSA.generate(2048, e=65537) 
public_key = new_key.publickey().exportKey("PEM") 
private_key = new_key.exportKey("PEM") 


fileWrite(fileName, data):
    fd = open(fileName, 'wb')
    fd.write(data)
    fd.close()

fileWrite('privkey.der', private_key)
fileWrite('pubkey.der', public_key)
</code></pre>

<p><a href="http://i.stack.imgur.com/NY8ox.png" rel="nofollow">Here is the Error Message</a></p>

<p>You encrypt with a <strong>2048 bit RSA key</strong>, which gives encrypted blocks of <strong>2048 bites (256 bytes)</strong>. Your decrypt implementation assumes the encrypted blocks are <strong>128 bytes</strong> where they are actually <strong>256 bytes</strong>, and thus you get the 'incorrect length' error. Notice how your encrypted files size (64512) is more than double of the compressed file size (32223).</p>

<p>In general you would not use <strong>RSA</strong> for <strong>bulk encryption</strong> (as it's quite slow) but would instead combine it with a symmetric encryption like <strong>AES</strong>. You would then encrypt the data with a random <strong>AES</strong> key, and then encrypt the <strong>AES</strong> key with the <strong>RSA</strong> key. This way you get the speed of <strong>AES</strong> and the two keys of <strong>RSA</strong>. This is known as <a href="https://en.wikipedia.org/wiki/Hybrid_cryptosystem" rel="nofollow">Hybrid Encryption</a>.</p>

python encryption cryptography public-key-encryption pycrypto