C# 加密 (AesManaged) 与 PHP 等效

标签 c# php encryption

我有一些由第三方提供的数据,这些数据是使用 C# 算法加密的。请看下面:

using System.Security.Cryptography;

private static int KEY_SIZE = 32;
private static byte[] IV = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };

public static string EncryptString(string plaintext, string password)
{
    byte[] key = new byte[KEY_SIZE];
    byte[] passwordbytes = Encoding.UTF8.GetBytes(password);

    for (int i = 0; i < KEY_SIZE; i++)
    {
        if (i >= passwordbytes.Length)
            key[i] = 0;
        else
            key[i] = passwordbytes[i];
    }

    byte[] encrypted;

    // Create an AesCryptoServiceProvider object
    // with the specified key and IV.
    using (AesManaged aesAlg = new AesManaged())
    {
        aesAlg.Mode = CipherMode.CBC;
        aesAlg.KeySize = KEY_SIZE * 8;

        // Create a decrytor to perform the stream transform.
        ICryptoTransform encryptor = aesAlg.CreateEncryptor(key, IV);

        // Create the streams used for encryption.
        using (MemoryStream msEncrypt = new MemoryStream())
        {
            using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
            {
                using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
                {
                    //Write all data to the stream.
                    swEncrypt.Write(plaintext);
                }
                encrypted = msEncrypt.ToArray();
            }
        }
    }

    return Convert.ToBase64String(encrypted);
}

我想用 PHP 编写解密程序。我还只获得了一个 key ,他们称之为“AES256 加密 key ”。

他们给我提供的C#解密算法如下:

public static string DecryptString(string cipherText, string password)
{
    byte[] key = new byte[KEY_SIZE];
    byte[] passwordbytes = Encoding.UTF8.GetBytes(password);

    for (int i = 0; i < KEY_SIZE; i++)
    {
        if (i >= passwordbytes.Length)
            key[i] = 0;
        else
            key[i] = passwordbytes[i];
    }

    byte[] CipherTextBytes = Convert.FromBase64String(cipherText);

    // Declare the string used to hold
    // the decrypted text.
    string plaintext = null;

    // Create an AesCryptoServiceProvider object
    // with the specified key and IV.
    using (AesManaged aesAlg = new AesManaged())
    {
        aesAlg.Mode = CipherMode.CBC;
        aesAlg.KeySize = KEY_SIZE * 8;

        // Create a decrytor to perform the stream transform.
        ICryptoTransform decryptor = aesAlg.CreateDecryptor(key, IV);

        // Create the streams used for decryption.
        using (MemoryStream msDecrypt = new MemoryStream(CipherTextBytes))
        {
            using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
            {
                using (StreamReader srDecrypt = new StreamReader(csDecrypt))
                {
                    // Read the decrypted bytes from the decrypting stream
                    // and place them in a string.
                    plaintext = srDecrypt.ReadToEnd();
                }
            }
        }

    }
    return plaintext;
}

但是我想用PHP编写解密。由于我对加密方面非常陌生,请引导我走向正确的方向。

非常感谢......

最佳答案

我们得出如下结论。我希望它对某人有用。

function encrypt($string = '', $key = '') {
    $key = utf8_encode($key);

    //make it 32 chars long. pad with \0 for shorter keys
    $key = str_pad($key, 32, "\0");

    //make the input string length multiples of 16. This is necessary
    $padding = 16 - (strlen($string) % 16);
    $string .= str_repeat(chr($padding), $padding);

    //emtpy IV - initialization vector
    $iv = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";

    $encrypted = base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $string, MCRYPT_MODE_CBC, $iv));
    return rtrim($encrypted);
}

function decrypt($string = '', $key = '') {
    $key = $key . "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
    $iv = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
    $string = base64_decode($string);

    return mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, $string, MCRYPT_MODE_CBC, $iv);
}

关于C# 加密 (AesManaged) 与 PHP 等效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16734423/

相关文章:

c# - resharper 可以跳转到包含单元测试的文件吗?

encryption - 保护 Windows Azure 和 web.config 中的连接字符串

ios - 如何确定我的应用程序是否包含加密?

php - Laravel 5.1 - 对合并的 Eloquent 集合进行排序

php - 切换我的服务器中的 php 版本

PHP 7.2 计数错误

java - Java中的AES加解密

c# - 返回子类实例的方法

c# - 如何在有选择的情况下获取 TextBox 的插入位置?

c# - 是否可以在不获取所有链接的情况下获取链接表?