0

I have been translating PHP code to Python because my company is switching language and framework, but i am stuck on this one method, i can't seems to find the Python equivalent for it.

This is the PHP code:

function addpadding($string, $blocksize = 32) {
  $len = strlen($string);
  $pad = $blocksize - ($len % $blocksize);
  $string .= str_repeat(chr($pad), $pad);
  return $string;
}

function create_mpg_aes_encrypt($parameter = '', $key = '', $iv = '') {
   $return_str = http_build_query($parameter);
   return trim(bin2hex(mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, addpadding($return_str), MCRYPT_MODE_CBC, $iv)));
}

This is my python code:

def addpadding_pay2go(string, blocksize=32):
   length              = len(string)
   pad                 = blocksize - (length % blocksize)
   padding_strings     = chr(pad) * pad
   new_string          = string + padding_strings
   return new_string

Now for the create_mpg_aes_encrypt, i can only re-create the first line in Python, i don't know how to do the second line.

test_array = {
    'MerchantID'        : 'XXX',
    'RespondType'       : 'JSON',
    'TimeStamp'         : unixtime,
    'Version'           : '1.4',
    'MerchantOrderNo'   : 1,
    'Amt'               : 10,
    'ItemDesc'          : 'product description',
    'Key'               : 'XXXXX',
    'IV'                : 'XXXXX'
}

return_str = urllib.urlencode(test_array)

I can do the trim and bin2hex. But my only problem is "how to re-create this in Python?"

mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, addpadding($return_str), MCRYPT_MODE_CBC, $iv)
RaR
  • 193
  • 1
  • 13
  • Also see [Upgrading my encryption library from Mcrypt to OpenSSL](http://stackoverflow.com/q/43329513/608639), [Replace Mcrypt with OpenSSL](http://stackoverflow.com/q/9993909/608639) and [Preparing for removal of Mcrypt in PHP 7.2](http://stackoverflow.com/q/42696657/608639) – jww Jun 05 '17 at 03:35
  • 1
    no i haven't tried python aes, i am weak with cryptography. I don't even know what MCRYPT_RIJNDAEL_128 and MCRYPT_MODE_CBC means, so i am quite stuck with that method translation to Python. – RaR Jun 05 '17 at 03:40
  • Forgive my prying... Why have you been assigned the task if you don't have the experience or knowledge to complete it? This seems like one of those areas the company should want to avoid mistakes. – jww Jun 05 '17 at 04:13
  • 1
    i am the guy that wrote the original PHP project code and i am the one that is now using python Django for the new software. – RaR Jun 05 '17 at 04:16
  • 1
    I can use python and django, i just don't know the equivalent of that PHP method and code in Python – RaR Jun 05 '17 at 04:16
  • `MCRYPT_RIJNDAEL_128` is Rijndael with a block size if 128-bits which is essentially AES. So: 1. You are really encryptiing with [AES](https://en.wikipedia.org/wiki/Advanced_Encryption_Standard), a good thing. 2. The block size is 16-bytes, not 32-bytes. 3. `MCRYPT_MODE_CBC` is specifing an encryption mode, [Cipher Block Chaining](https://en.wikipedia.org/wiki/Block_cipher_mode_of_operation#Cipher_Block_Chaining_.28CBC.29). 4. If you are trying to create security it really would be worth your time to study encryption, espicially how to use AES, not necessarily how it works internally. – zaph Jun 05 '17 at 12:31
  • zaph, i added an answer, is it correct? – RaR Jun 06 '17 at 02:16

1 Answers1

1

I tried this, i don't know whether it is correct or not

from Crypto.Cipher import AES
import urllib

def addpadding_pay2go(string, blocksize=32):
  length              = len(string)
  pad                 = blocksize - (length % blocksize)
  padding_strings     = chr(pad) * pad
  new_string          = string + padding_strings
  return new_string

def create_mpg_aes_encrypt(test_array, key, iv):
  return_str              = urllib.urlencode(test_array)
  return_str_with_padding = addpadding_pay2go(return_str)

  AES.key_size    = 128
  crypt_object    = AES.new(key=key, mode=AES.MODE_CBC, IV=iv)
  encrypted_text  = crypt_object.encrypt(return_str_with_padding)
  hex             = encrypted_text.encode('hex')
  hex_with_strip  = hex.strip()
  return hex_with_strip
RaR
  • 193
  • 1
  • 13