【问题标题】:C# to PHP AES DecryptionC# 到 PHP AES 解密
【发布时间】:2013-02-13 12:30:54
【问题描述】:

您好,我有 c# 代码示例,但我无法将其转换为 php。 我试图重写代码,但我做不到。 在我的项目中,其他服务器使用 c# 加密数据,我必须使用 PHP 对其进行解密。

我有密码和盐值。

这里是 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;
 }

【问题讨论】:

  • 你的php代码在哪里?
  • 我也添加了我的 php 代码。
  • Hash-then-encrypt 不是一个安全的 MAC。在 encrypt-then-mac 方案中使用 HMAC。
  • 密码应该被安全地(并且缓慢地)散列,而不是加密,当然也不能使用 MD5/SHA2

标签: php encryption aes rfc2898


【解决方案1】:

乍一看,我可以看到您的密钥会有所不同。您的 C# 代码使用Rfc2898DeriveBytes 生成您的密钥,这是一个基于 PBKDF2 的密钥生成器。另一方面,您的 php 代码使用 SHA256 来生成密钥。这些将返回不同的值。使用不同的键,您甚至在开始之前就完成了。

另外,我不知道CryptoStream 会在密文开头附加IV,也不知道在密文末尾附加MAC 值。如果完全解密,删除该文本将使您的纯文本出现乱码。请注意,在 C# 解密方法中,您基于密钥派生对象派生 IV(这并不聪明,因为相同的密钥将为每条消息生成相同的 IV,这会降低密文第一个块的安全性,但这是一个完全独立的问题)。

您知道 C# 服务器生成的密文与您的代码示例完全相同吗?您需要知道服务器端正在使用的密码学的确切参数

我建议您实际上尝试研究和理解 C# 将发出的密文格​​式,然后弄清楚如何在 PHP 中使用它。使用密码学可能非常棘手,尤其是在尝试集成异构系统时。

【讨论】:

    【解决方案2】:

    我不是加密专家,但我认为您可能会发现 phpseclib 很有用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-09-22
      • 1970-01-01
      • 1970-01-01
      • 2016-09-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多