91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

c# rijndaelmanaged在.NET框架中的使用示例

c#
小樊
81
2024-08-13 15:09:43
欄目: 編程語言

RijndaelManaged類是.NET框架中用于對稱加密和解密數據的類。下面是一個簡單的示例,演示如何使用RijndaelManaged類進行數據加密和解密:

using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;

public class Program
{
    static void Main()
    {
        string plainText = "Hello, world!";
        string key = "0123456789abcdef";
        string iv = "fedcba9876543210";

        byte[] encryptedData = EncryptData(plainText, key, iv);
        string decryptedText = DecryptData(encryptedData, key, iv);

        Console.WriteLine("Encrypted Data: " + Convert.ToBase64String(encryptedData));
        Console.WriteLine("Decrypted Text: " + decryptedText);
    }

    static byte[] EncryptData(string plainText, string key, string iv)
    {
        using (RijndaelManaged rijndael = new RijndaelManaged())
        {
            rijndael.Key = Encoding.UTF8.GetBytes(key);
            rijndael.IV = Encoding.UTF8.GetBytes(iv);

            ICryptoTransform encryptor = rijndael.CreateEncryptor(rijndael.Key, rijndael.IV);

            using (MemoryStream ms = new MemoryStream())
            {
                using (CryptoStream cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write))
                {
                    byte[] plainBytes = Encoding.UTF8.GetBytes(plainText);
                    cs.Write(plainBytes, 0, plainBytes.Length);
                }

                return ms.ToArray();
            }
        }
    }

    static string DecryptData(byte[] encryptedData, string key, string iv)
    {
        using (RijndaelManaged rijndael = new RijndaelManaged())
        {
            rijndael.Key = Encoding.UTF8.GetBytes(key);
            rijndael.IV = Encoding.UTF8.GetBytes(iv);

            ICryptoTransform decryptor = rijndael.CreateDecryptor(rijndael.Key, rijndael.IV);

            using (MemoryStream ms = new MemoryStream(encryptedData))
            {
                using (CryptoStream cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Read))
                {
                    using (StreamReader sr = new StreamReader(cs))
                    {
                        return sr.ReadToEnd();
                    }
                }
            }
        }
    }
}

在上面的示例中,我們定義了EncryptData和DecryptData兩個方法來分別進行數據的加密和解密。在Main方法中,我們演示了如何使用這兩個方法來加密和解密數據。請注意,需要提供一個密鑰和初始化向量(IV)來進行加密和解密操作。

0
五大连池市| 冕宁县| 岱山县| 昌图县| 卓尼县| 泽库县| 竹溪县| 大邑县| 晋城| 宁南县| 莲花县| 曲麻莱县| 赤壁市| 特克斯县| 新泰市| 江津市| 石景山区| 贵溪市| 东方市| 深泽县| 常熟市| 建宁县| 湖北省| 东乡县| 固始县| 永泰县| 东丰县| 景谷| 新营市| 高雄市| 台湾省| 庆阳市| 祁阳县| 伊金霍洛旗| 胶州市| 盐池县| 清丰县| 同德县| 依安县| 凤山县| 财经|