首页 > 后端开发 > C++ > 如何在C#中实现AES加密?

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

Patricia Arquette
发布: 2025-01-28 20:16:08
原创
845 人浏览过

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
作者最新文章
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板