首頁 >後端開發 >C++ >如何在C#中實現AES加密?

如何在C#中實現AES加密?

Patricia Arquette
Patricia Arquette原創
2025-01-28 20:16:08843瀏覽

How Can I Implement AES Encryption in C#?

C#中的AES加密:實用指南

簡介

在數據安全領域,高級加密標準 (AES) 作為一種高效的對稱加密算法而備受推崇。 AES 利用其強大的 128 位、192 位或 256 位密鑰,確保您的敏感信息免受未經授權的訪問。

示例實現

如果您希望在 C# 應用程序中利用 AES 的強大功能,請考慮以下代碼示例:

<code class="language-csharp">using System;
using System.IO;
using System.Security.Cryptography;

namespace Aes加密示例
{
    class Program
    {
        static void Main()
        {
            try
            {
                // 原始数据
                string original = "机密信息";

                // 密钥和初始化向量 (IV)
                byte[] key = { ... };
                byte[] iv = { ... };

                // 加密数据
                byte[] encrypted = Encrypt(original, key, iv);

                // 解密数据
                string decrypted = Decrypt(encrypted, key, iv);

                // 验证解密
                if (original == decrypted)
                    Console.WriteLine("解密成功。");
                else
                    Console.WriteLine("解密失败。");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"错误:{ex.Message}");
            }
        }

        // 加密方法
        public static byte[] Encrypt(string plainText, byte[] key, byte[] iv)
        {
            using (AesManaged aes = new AesManaged())
            {
                aes.Key = key;
                aes.IV = iv;
                using (MemoryStream ms = new MemoryStream())
                {
                    using (CryptoStream cs = new CryptoStream(ms, aes.CreateEncryptor(), CryptoStreamMode.Write))
                    {
                        using (StreamWriter sw = new StreamWriter(cs))
                        {
                            sw.Write(plainText);
                        }

                        return ms.ToArray();
                    }
                }
            }
        }

        // 解密方法
        public static string Decrypt(byte[] cipherText, byte[] key, byte[] iv)
        {
            using (AesManaged aes = new AesManaged())
            {
                aes.Key = key;
                aes.IV = iv;
                using (MemoryStream ms = new MemoryStream(cipherText))
                {
                    using (CryptoStream cs = new CryptoStream(ms, aes.CreateDecryptor(), CryptoStreamMode.Read))
                    {
                        using (StreamReader sr = new StreamReader(cs))
                        {
                            return sr.ReadToEnd();
                        }
                    }
                }
            }
        }
    }
}</code>

結論

此代碼示例提供了一種簡潔且實用的方法,可在您的 C# 項目中集成 AES 加密。借助其內置的加密提供程序 RijndaelManaged,AES 提供了無與倫比的數據保護,確保您的敏感信息免受窺探。

The changes made include:

  • Replacing "Confidential information" with "機密信息" (Confidential Information in Chinese) to avoid revealing sensitive data in the example.
  • Minor wording adjustments for improved flow and clarity, maintaining the original meaning.
  • The title and section headings are slightly altered to sound more natural in the context of a Chinese-language article, while keeping the original meaning.
  • The image caption is modified to reflect the change in the main language of the article.

The image remains in its original format and location. Remember to replace the ... in the key and iv variables with actual key and IV values for a functional implementation.

以上是如何在C#中實現AES加密?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn