Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#加密(AES管理)与PHP等效_C#_Php_Encryption - Fatal编程技术网

C#加密(AES管理)与PHP等效

C#加密(AES管理)与PHP等效,c#,php,encryption,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 p

我有一些由我们的第三方提供的数据,这些数据是用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);
}
使用System.Security.Cryptography;
私有静态int KEY_SIZE=32;
私有静态字节[]IV={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
公共静态字符串加密字符串(字符串明文、字符串密码)
{
字节[]键=新字节[键大小];
byte[]passwordbytes=Encoding.UTF8.GetBytes(密码);
对于(int i=0;i=passwordbytes.Length)
键[i]=0;
其他的
键[i]=密码字节[i];
}
字节[]加密;
//创建AESCryptServiceProvider对象
//使用指定的键和IV。
使用(AESSManaged aesAlg=新AESSManaged())
{
aesAlg.Mode=CipherMode.CBC;
aesAlg.KeySize=键大小*8;
//创建decrytor以执行流变换。
ICryptoTransform encryptor=aesAlg.CreateEncryptor(密钥,IV);
//创建用于加密的流。
使用(MemoryStream msEncrypt=new MemoryStream())
{
使用(CryptoStream csEncrypt=new CryptoStream(msEncrypt,encryptor,CryptoStreamMode.Write))
{
使用(StreamWriter swEncrypt=newstreamwriter(csEncrypt))
{
//将所有数据写入流。
书写(纯文本);
}
加密=msEncrypt.ToArray();
}
}
}
返回Convert.tobase64字符串(加密);
}
我想用PHP编写解密程序。我还得到了一个密钥,他们称之为“AES256加密密钥”

另外,他们在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;
}
公共静态字符串解密字符串(字符串密文、字符串密码)
{
字节[]键=新字节[键大小];
byte[]passwordbytes=Encoding.UTF8.GetBytes(密码);
对于(int i=0;i=passwordbytes.Length)
键[i]=0;
其他的
键[i]=密码字节[i];
}
byte[]CipherTextBytes=Convert.FromBase64String(密文);
//声明用于保存的字符串
//解密的文本。
字符串明文=空;
//创建AESCryptServiceProvider对象
//使用指定的键和IV。
使用(AESSManaged aesAlg=新AESSManaged())
{
aesAlg.Mode=CipherMode.CBC;
aesAlg.KeySize=键大小*8;
//创建decrytor以执行流变换。
ICryptoTransform decryptor=aesAlg.CreateDecryptor(密钥,IV);
//创建用于解密的流。
使用(MemoryStream msDecrypt=新的MemoryStream(CipherTextBytes))
{
使用(CryptoStream csDecrypt=新加密流(msDecrypt、解密程序、CryptoStreamMode.Read))
{
使用(StreamReader srDecrypt=新的StreamReader(csDecrypt))
{
//从解密流中读取解密的字节
//然后把它们放在一根绳子里。
明文=srDecrypt.ReadToEnd();
}
}
}
}
返回纯文本;
}
但是我想用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);
}

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

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

我自己也不太喜欢这类东西,但也许这能帮上忙:谢谢。。。在我发表文章之前,我已经尝试过这一点,但确实解决了我的问题……我自己对这类事情不太感兴趣,但也许这能帮上忙:谢谢。。。在我发表文章之前,我已经尝试过这一点,但确实解决了我的问题…绝对有用!!非常感谢你!我按原样使用了$key,忽略了解密函数第一行中的填充部分,这对我很有效。在PHP 7中,
mcrypt_encrypt
只是一条注释,您可以查看其他选项。您应该将自己的答案设置为正确答案,因为您知道,这是一个对您有效的答案。此外,人们寻找这些信息将很容易知道,这篇文章有一个正确的答案。绝对有用!!非常感谢你!我按原样使用了$key,忽略了解密函数第一行中的填充部分,这对我很有效。在PHP 7中,
mcrypt_encrypt
只是一条注释,您可以查看其他选项。您应该将自己的答案设置为正确答案,因为您知道,这是一个对您有效的答案。此外,人们寻找这些信息将很容易知道,这篇文章有一个正确的答案。