Python AES decryption

I have the following piece of code in Java that I want to replicate in Python.

public class AESDecryption {

    protected SecretKeySpec getPublicKey() {

        try {
            byte[] key = "MuidKeibimbtjph9".getBytes("UTF-8");
            key = MessageDigest.getInstance("SHA-256").digest(key);
            key = Arrays.copyOf(key, 32);
            return new SecretKeySpec(key, "AES");
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }

    public String decrypt(byte[] data) {
        Cipher cipher = null;
        try {
            cipher = Cipher.getInstance("AES/CBC/NoPadding");
            cipher.init(2, new SecretKeySpec(getPublicKey().getEncoded(), "AES"), new IvParameterSpec(new byte[cipher.getBlockSize()]));
            byte decryptedBytes[] = cipher.doFinal(data);
            return new String(Arrays.copyOf(decryptedBytes, decryptedBytes.length - decryptedBytes[-1 + decryptedBytes.length]));
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        } catch (InvalidAlgorithmParameterException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        }
        return "";
    }

    public static void main(String[] args) {
        try {
            byte[] content = Files.readAllBytes(Paths.get("/tmp/dump.gzip"));
            AESDecryption aesDecryption = new AESDecryption();
            System.out.println(aesDecryption.decrypt(content));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

This code comes from the client application. I do not have authority on the server side where the encrypted content is created. On this issue, I changed the symmetric key and the way to get the content (in this example from the file, but actually from the https response)

I want to replicate this function in a python script using the PyCrypto library. This is what my original code looks like:

from Crypto.Cipher import AES
from Crypto.Hash import SHA256
from Crypto import Random

BLOCK_SIZE = 16
unpad = lambda s: s[0:-ord(s[-1])]

hash = SHA256.new()
hash.update('MuidKeibimbtjph9')
symmetric_key = hash.digest()
symmetric_key = symmetric_key[:32]

bytes_store = None
with open('/tmp/dump.gzip','r') as f:
    bytes_store = f.read()

rndfile = Random.new()
aes_decryptor = AES.new(symmetric_key, AES.MODE_CBC, rndfile.read(BLOCK_SIZE))
print unpad(aes_decryptor.decrypt(bytes_store))

Running Java code in an encrypted file works fine. The result looks a little different:

{"code":200,"status":"ok","api_version":"0.0.0","data":[.....],"notifications":{}}

However, python replication unloads "semi-delimited" text. Well, sort of ..

=c q[A $ dl tus":"ok","api_version":"0.0.0","data":[.....],"notifications":{}}

I can't make anything out of it. Looking at the Java code it is clear that there is no padding in the cipter block, so thought that probably the data on the server side is already a multiple of the cipher block size. There was also a lot of ▯▯▯ characters at the end of python output but quickly got rid of them by unpadding decrypted data. Still, can't figure out what I'm doing wrong that the first part of the payload is scrambled. , :)

+4
1

, IV ( ) , Python IV AES.new.

rndfile.read(BLOCK_SIZE) "\x00"*BLOCK_SIZE.

+4

Source: https://habr.com/ru/post/1569146/


All Articles