2015-09-15 2 views
4

Код C# выглядит так (не может изменить его, как в клиентской системе).Строка, зашифрованная в C# Использование TripleDES, необходимо расшифровать в PHP

namespace Common { 
public static class EncryptionHelper 
{ 

    private const string cryptoKey = "password"; 

// The Initialization Vector for the DES encryption routine 
    private static readonly byte[] IV = new byte[8] { 240, 3, 45, 29, 0, 76, 173, 59 }; 

/// <summary> 
/// Encrypts provided string parameter 
/// </summary> 
public static string Encrypt(string s) 
{ 

    string result = string.Empty; 

    byte[] buffer = Encoding.ASCII.GetBytes(s); 

    byte[] k = Encoding.ASCII.GetBytes(cryptoKey); 

    TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider(); 
    MD5CryptoServiceProvider MD5 = new MD5CryptoServiceProvider(); 


    des.Key = MD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(cryptoKey)); 

    des.IV = IV; 

    result = Convert.ToBase64String(des.CreateEncryptor().TransformFinalBlock(buffer, 0, buffer.Length)); 

    return result; 
    } 
} 
} 

Я нашел клиент взял этот класс здесь: http://johnnycoder.com/blog/2008/07/03/c-encryption-decryption-helper-class/

Я не очень хорошо знаком с C#, и мне нужно Расшифровать строку в PHP шифруется с этим кодом.

Когда я делаю «md5 ($ key, true)« Я не получаю такой же результат, как «MD5.ComputeHash (ASCIIEncoding.ASCII.GetBytes (cryptoKey))», не знаю, почему.

Как преобразовать «byte [] IV» в строку PHP?

Любая помощь будет оценена по достоинству. Спасибо.

+0

Вы уверены, что 'MD5.ComputeHash()', или даже ваш C# класс, возвращает необработанный бинарный хеш? 'md5 ($ key, true)' _will_ выводит исходный двоичный хеш, поэтому это может быть источником вашей несоответствия. – Fuselight

+0

Мне удалось получить ту же строку md5, но до сих пор не могу ее расшифровать. – staskus

ответ

2

удалось заставить его работать:

class Crypter 
{ 

/** 
* 
* Encryption key 
* 
* @var 
*/ 
protected $key; 

/** 
* 
* Encryption vector 
* 
* @var 
*/ 
protected $iv; 

public function __construct() 
{ 

    $this->key = config('auth.triple_des_key'); 
    $this->iv = implode(array_map("chr", config('auth.triple_des_iv'))); 
} 


/** 
* 
* Decrypts string using tripleDES method. 
* 
* @param $input String 
* @return String 
*/ 
public function decryptTripleDES($input) 
{ 

    $td = mcrypt_module_open(MCRYPT_3DES, '', MCRYPT_MODE_CBC, ''); 

    $encryptedData = base64_decode($input); 

    $key = iconv('utf-8', 'us-ascii//TRANSLIT', $this->key); 

    $key = md5($key, true); 
    $key .= substr($key, 0, 8); 

    mcrypt_generic_init($td, $key, $this->iv); 

    $decryptedData = mdecrypt_generic($td, $encryptedData); 
    mcrypt_generic_deinit($td); 

    //remove the padding text 
    $block = mcrypt_get_block_size("tripledes", "cbc"); 

    $packing = ord($decryptedData{strlen($decryptedData) - 1}); 

    if ($packing and ($packing < $block)) { 
     for ($P = strlen($decryptedData) - 1; $P >= strlen($decryptedData) - $packing; $P--) { 
      if (ord($decryptedData[$P]) != $packing) { 
       $packing = 0; 
      } 
     } 
    } 

    $decryptedData = substr($decryptedData, 0, strlen($decryptedData) - $packing); 

    return $decryptedData; 
} 
} 
Смежные вопросы