using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
namespace ConsoleApp1
{
class Program
{
private static string key = "xxxxxxxxxxxxxxxx";
static void Main(string[] args)
{
while (true)
{
try
{
string sourceText = Console.ReadLine();
string en = Encrypt(key, sourceText);
Console.WriteLine(en);
string de = Decrypt(key, en);
Console.WriteLine(de);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
public static string Encrypt(string key, string sourceText)
{
return Encrypt(key, key, sourceText);
}
public static string Encrypt(string key, string IV, string sourceText)
{
if (key == null)
throw new ArgumentNullException(nameof(key));
if (IV == null)
throw new ArgumentNullException(nameof(IV));
if (sourceText == null)
throw new ArgumentNullException(nameof(sourceText));
return Convert.ToBase64String(Encrypt(Encoding.UTF8.GetBytes(key), Encoding.UTF8.GetBytes(IV), sourceText));
}
public static byte[] Encrypt(byte[] rgbKey, byte[] rgbIV, string sourceText)
{
if (rgbKey == null)
throw new ArgumentNullException(nameof(rgbKey));
if (rgbIV == null)
throw new ArgumentNullException(nameof(rgbIV));
if (sourceText == null)
throw new ArgumentNullException(nameof(sourceText));
using (MemoryStream memoryStream = new MemoryStream())
{
using (Aes aes = Aes.Create())
using (ICryptoTransform transform = aes.CreateEncryptor(rgbKey, rgbIV))
using (CryptoStream cryptoStream = new CryptoStream(memoryStream, transform, CryptoStreamMode.Write))
using (StreamWriter streamWriter = new StreamWriter(cryptoStream))
{
streamWriter.Write(sourceText);
streamWriter.Flush();
}
return memoryStream.ToArray();
}
}
public static string Decrypt(string key, string cipherText)
{
return Decrypt(key, key, cipherText);
}
public static string Decrypt(string key, string IV, string cipherText)
{
if (key == null)
throw new ArgumentNullException(nameof(key));
if (IV == null)
throw new ArgumentNullException(nameof(IV));
if (cipherText == null)
throw new ArgumentNullException(nameof(cipherText));
return Decrypt(System.Text.Encoding.UTF8.GetBytes(key), System.Text.Encoding.UTF8.GetBytes(IV), Convert.FromBase64String(cipherText));
}
public static string Decrypt(string key, byte[] cipherBuffer)
{
return Decrypt(key, key, cipherBuffer);
}
public static string Decrypt(string key, string IV, byte[] cipherBuffer)
{
if (key == null)
throw new ArgumentNullException(nameof(key));
if (IV == null)
throw new ArgumentNullException(nameof(IV));
if (cipherBuffer == null)
throw new ArgumentNullException(nameof(cipherBuffer));
return Decrypt(System.Text.Encoding.UTF8.GetBytes(key), System.Text.Encoding.UTF8.GetBytes(IV), cipherBuffer);
}
public static string Decrypt(byte[] rgbKey, byte[] rgbIV, byte[] cipherBuffer)
{
if (rgbKey == null)
throw new ArgumentNullException(nameof(rgbKey));
if (rgbIV == null)
throw new ArgumentNullException(nameof(rgbIV));
if (cipherBuffer == null)
throw new ArgumentNullException(nameof(cipherBuffer));
using (MemoryStream stream = new MemoryStream(cipherBuffer))
{
return Decrypt(rgbKey, rgbIV, stream);
}
}
public static string Decrypt(string key, Stream cipherStream)
{
return Decrypt(key, key, cipherStream);
}
public static string Decrypt(string key, string IV, Stream cipherStream)
{
if (key == null)
throw new ArgumentNullException(nameof(key));
if (IV == null)
throw new ArgumentNullException(nameof(IV));
if (cipherStream == null)
throw new ArgumentNullException(nameof(cipherStream));
return Decrypt(Encoding.UTF8.GetBytes(key), Encoding.UTF8.GetBytes(IV), cipherStream);
}
public static string Decrypt(byte[] rgbKey, byte[] rgbIV, Stream cipherStream)
{
if (rgbKey == null)
throw new ArgumentNullException(nameof(rgbKey));
if (rgbIV == null)
throw new ArgumentNullException(nameof(rgbIV));
if (cipherStream == null)
throw new ArgumentNullException(nameof(cipherStream));
using (Aes aes = Aes.Create())
using (ICryptoTransform transform = aes.CreateDecryptor(rgbKey, rgbIV))
using (CryptoStream cryptoStream = new CryptoStream(cipherStream, transform, CryptoStreamMode.Read))
using (StreamReader streamReader = new StreamReader(cryptoStream))
{
return streamReader.ReadToEnd();
}
}
}
}
【加解密】C# AES加解密工具
于 2023-08-07 17:41:55 首次发布