Я хочу перенести следующий метод PHP на C#. Пожалуйста, помогите мне
function encryptData($input, $key_seed = "1234567890123") { $input = trim($input); $block = mcrypt_get_block_size('tripledes', 'ecb'); $len = strlen($input); $padding = $block - ($len % $block); $input .= str_repeat(chr($padding), $padding); // generate a 24 byte key from the md5 of the seed $key = substr(md5($key_seed), 0, 24); $iv_size = mcrypt_get_iv_size(MCRYPT_TRIPLEDES, MCRYPT_MODE_ECB); $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND); // encrypt $encrypted_data = mcrypt_encrypt(MCRYPT_TRIPLEDES, $key, $input, MCRYPT_MODE_ECB, $iv); // clean up output and return base64 encoded $encrypted_data = base64_encode($encrypted_data); return $encrypted_data; } function decrypt($input, $key_seed = "1234567890123") { $input = base64_decode($input); $key = substr(md5($key_seed), 0, 24); $text = mcrypt_decrypt(MCRYPT_TRIPLEDES, $key, $input, MCRYPT_MODE_ECB, 'Mkd34ajdfka5'); $block = mcrypt_get_block_size('tripledes', 'ecb'); $packing = ord($text{strlen($text) - 1}); if ($packing and ( $packing < $block)) { for ($P = strlen($text) - 1; $P >= strlen($text) - $packing; $P--) { if (ord($text{$P}) != $packing) { $packing = 0; } } } $text = substr($text, 0, strlen($text) - $packing); return $text; }
Что я уже пробовал:
private static string EncryptData(string input, string keySeed = "1234567890123") { byte[] myEncryptedArray = Encoding.UTF8 .GetBytes(input); MD5CryptoServiceProvider myMd5CryptoService = new MD5CryptoServiceProvider(); byte[] mysecurityKeyArray = myMd5CryptoService.ComputeHash (Encoding.UTF8.GetBytes(keySeed)); myMd5CryptoService.Clear(); var myTripleDesCryptoService = new TripleDESCryptoServiceProvider(); myTripleDesCryptoService.Key = mysecurityKeyArray; myTripleDesCryptoService.Mode = CipherMode.ECB; myTripleDesCryptoService.Padding = PaddingMode.PKCS7; var myCrytpoTransform = myTripleDesCryptoService .CreateEncryptor(); byte[] myresultArray = myCrytpoTransform .TransformFinalBlock(myEncryptedArray, 0, myEncryptedArray.Length); myTripleDesCryptoService.Clear(); return Convert.ToBase64String(myresultArray, 0, myresultArray.Length); } public static string Decrypt(string input, string keySeed = "1234567890123") { byte[] myDecryptArray = Convert.FromBase64String (input); MD5CryptoServiceProvider myMd5CryptoService = new MD5CryptoServiceProvider(); byte[] mysecurityKeyArray = myMd5CryptoService.ComputeHash (Encoding.UTF8.GetBytes(keySeed)); myMd5CryptoService.Clear(); var myTripleDesCryptoService = new TripleDESCryptoServiceProvider(); myTripleDesCryptoService.Key = mysecurityKeyArray; myTripleDesCryptoService.Mode = CipherMode.ECB; myTripleDesCryptoService.Padding = PaddingMode.PKCS7; var myCrytpoTransform = myTripleDesCryptoService .CreateDecryptor(); byte[] myresultArray = myCrytpoTransform .TransformFinalBlock(myDecryptArray, 0, myDecryptArray.Length); myTripleDesCryptoService.Clear(); return Encoding.UTF8.GetString(myresultArray); }