加密.Net中的.Net解密

时间:2011-01-22 23:29:05

标签: c# php aes

我正在写一些由php解密的C#加密。他们没有很好地合作。 php客户端告诉我他们的配置(RSSL_KEYGEN_PKCS5_V20,26位AES在CBC模式下,RSSL_PAD_ANSIX923)。这是我放在一起的:

public static byte[] Encrypt(byte[] plainText, string password,
         CipherMode cipherMode, int keySize, PaddingMode paddingMode)
    {
        byte[] salt = new byte[8];
        new RNGCryptoServiceProvider().GetBytes(salt);

        // PKCS#5 V2.0 PBKDF2 keyGenerator (uses default iteration count of 1000) 
        Rfc2898DeriveBytes keyGenerator = new Rfc2898DeriveBytes(password, salt);

        AesCryptoServiceProvider aes = new AesCryptoServiceProvider();
        aes.Mode = cipherMode;
        aes.KeySize = keySize;
        aes.Padding = paddingMode;
        aes.Key = keyGenerator.GetBytes(aes.KeySize / 8);

        // aes.Padding is left default PaddingMode.PKCS7 
        // aes.IV should be automatically set to some random value by .NET 
        // (as described in http://msdn.microsoft.com/en-us/library/5e9ft273(VS.71).aspx) 

        // Overestimate encrypted size requirements 
        byte[] encryptedDataBuffer = new byte[plainText.Length + 32 + 32 + 8];
        MemoryStream encryptedOutput = new MemoryStream(encryptedDataBuffer, true);
        CryptoStream encStream = new CryptoStream(encryptedOutput, aes.CreateEncryptor(),
                                                  CryptoStreamMode.Write);

        encryptedOutput.Write(salt, 0, salt.Length);
        encryptedOutput.Write(aes.IV, 0, aes.IV.Length);
        encStream.Write(plainText, 0, plainText.Length);

        encStream.FlushFinalBlock();
        byte[] encryptedData = new byte[encryptedOutput.Position];
        Array.Copy(encryptedDataBuffer, encryptedData, encryptedData.Length);
        encStream.Close();

        return encryptedData;
    }

我可以加密和解密它,但他们无法解密它。我在这里做错了吗?我传了这样的话:

Encrypt(mytext, myPassword, CipherMode.CBC, 256, PaddingMode.ANSIX923);

感谢。

3 个答案:

答案 0 :(得分:2)

由于.NET和mcrypt处理“默认”AES256 imlpement的不同方式,这对我自己来说是一个挑战。在某些版本的PHP中,m_crypt使用零填充,因此您必须设置.NET端才能执行相同的操作:

symmetricKey.Padding = PaddingMode.Zeros;

我发布了一个示例PHP& github的C#解决方案可以帮助遇到同样问题的其他人: https://github.com/dchymko/.NET--PHP-encryption

答案 1 :(得分:1)

首先缩小范围: 你在生成相同的密钥吗?

一旦你确定了,那么你可以考虑加密。

答案 2 :(得分:0)

使用CryptoServiceProvider可以解决问题。

 Encoding stringEncoding = new System.Text.UTF8Encoding();

        byte[] salt = new byte[8];

        new RNGCryptoServiceProvider().GetBytes(salt);

        AesCryptoServiceProvider aes = new AesCryptoServiceProvider();
        aes.Mode = cipherMode;
        aes.KeySize = keySize;

        aes.Key = stringEncoding.GetBytes(password);
        aes.IV = new byte[aes.IV.Length];

        // Overestimate encrypted size requirements 
        byte[] encryptedDataBuffer = new byte[plainText.Length + 32 + 32 + 8];
        MemoryStream encryptedOutput = new MemoryStream(encryptedDataBuffer, true);

        CryptoStream encStream = new CryptoStream(encryptedOutput, aes.CreateEncryptor(),
                                                  CryptoStreamMode.Write);

        encStream.Write(plainText, 0, plainText.Length);

        encStream.FlushFinalBlock();
        byte[] encryptedData = new byte[encryptedOutput.Position];
        Array.Copy(encryptedDataBuffer, encryptedData, encryptedData.Length);
        encStream.Close();

        return encryptedData;