1

I have php code, I want to run it in python3 so which Encryption in python equivalent to php's MCRYPT_RIJNDAEL_256?

Here is php code:

<?php
     $customerId = 108;
     $user = "John Doe";
     $password = "mysecretpassword";
     $aes_key = "NzQ3MGIyOWEyMDk0MzI3Y2RiNzlkMThjZGY5YTJmY2YzNzI1OTQxMw";
     date_default_timezone_set("Asia/Singapore");
     // 1)Encode User password in SHA1 : SHA1(User_Password)
     $password = sha1($password);
     // 2)concat Customer ID, user name, user password encoded in SHA1 and date
     $ticket = $customerId.$user.$password.date('Ymd');
     // Get padding and AES Initialization Vector
     $IVsize = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CBC);
     $IV = substr(str_pad($aes_key, $IVsize, $aes_key), 0, $IVsize);
     $keySize = mcrypt_get_key_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CBC);
     $aes_key = substr(str_pad($aes_key, $keySize, $aes_key), 0, $keySize);
     $BlockSize = mcrypt_get_block_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CBC);
     $StringLength = strlen($ticket);
     $Padding = $BlockSize - ($StringLength % $BlockSize);
     // 3)Encrypt the result of 2) in AES 256 (Rijndael) with padding in CBC mode 
     //:MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CBC
     $ticket .= str_repeat(chr($Padding), $Padding);
     $ticket = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $aes_key, $ticket, MCRYPT_MODE_CBC, $IV);
    // 4)Encode the result of 3) in base 64
    $ticket = base64_encode($ticket);
    // 5)In the result of 4), replace reserved characters :
    $ticket = strtr($ticket, '+/=', '-_,');
    echo "Ticket = '$ticket'";
?>

For mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $aes_key, $ticket, MCRYPT_MODE_CBC, $IV); this function in php which one I need to use in python? Could anyone help me?

Zer0
  • 1,580
  • 10
  • 28

1 Answers1

3

The encryption

$ciphertext = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $plaintext, MCRYPT_MODE_CBC, $iv);

uses Rijndael with a block size of 256 bits, the CBC mode and Zero padding. Note that mcrypt_encrypt is deprecated.

Rijndael is defined for block and key sizes that correspond to an integer multiple of 32 bits and lie between 128 and 256 bits, here. MCRYPT_RIJNDAEL_256 specifies Rijndael with a block size of 256 bits. Care must be taken here to avoid confusion with AES. AES is a subset of Rijndael with the key sizes 128, 192 and 256 bits and the block size 128 bits. Therefore AES is not used in the posted code.
Unlike AES, Rijndael is not a standard and should therefore be avoided if possible. For this reason Rijndael is not offered in many libraries (unlike AES), for example not in the well-known Python libraries PyCryptodome or Cryptography.

A Python 3 library that implements Rijndael is py3rijndael. The following code shows its use for Rijndael with a block size of 256 bits in CBC mode and Zero padding:

from py3rijndael import RijndaelCbc, ZeroPadding
import base64

key = b'01234567890123456789012345678901'
iv = b'98765432109876543210987654321098'
plaintext = b'The quick brown fox jumps over the lazy dog'
rijndaelCbc = RijndaelCbc(
    key = key,
    iv = iv,
    padding = ZeroPadding(32),
    block_size = 32
)
ciphertext = rijndaelCbc.encrypt(plaintext)
print(base64.b64encode(ciphertext).decode('utf8'))

Output:

Ce33Y2Q5YDfven922xt/pZgEFVPWEe6rUuEpMS+YHtq2MPbRZ3L14T4t+EMOoFfoPkGw8cJ7q5oH58vr5hhzFQ==

The result matches the ciphertext provided by mcrypt (assuming the same values for plaintext, key and IV):

$key = '01234567890123456789012345678901';
$iv = '98765432109876543210987654321098';
$plaintext = 'The quick brown fox jumps over the lazy dog';
$ciphertext = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $plaintext, MCRYPT_MODE_CBC, $iv);
print(base64_encode($ciphertext) . PHP_EOL);

Output:

Ce33Y2Q5YDfven922xt/pZgEFVPWEe6rUuEpMS+YHtq2MPbRZ3L14T4t+EMOoFfoPkGw8cJ7q5oH58vr5hhzFQ==
Topaco
  • 40,594
  • 4
  • 35
  • 62