Replicate encryption method PHP Mcrypt to C # [closed]

-2

I need to be able to replicate a class that is created in PHP to C # Attached class code created in PHP. At this moment I must send an encrypted string to a client platform, but I can not send the encryption in the same way as it is done in PHP. I've tried several codes in c # but nothing works for me.

<?php
class MCrypt {

private $iv;
private $key;

public function __construct($vkey,$viv) {
    $this->key = $vkey;
    $this->iv = $viv;
}

public function encrypt($str) { 
    $str = $this->pkcs5_pad($str);   
    $td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, $this->iv); 
    mcrypt_generic_init($td, $this->key, $this->iv);
    $encrypted = mcrypt_generic($td, $str); 
    mcrypt_generic_deinit($td);
    mcrypt_module_close($td); 
    return bin2hex($encrypted);
}

public function decrypt($code) { 
    $code = $this->hex2bin($code);
    $td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, $this->iv); 
    mcrypt_generic_init($td, $this->key, $this->iv);
    $decrypted = mdecrypt_generic($td, $code); 
    mcrypt_generic_deinit($td);
    mcrypt_module_close($td); 
    $ut =  utf8_encode(trim($decrypted));
    return $this->pkcs5_unpad($ut);
}

protected function hex2bin($hexdata) {
    $bindata = ''; 
    for ($i = 0; $i < strlen($hexdata); $i += 2) {
        $bindata .= chr(hexdec(substr($hexdata, $i, 2)));
    } 
    return $bindata;
} 

protected function pkcs5_pad ($text) {
    $blocksize = 16;
    $pad = $blocksize - (strlen($text) % $blocksize);
    return $text . str_repeat(chr($pad), $pad);
}

protected function pkcs5_unpad($text) {
    $pad = ord($text{strlen($text)-1});
    if ($pad > strlen($text)) {
        return $text;   
    }
    if (strspn($text, chr($pad), strlen($text) - $pad) != $pad) {
        return $text;
    }
    return substr($text, 0, -1 * $pad);
}
}
?>
    
asked by Abelardo Irarrazabal 03.05.2018 в 17:00
source

1 answer

0

Finally inquiring more about the class that you attach in the question, get to this code which does what it needed to use the encryption method:

public class MCrypt
{
    string iv = "F2pb5=OYkq=qrar8";
    string key = "ZTlg}NCsS2[7kH16";

    public string Encrypt(string plainText)
    {
        string textByte = "";
        byte[] encrypted;

        RijndaelManaged rijAlg = new RijndaelManaged()
        {
            Key = Encoding.UTF8.GetBytes(key),
            IV = Encoding.UTF8.GetBytes(iv),
            Mode = CipherMode.CBC,
            BlockSize = 128,
            Padding = PaddingMode.PKCS7,
        };

        // Create a decrytor to perform the stream transform.
        ICryptoTransform encryptor = rijAlg.CreateEncryptor(rijAlg.Key, rijAlg.IV);

        // Create the streams used for encryption.
        using (MemoryStream msEncrypt = new MemoryStream())
        {
            using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
            {
                using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
                {
                    //Write all data to the stream.
                    swEncrypt.Write(plainText);
                }
                encrypted = msEncrypt.ToArray();
                StringBuilder hex = new StringBuilder(encrypted.Length * 2);
                foreach (byte b in encrypted)
                    hex.AppendFormat("{0:x2}", b);
                textByte = hex.ToString();
            }
        }

        return textByte;
    }


}
}
    
answered by 07.05.2018 / 14:47
source