1

I have 3 projects and want in all 3 projects the same encrypt and decrypt methods. it works to get the same encrypted output for c# and php. But in powershell I get a slight diffrent output. Can someone EDIT my powershell script so I get same output in all 3?

Hi, I have 3 projects and want in all 3 projects the same encrypt and decrypt methods. it works to get the same encrypted output for c# and php. But in powershell I get a slight diffrent output. Can someone EDIT my powershell script so I get same output in all 3?

c# code:

    public class rijndael
    {
        /*
         * Encrypt method
         * Both Keys and IVs need to be 16 characters encoded in base64. 
         */
        public String AES_encrypt(String Input, String AES_Key, String AES_IV)
        {
            // Create encryptor
            var aes = new System.Security.Cryptography.RijndaelManaged();
            aes.KeySize = 128;
            aes.BlockSize = 128;
            aes.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
            aes.Key = Convert.FromBase64String(AES_Key);
            aes.IV = Convert.FromBase64String(AES_IV);
            var encrypt = aes.CreateEncryptor(aes.Key, aes.IV);

            // Encrypt Input
            byte[] xBuff = null;
            using (var ms = new System.IO.MemoryStream())
            {
                // Convert from UTF-8 String to byte array, write to memory stream and encrypt, then convert to byte array
                using (var cs = new System.Security.Cryptography.CryptoStream(ms, encrypt, System.Security.Cryptography.CryptoStreamMode.Write))
                {
                    byte[] xXml = Encoding.UTF8.GetBytes(Input);
                    cs.Write(xXml, 0, xXml.Length);
                }
                xBuff = ms.ToArray();
            }

            // Convert from byte array to base64 string then return
            String Output = Convert.ToBase64String(xBuff);
            return Output;
        }

        /*
         * Decrypt method
         * Both Keys and IVs need to be 16 characters encoded in base64. 
         */
        public String AES_decrypt(String Input, String AES_Key, String AES_IV)
        {
            // Create decryptor
            System.Security.Cryptography.RijndaelManaged aes = new System.Security.Cryptography.RijndaelManaged();
            aes.KeySize = 128;
            aes.BlockSize = 128;
            aes.Mode = CipherMode.CBC;
            aes.Padding = PaddingMode.PKCS7;
            aes.Key = Convert.FromBase64String(AES_Key);
            aes.IV = Convert.FromBase64String(AES_IV);
            var decrypt = aes.CreateDecryptor();

            // Decrypt Input
            byte[] xBuff = null;
            using (var ms = new MemoryStream())
            {
                // Convert from base64 string to byte array, write to memory stream and decrypt, then convert to byte array.
                using (var cs = new CryptoStream(ms, decrypt, CryptoStreamMode.Write))
                {
                    byte[] xXml = Convert.FromBase64String(Input);
                    cs.Write(xXml, 0, xXml.Length);
                }
                xBuff = ms.ToArray();
            }

            // Convert from byte array to UTF-8 string then return
            String Output = Encoding.UTF8.GetString(xBuff);
            return Output;
        }
    }

            rijndael rijndael_crypt = new rijndael();
            string pass = rijndael_crypt.AES_encrypt("saltkeytesting@ken.vanhuffel@password", Base64Encode("1234567891123456"), Base64Encode("1234567891123456"));

powershell code:

Clear-Host

[Reflection.Assembly]::LoadWithPartialName("System.Security") | Out-Null

function Encrypt-String($stringToEncrypt, $AES_Key, $AES_IV)
{
    # Create a COM Object for RijndaelManaged Cryptography
    $aes = new-Object System.Security.Cryptography.RijndaelManaged
    $aes.KeySize = 128;
    $aes.BlockSize = 128;
    #$aes.Mode = [System.Security.Cryptography.CipherMode]::CBC;
    $aes.Padding = [System.Security.Cryptography.PaddingMode]::PKCS7;
    $aes.Key = [System.Convert]::FromBase64String($AES_Key);
    $aes.IV = [System.Convert]::FromBase64String($AES_IV);
    $encrypt = $aes.CreateEncryptor($aes.Key,$aes.IV);

    [byte[]] $xBuff = $null;
    $ms = new-Object IO.MemoryStream;
    $cs = new-Object Security.Cryptography.CryptoStream $ms,$encrypt,"Write";

    [byte[]]$xXml = [Text.Encoding]::UTF8.GetBytes($stringToEncrypt);

    $cs.Write($xXml, 0, $xXml.Length);
    $xBuff = $ms.ToArray();

    return [Convert]::ToBase64String($xBuff); 
}

$exampleString = "saltkeytesting@ken.vanhuffel@password";


$key_passphrase = "1234567891123456"
$key_bytes = [Text.Encoding]::UTF8.GetBytes($key_passphrase);
$AES_Key =[Convert]::ToBase64String($key_bytes);

$iv = "1234567891123456";
$iv_bytes = [Text.Encoding]::UTF8.GetBytes($iv);
$AES_IV =[Convert]::ToBase64String($iv_bytes);

write-host "String to encrypt is: $exampleString"
$stringToDecrypt = Encrypt-String  $exampleString $AES_Key $AES_IV
write-host "Encrypted is: $stringToDecrypt"

php code:

class Rijndael128Encryptor
{
    /*
     * Adds PKCS7 padding
     */
    private function addpadding($inputstring)
    {
        $blocksize = 16;
        $len = strlen($inputstring);
        $pad = $blocksize - ($len % $blocksize);
        $inputstring .= str_repeat(chr($pad), $pad);
        return $inputstring;
    }

    /*
     * Strips PKCS7 padding
     */
    private function strippadding($inputstring)
    {
        $slast = ord(substr($inputstring, -1));
        $slastc = chr($slast);
        if(preg_match("/$slastc{".$slast."}/", $inputstring)){
            $inputstring = substr($inputstring, 0, strlen($inputstring)-$slast);
            return $inputstring;
        } else {
            return false;
        }
    }

    /*
     * Encrypt method
     * Both Keys and IVs need to be 16 characters encoded in base64. 
     */ 
    public function encrypt($inputstring, $inputkey, $inputiv)
    {
            $key = base64_decode($inputkey);
            $iv = base64_decode($inputiv);
            // Pad text and encrypt
            $padded_string = $this->addpadding($inputstring);
            $encrypted_string = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $padded_string, MCRYPT_MODE_CBC, $iv);
            // Encode to base64 and return
            return base64_encode($encrypted_string);
    }

    /*
     * Decrypt method
     * Both Keys and IVs need to be 16 characters encoded in base64. 
     */ 
    public function decrypt($inputstring, $inputkey, $inputiv)
    {
            $key = base64_decode($inputkey);
            $iv = base64_decode($inputiv);
            // Decode from base64 and decrypt
            $decoded_string = base64_decode($inputstring);
            $decrypted_string = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, $decoded_string, MCRYPT_MODE_CBC, $iv);
            // Unpad text and return
            return $this->strippadding($decrypted_string);
    }
}

$encrypter = new Rijndael128Encryptor;
$encrypt_me = $encrypter->encrypt("saltkeytesting@ken.vanhuffel@password", base64_encode("1234567891123456"), base64_encode("1234567891123456"));

echo $encrypt_me;
  • You can just use your C# code in your Powershell script. No real reason to write up a whole different thing. See these: --- https://stackoverflow.com/questions/742262/what-can-i-do-with-c-sharp-and-powershell --- https://blog.adamfurmanek.pl/2016/03/19/executing-c-code-using-powershell-script/ --- https://blog.stefan-gossner.com/2010/05/07/using-csharp-c-code-in-powershell-scripts – postanote Feb 18 '20 at 05:34
  • thats not true at all. if I paste my c# it gives me a bunch of syntax errors. and my C# script works perfectly. – ken.vanhuffel Feb 18 '20 at 06:45
  • Copy-paste was not the point of my response, as the attached articles show. You have constructs you must follow to get C# to run in PowerShell, again, as the articles show. There are many others that show this, as well as many videos on Youtube, MSDN, etc. I use C# in my scripts, raw code and dll's (native and 3rdP) regularly. This is what the Add-Type cmdlet is for. Did you take a look at the articles? – postanote Feb 18 '20 at 22:33
  • I have found my soultion: I just write code in C#. then build a DLL of it. Then I load that DLL c# assembly in powershell. Then I call my methods in powershell and call those methods from the DLL file. So my powershell calls all needed functions from a DLL C# file. (load assembly) – ken.vanhuffel Mar 02 '20 at 07:00
  • Good for you, but, that was my point in my response. Hence the pointer to the MS docs / PowerShell Add-type cmdlet. I've got lots of DLL I and my team have developed as well, which are leveraged in PowerShell daily as do many others all over the web who demo how to do that. So, not some new things. It's been this way for a few years. – postanote Mar 02 '20 at 07:07
  • sorry bro, I just didnt understand your comment the first time ;) thx for helping man – ken.vanhuffel Mar 03 '20 at 09:46
  • No worries, this happens to us all. take care. – postanote Mar 03 '20 at 17:20

0 Answers0