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>