」工欲善其事,必先利其器。「—孔子《論語.錄靈公》
首頁 > 程式設計 > 在C#中如何實現AES加密?

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

發佈於2025-03-13
瀏覽:513

How Can I Implement AES Encryption in C#?

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

簡介

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

示例實現

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

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

結論

此代碼示例提供了一種簡潔且實用的方法,可在您的 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.

最新教學 更多>

免責聲明: 提供的所有資源部分來自互聯網,如果有侵犯您的版權或其他權益,請說明詳細緣由並提供版權或權益證明然後發到郵箱:[email protected] 我們會在第一時間內為您處理。

Copyright© 2022 湘ICP备2022001581号-3