dthdlv9777 2015-09-15 17:00
浏览 70
已采纳

字符串在C#中加密使用TripleDES,需要在PHP中解密

C# Code looks like that (can't change it as it's in a client's system).

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

I found client took this class from here: http://johnnycoder.com/blog/2008/07/03/c-encryption-decryption-helper-class/

I'm not very familiar with C# and I need to Decrypt string in PHP encrypted with this code.

When I do "md5($key, true)" I don't get the same result as "MD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(cryptoKey));", not sure why.

How to convert "byte[] IV" to PHP string ?

Any help would be appreciated. Thank you.

  • 写回答

1条回答 默认 最新

  • dtpyvb1873 2015-09-16 10:21
    关注

    Managed to get it working:

    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;
    }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 需要跳转番茄畅听app的adb命令
  • ¥50 寻找一位有逆向游戏盾sdk 应用程序经验的技术
  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证