Home >Backend Development >C++ >How Can I Implement AES Encryption in C#?

How Can I Implement AES Encryption in C#?

Patricia Arquette
Patricia ArquetteOriginal
2025-01-28 20:16:08847browse

How Can I Implement AES Encryption in C#?

AES encryption in C#: Practical Guide

Introduction

In the field of data security, high -level encryption standards (AES) are highly respected as an efficient symmetrical encryption algorithm. AES uses its powerful 128 -bit, 192 -bit, or 256 -bit key to ensure that your sensitive information is avoided from unauthorized access.

Example implementation

If you want to use AES's powerful features in the C# application, please consider the following code example:

Conclusion

<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>
This code example provides a simple and practical method, which can integrate AES encryption in your C# project. With its built -in encryption provider Rijndaelmanaged, AES provides unparalleled data protection to ensure that your sensitive information is avoided.

The Changes Made Include:

Replass "Confidence 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 slightened altered to sound, "
  • The Image Caption is Modified to Reflect The Main Language of the Article.
  • The Image Remains in ITS Original Format and Location. Remember to Replace the
  • In the
  • and
  • Variables with Actual Key and Iv Values ​​for A ONAL Implementation.

The above is the detailed content of How Can I Implement AES Encryption in C#?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn