-2

I tried to make a program that will encrypt some string with 256 AES. But I'm not able to decrypt it.

Here is the code

from Crypto.Cipher import AES
from Crypto import Random
import os, base64

block_size = 32

def pad(s):
        return s + "\0" * (block_size - len(s) % block_size)

key = os.urandom(block_size)
iv = Random.new().read(AES.block_size)
cipher = AES.new(key, AES.MODE_CBC, iv)
enc = cipher.encrypt(pad('Hello World'))

print 'Encrypted', enc

iv = enc[:AES.block_size]
key = os.urandom(block_size)
cipher = AES.new(key, AES.MODE_CBC, iv)
dec = cipher.decrypt(enc[block_size:])
dec_rstrip = dec.rstrip("\0")

print 'Decrypted', dec_rstrip
Filip Timko
  • 503
  • 5
  • 11
  • Possible duplicate of [How to decrypt OpenSSL AES-encrypted files in Python?](https://stackoverflow.com/questions/16761458/how-to-decrypt-openssl-aes-encrypted-files-in-python) – stovfl Nov 03 '18 at 19:45
  • "Please write a decryption tool for this encryption code" is not a reasonably-scoped question, nor one likely to help others. – Charles Duffy Nov 03 '18 at 20:38

1 Answers1

0
  1. You are using a different random key for encryption and decryption, it must be the same.
  2. The decryption iv is never initialized, it needs to one the same for encryption and decryption..
zaph
  • 111,848
  • 21
  • 189
  • 228