PHP code:
mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $to_encrypt, MCRYPT_MODE_ECB);
I need the corresponding powershell code which can produce the same result.
I have already tried ConvertTo-SecureString/ ConvertFrom-SecureString.
Powershell Code (Not producing correct encryption):
$api_code = "214e3854a1ec5433ae986d1e5d40c436"
$params = @{"controller" = $controller; "action"= $action; "all"= $TRUE; }
[Byte[]] $key = $api_code[0..$api_code.length]
$param_object = $params | ConvertTo-SecureString -AsPlainText -Force
$param_aes = $param_object | ConvertFrom-SecureString -key $key
The encrypted string is coming out different. Is there a parameter that I am missing? Or Is there another module?
As pointed out in the comments,
SecureString
s have nothing to do with the Rijndael specification, andMCRYPT_RIJNDAEL_256
is not the same as AES256 (which refer Rijndael-128 with a 256-bit key)So, to solve your problem, we just need a function to encrypt a plaintext in ECB cipher mode using Rijndael with a block size of 256.
For this, the obvious choice is the
RijndaelManaged
class. Fortunately, the MSDN documentation provides a basic but fully functional example of how to use theRijndaelManaged
class and aCryptoStream
to encrypt and decrypt strings - all we need to do is rewrite it in PowerShell and change the block size and cipher mode:The decryption process is almost the same, although this time we'll need to reverse it and read the cipher text back through the CryptoStream from the MemoryStream: