2013-02-13 142 views
1

嗨,我有c#的代碼示例,但我不能把它變成php。 ©試圖重寫代碼,但我不能這樣做。 在我的項目中,其他服務器使用c#加密數據,我必須使用PHP解密。C#到PHP的AES解密

我有密碼和鹽值。

這裏是C#代碼包括加密和解密功能。

using System; 

using System.Collections.Generic; 

using System.Linq; 
using System.Text; 
using System.Security.Cryptography; 
using System.IO; 

namespace EncryptionSample 
{ 
    public static class CipherUtility 
    { 
     public static string Encrypt(string plainText, string password, string salt) 
     { 
      if (plainText == null || plainText.Length <= 0) 
      { 
       throw new ArgumentNullException("plainText"); 
      } 

      if (String.IsNullOrEmpty(password)) 
      { 
       throw new ArgumentNullException("password"); 
      } 

      if (String.IsNullOrEmpty(salt)) 
      { 
       throw new ArgumentNullException("salt"); 
      } 

      byte[] encrypted; 
      byte[] saltBytes = Encoding.UTF8.GetBytes(salt); 

      using (Rfc2898DeriveBytes derivedBytes = new Rfc2898DeriveBytes(password, saltBytes)) 
      { 
       using (AesCryptoServiceProvider aesAlg = new AesCryptoServiceProvider()) 
       { 
        aesAlg.Key = derivedBytes.GetBytes(32); 
        aesAlg.IV = derivedBytes.GetBytes(16); 

        ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV); 

        using (MemoryStream msEncrypt = new MemoryStream()) 
        { 
         using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)) 
         { 
          using (StreamWriter swEncrypt = new StreamWriter(csEncrypt)) 
          { 
           swEncrypt.Write(plainText); 
          } 

          encrypted = msEncrypt.ToArray(); 
         } 
        } 
       } 
      } 

      return Convert.ToBase64String(encrypted); 
     } 

     public static string Decrypt(string cipherValue, string password, string salt) 
     { 
      byte[] cipherText = Convert.FromBase64String(cipherValue); 

      if (cipherText == null 
       || cipherText.Length <= 0) 
      { 
       throw new ArgumentNullException("cipherValue"); 
      } 

      if (String.IsNullOrWhiteSpace(password)) 
      { 
       throw new ArgumentNullException("password"); 
      } 

      if (String.IsNullOrWhiteSpace(password)) 
      { 
       throw new ArgumentNullException("salt"); 
      } 

      string plaintext = null; 
      byte[] saltBytes = Encoding.UTF8.GetBytes(salt); 

      using (Rfc2898DeriveBytes deriveBytes = new Rfc2898DeriveBytes(password, saltBytes)) 
      { 
       using (AesCryptoServiceProvider aesAlg = new AesCryptoServiceProvider()) 
       { 
        aesAlg.Key = deriveBytes.GetBytes(32); 
        aesAlg.IV = deriveBytes.GetBytes(16); 

        ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV); 

        using (MemoryStream msDecrypt = new MemoryStream(cipherText)) 
        { 
         using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)) 
         { 
          using (StreamReader srDecrypt = new StreamReader(csDecrypt)) 
          { 
           plaintext = srDecrypt.ReadToEnd(); 
          } 
         } 
        } 
       } 
      } 

      return plaintext; 
     } 
    } 
} 

我的php代碼在這裏,但我認爲我完全錯了。

function decrypt($encrypted, $password, $salt) { 
// Build a 256-bit $key which is a SHA256 hash of $salt and $password. 
$key = hash('SHA256', $salt . $password, true); 
// Retrieve $iv which is the first 22 characters plus ==, base64_decoded. 
$iv = base64_decode(substr($encrypted, 0, 22) . '=='); 
// print_r($iv);die(); 
// Remove $iv from $encrypted. 
$encrypted = substr($encrypted, 22); 
//print_r($encrypted);die(); 
// Decrypt the data. rtrim won't corrupt the data because the last 32 characters are the md5 hash; thus any \0 character has to be padding. 

$decrypted = rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, base64_decode($encrypted), MCRYPT_MODE_CBC, $iv), "\0\4"); 

// Retrieve $hash which is the last 32 characters of $decrypted. 
$hash = substr($decrypted, -32); 
// Remove the last 32 characters from $decrypted. 
$decrypted = substr($decrypted, 0, -32); 
// Integrity check. If this fails, either the data is corrupted, or the password/salt was incorrect. 
if (md5($decrypted) != $hash) return false; 

return $decrypted; 
} 
+0

你的php代碼在哪裏? – ChrisBint 2013-02-13 12:32:10

+0

我也加了我的php代碼。 – user2068321 2013-02-13 12:45:30

+0

哈希然後加密不是一個安全的MAC。在加密 - 然後mac方案中使用HMAC。 – CodesInChaos 2013-02-13 13:22:58

回答

1

乍一看,我可以看到你的鑰匙將會不同。您的C#代碼使用Rfc2898DeriveBytes生成密鑰,該密鑰是基於PBKDF2的密鑰生成器。另一方面,您的php代碼使用SHA256來生成密鑰。這些將返回不同的值。用不同的鑰匙,你甚至在你開始之前就完成了。

另外,我不知道CryptoStream會在密文的開頭附加IV,也不會在密文的末尾附加MAC值。如果文本解密,刪除該文本將使您的明文變得晦澀難懂。請注意,在C#解密方法中,您根據密鑰派生對象(這不是聰明的,因爲相同的密鑰會爲每個消息生成相同的IV,這會降低密文的第一個塊的安全性,但這是一個完全獨立的問題)。

您是否知道C#服務器正在生成與您的代碼示例完全相同的密文?你需要知道在服務器端使用的密碼學的確切參數

我建議你實際嘗試研究和理解C#將要發出的密文的格式,然後弄清楚如何使用在PHP中。加密技術可能非常棘手,特別是在嘗試集成異構系統時。