【问题标题】:String Encrypted in C# Using TripleDES, need to decrypt in PHP在 C# 中使用 TripleDES 加密的字符串,需要在 PHP 中解密
【发布时间】:2015-12-12 01:00:17
【问题描述】:

C# 代码看起来像这样(无法更改它,因为它在客户端的系统中)。

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 字符串?

任何帮助将不胜感激。 谢谢。

【问题讨论】:

  • 你确定MD5.ComputeHash(),甚至你的C#类,返回原始二进制哈希? md5($key, true) 输出原始二进制哈希,因此这可能是您不一致的根源。
  • 我已经设法得到相同的 md5 字符串,但仍然无法解密。

标签: c# php tripledes


【解决方案1】:

设法让它工作:

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;
}
}

【讨论】:

    猜你喜欢
    • 2010-09-18
    • 1970-01-01
    • 1970-01-01
    • 2016-12-25
    • 2012-12-12
    • 1970-01-01
    • 1970-01-01
    • 2023-03-25
    • 1970-01-01
    相关资源
    最近更新 更多