首页 > 解决方案 > 为什么这两个代码(php/C#)的加密/解密方式不同?

问题描述

在 7.1 php 网站上,此函数加密一个字符串,我应该在 C# web 服务中对其进行解密,但它们不会产生相同的加密/解密结果。php和c#中的加密方法是否有任何理由不使用相同的密钥和iv密钥进行相同的加密?...

在 PHP 中,加密的问题是:l0zhGwbpC3aQLaEuQAgmqL3gdWBPKhqjX7/s5o/x2i6mPW74kSmsWMesweNHYfR1 而在 C# 中它给出了这个结果:cWI7Ovb44gDl3WZ/p86JLjFdmAFml92VmbrwZnQZH9z+RMfGPx6hp2e6cAnMRYIr

<?php



$question = "Quel est le nom de jeune fille de ma mère ?";

$encrypt_method = "AES-256-CBC";
$secret_key = 'Hakei54?sqSe6R71!lmdlrmPks65ekcj';
$secret_iv = 'Ryfdhi.q546dmlfp';

$key1 = hash('sha256', $secret_key);
$key2 = substr($key1, 0, 32);
$iv = substr(hash('sha256', $secret_iv), 0, 16);

$output1 = openssl_encrypt($question, $encrypt_method, $key2, 0, $iv);
$output2 = base64_encode($output1);


echo "<b>La question</b> : ".$question."<br> <b>La question cyrptée</b> : ".$output1;
echo "<br>";
echo "<b>La question crytée encodée</b> : ".$output2;
echo "<br>";
echo "<br>";
echo "<b>$Secret_key</b> : ".$secret_key."<br>";
echo "<b>$secret_iv</b> : ".$secret_iv."<br>";
echo "<br>";

echo "<b>[ hash('sha256', $secret_key); ]</b> : ".$key1."<br>";
echo "<b>[ $key = substr(hash('sha256', $secret_key), 0, 32); ]</b> : ".$key2."<br>";
echo "<b>[ hash('sha256', $secret_iv) ]</b> : ".hash('sha256', $secret_iv)."<br>";
echo "<b>[ $iv = substr(hash('sha256', $secret_iv), 0, 16); ]</b> : ".$iv."<br>";
echo "<br>";

echo "<b>[ $output1 = openssl_encrypt($question, $encrypt_method, $key, 0, $iv); ]</b> : ".$output1."<br>";
echo "<b>[ $output2 = base64_encode($output1); ]</b> : ".$output2."<br>";

echo "<br>";
echo "Le code utilisé :<br><br>";
echo "<pre>
        $question = 'Quel est le nom de jeune fille de ma mère ?';

        $encrypt_method = 'AES-256-CBC';
        $secret_key = 'Hakei54?sqSe6R71!lm';
        $secret_iv = 'Ryfdhi.q546';

        $key = hash('sha256', $secret_key);
        $iv = substr(hash('sha256', $secret_iv), 0, 16);

        $output1 = openssl_encrypt($question, $encrypt_method, $key, 0, $iv);
        $output2 = base64_encode($output1);
</pre>";
echo "<br>";
echo "<br>";
echo "La fonction php de décryptage :<br><br>";
echo "<pre>
        $output = openssl_decrypt(base64_decode($question), $encrypt_method, $key, 0, $iv);
</pre>";

?>

和 C# 代码:

using System;
using System.Security.Cryptography;
using System.IO;
using System.Text;
namespace ConsoleApp1
{
    class Program
    {
        static string EncryptString(string plainText, byte[] key, byte[] iv)
    {
        // Instantiate a new Aes object to perform string symmetric encryption
        Aes encryptor = Aes.Create();

        encryptor.Mode = CipherMode.CBC;

        // Set key and IV
        byte[] aesKey = new byte[32];
        Array.Copy(key, 0, aesKey, 0, 32);
        encryptor.Key = aesKey;
        encryptor.IV = iv;

        // Instantiate a new MemoryStream object to contain the encrypted bytes
        MemoryStream memoryStream = new MemoryStream();

        // Instantiate a new encryptor from our Aes object
        ICryptoTransform aesEncryptor = encryptor.CreateEncryptor();

        // Instantiate a new CryptoStream object to process the data and write it to the 
        // memory stream
        CryptoStream cryptoStream = new CryptoStream(memoryStream, aesEncryptor, CryptoStreamMode.Write);

        // Convert the plainText string into a byte array
        byte[] plainBytes = Encoding.ASCII.GetBytes(plainText);

        // Encrypt the input plaintext string
        cryptoStream.Write(plainBytes, 0, plainBytes.Length);

        // Complete the encryption process
        cryptoStream.FlushFinalBlock();

        // Convert the encrypted data from a MemoryStream to a byte array
        byte[] cipherBytes = memoryStream.ToArray();

        // Close both the MemoryStream and the CryptoStream
        memoryStream.Close();
        cryptoStream.Close();

            // Convert the encrypted byte array to a base64 encoded string
            string cipherText = Convert.ToBase64String(cipherBytes, 0, cipherBytes.Length);

        // Return the encrypted data as a string
        return cipherText;
    }

        static string DecryptString(string cipherText, byte[] key, byte[] iv)
    {
        // Instantiate a new Aes object to perform string symmetric encryption
        Aes encryptor = Aes.Create();

        encryptor.Mode = CipherMode.CBC;

        // Set key and IV
        byte[] aesKey = new byte[32];
        Array.Copy(key, 0, aesKey, 0, 32);
        encryptor.Key = aesKey;
        encryptor.IV = iv;

        // Instantiate a new MemoryStream object to contain the encrypted bytes
        MemoryStream memoryStream = new MemoryStream();

        // Instantiate a new encryptor from our Aes object
        ICryptoTransform aesDecryptor = encryptor.CreateDecryptor();

        // Instantiate a new CryptoStream object to process the data and write it to the 
        // memory stream
        CryptoStream cryptoStream = new CryptoStream(memoryStream, aesDecryptor, CryptoStreamMode.Write);

        // Will contain decrypted plaintext
        string plainText = String.Empty;

        try
        {
            // Convert the ciphertext string into a byte array
            byte[] cipherBytes = Convert.FromBase64String(cipherText);

            // Decrypt the input ciphertext string
            cryptoStream.Write(cipherBytes, 0, cipherBytes.Length);

            // Complete the decryption process
            cryptoStream.FlushFinalBlock();

            // Convert the decrypted data from a MemoryStream to a byte array
            byte[] plainBytes = memoryStream.ToArray();

            // Convert the decrypted byte array to string
            plainText = Encoding.ASCII.GetString(plainBytes, 0, plainBytes.Length);
        }
        finally
        {
            // Close both the MemoryStream and the CryptoStream
            memoryStream.Close();
            cryptoStream.Close();
        }

        // Return the decrypted data as a string
        return plainText;
    }
    static void Main(string[] args)
        {
            string message = "Quel est le nom de jeune fille de ma mère ?";
            string password = "Hakei54?sqSe6R71!lmdlrmPks65ekcj";
            string secretiv= "Ryfdhi.q546dmlfp";

            // Create sha256 hash
            SHA256 mySHA256 = SHA256.Create();
            byte[] key = mySHA256.ComputeHash(Encoding.ASCII.GetBytes(password));
            byte[] iv = new byte[16] { 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 };
            Array.Copy(mySHA256.ComputeHash(Encoding.ASCII.GetBytes(secretiv)), 0, iv, 0, 16);


            // Create secret IV
            //Console.OutputEncoding = System.Text.Encoding.UTF8;

            Console.WriteLine("Hello World!");
            string encrypted = EncryptString(message, key, iv);
            string decrypted = DecryptString(encrypted, key, iv);

            Console.WriteLine(encrypted);
            Console.WriteLine(decrypted);
            Console.ReadKey();
        }
    }
}

标签: c#phpencryptionaes

解决方案


推荐阅读