”工欲善其事,必先利其器。“—孔子《论语.录灵公》
首页 > 编程 > 在C#中如何实现AES加密?

在C#中如何实现AES加密?

发布于2025-03-13
浏览:161

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