C# AesHelper
本文最后更新于 325 天前,其中的信息可能已经有所发展或是发生改变。
using System;
using System.Security.Cryptography;
using System.Text;

public class AesHelper
{
    public static string Encrypt(string text, string key, string iv = "")
    {
        byte[] textBytes = Encoding.UTF8.GetBytes(text);

        using (Aes aes = Aes.Create())
        {
            aes.Key = SHA256.Create().ComputeHash(Encoding.UTF8.GetBytes(key));
            aes.IV = MD5.Create().ComputeHash(Encoding.UTF8.GetBytes(iv));

            ICryptoTransform encryptor = aes.CreateEncryptor();
            byte[] encryptedBytes = encryptor.TransformFinalBlock(textBytes, 0, textBytes.Length);

            return Convert.ToBase64String(encryptedBytes);
        }
    }

    public static string Decrypt(string text, string key, string iv = "")
    {
        byte[] textBytes = Convert.FromBase64String(text);

        using (Aes aes = Aes.Create())
        {
            aes.Key = SHA256.Create().ComputeHash(Encoding.UTF8.GetBytes(key));
            aes.IV = MD5.Create().ComputeHash(Encoding.UTF8.GetBytes(iv));

            ICryptoTransform decryptor = aes.CreateDecryptor();
            byte[] decryptedBytes = decryptor.TransformFinalBlock(textBytes, 0, textBytes.Length);

            return Encoding.UTF8.GetString(decryptedBytes);
        }
    }
}
Console.Write("请输入要加密的文本:");
string text = Console.ReadLine();

Console.Write("请输入密钥(Key):");
string key = Console.ReadLine();

Console.Write("请输入初始化向量(IV):");
string iv = Console.ReadLine();

string encrypted = AesHelper.Encrypt(text, key, iv);
Console.WriteLine("【加密结果】{0}", encrypted);

string decrypted = AesHelper.Decrypt(encrypted, key, iv);
Console.WriteLine("【解密结果】{0}", decrypted);
作者:AlexSJC
本文采用 CC BY-NC-ND 4.0 许可协议
暂无评论

发送评论 编辑评论


|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇