0

我需要帮助才能运行我的 C# AES 加密。

在我的示例中,我有明文、密钥和结果。

在一个名为它的在线工具http://aes.online-domain-tools.com/中工作正常。

至少我知道我必须使用CipherMode.ECB.

    public static byte[] encrypt_Aes()
    {
        // bData = sData
        byte[] bData = { 0xf4, 0xef, 0x30, 0x8f, 0x1a, 0xba, 0x3e, 0xff, 0x9f, 0x5a, 0x11, 0x71, 0x72, 0xea, 0xca, 0xbd };
        string sData = "ôï0º>ÿŸZqrêʽ";
        byte[] bKey = { 0x45, 0x6E, 0x4F, 0x63, 0x65, 0x61, 0x6E, 0x20, 0x47, 0x6D, 0x62, 0x48, 0x2E, 0x31, 0x33, 0x00 };
        byte[] bResult = { 0x9b, 0xe2, 0xe3, 0x5d, 0x5f, 0xe7, 0x85, 0x86, 0x45, 0x20, 0x05, 0x87, 0xdd, 0x3b, 0x51, 0x5c };

        byte[] bEencrypted;

        using (Aes aes = Aes.Create())
        {
            aes.KeySize = 256;
            aes.BlockSize = 128;
            aes.Padding = PaddingMode.Zeros;
            aes.Mode = CipherMode.ECB;

            aes.Key = bKey;

            // Create an encryptor to perform the stream transform.
            ICryptoTransform encryptor = aes.CreateEncryptor();

            // 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(sData);
                    }
                    bEencrypted = msEncrypt.ToArray();
                }
            }
        }

        return bEencrypted;
    }
4

0 回答 0