将“AES-128-ECB”加密代码从 PHP 转换为 C#

问题描述 投票:0回答:1

我有一个要求,我需要将一些 PHP 代码转换为 C#.. 以下是需要转换的 PHP 代码:

$data = "Hello World!";

$key = "RTc0MDkwMEEwMDYxQjc4Mg=="

$encRaw = openssl_encrypt($data, 'AES-128-ECB', $key, OPENSSL_RAW_DATA);

要转换此 openssl 加密,我使用以下 C# 代码:

    static byte[] EncryptStringToBytes_Aes(string plainText, byte[] Key)
    {
        // Check arguments.
        if (plainText == null || plainText.Length <= 0)
            throw new ArgumentNullException("plainText");
        if (Key == null || Key.Length <= 0)
            throw new ArgumentNullException("Key");

        byte[] encrypted;

        // Create an Aes object
        // with the specified key and IV.
        using (Aes aesAlg = Aes.Create())
        {
            aesAlg.Key = Key;
            //aesAlg.IV = IV;

            // Create an encryptor to perform the stream transform.
            ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.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 the encrypted bytes from the memory stream.
        return encrypted;
    }

但不知何故,这并没有给我与 PHP 函数返回相同的结果。

有人可以帮我找出代码中的问题吗?或者建议我应该在代码中做哪些其他更改,以便获得与 PHP 函数返回的结果类似的结果。

c# php asp.net encryption aes
1个回答
0
投票

要将用于“AES-128-ECB”加密的 PHP 代码转换为 C#,您可以使用

AesManaged
命名空间中的
System.Security.Cryptography
类。下面是一个示例 C# 代码,它应该会为您提供与 PHP 代码相同的结果:

using System;
using System.Security.Cryptography;
using System.Text;

class Program
{
    static void Main(string[] args)
    {
        string data = "Hello World!";
        string key = "RTc0MDkwMEEwMDYxQjc4Mg==";

        byte[] keyBytes = Convert.FromBase64String(key);
        byte[] dataBytes = Encoding.UTF8.GetBytes(data);

        using (AesManaged aes = new AesManaged())
        {
            aes.KeySize = 128;
            aes.Key = keyBytes;
            aes.Mode = CipherMode.ECB;
            aes.Padding = PaddingMode.PKCS7;

            ICryptoTransform encryptor = aes.CreateEncryptor();

            byte[] encryptedBytes = encryptor.TransformFinalBlock(dataBytes, 0, dataBytes.Length);

            string encryptedString = Convert.ToBase64String(encryptedBytes);
            Console.WriteLine(encryptedString);
        }
    }
}

在上面的代码中,

data
key
变量被设置为 PHP 代码中使用的值。
key
变量经过 base64 解码以获得密钥的原始字节。
data
变量使用 UTF-8 编码转换为字节。

AesManaged
类用于执行加密。密钥大小设置为 128 位,模式设置为 ECB(电子密码本)模式。填充模式设置为 PKCS7,它向明文添加填充,使其成为块大小的倍数。

一个

ICryptoTransform
对象是使用
CreateEncryptor
类的
AesManaged
方法创建的。然后使用此转换来加密数据字节。

生成的加密字节是 base64 编码并打印到控制台。

此 C# 代码应为您提供与 PHP 代码相同的结果。

© www.soinside.com 2019 - 2024. All rights reserved.