using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.IO;
namespace AEStest
{
class Program
{
/// AES加密
/// </summary>
/// <param name="EncryptString">待加密密文</param>
/// <param name="EncryptKey">加密密钥</param>
/// <returns></returns>
public static string AESEncrypt(string EncryptString, string EncryptKey)
{
if (string.IsNullOrEmpty(EncryptString)) { throw (new Exception("密文不得为空")); }
if (string.IsNullOrEmpty(EncryptKey)) { throw (new Exception("密钥不得为空")); }
string m_strEncrypt = "";
byte[] m_btIV = Convert.FromBase64String("Rkb4jvUy/ye7Cd7k89QQgQ++");
Rijndael m_AESProvider = Rijndael.Create();
try
{
byte[] m_btEncryptString = Encoding.Default.GetBytes(EncryptString);
MemoryStream m_stream = new MemoryStream();
CryptoStream m_csstream = new CryptoStream(m_stream, m_AESProvider.CreateEncryptor(Encoding.Default.GetBytes(EncryptKey), m_btIV), CryptoStreamMode.Write);
m_csstream.Write(m_btEncryptString, 0, m_btEncryptString.Length);
m_csstream.FlushFinalBlock();
m_strEncrypt = Convert.ToBase64String(m_stream.ToArray());
m_stream.Close(); m_stream.Dispose();
m_csstream.Close(); m_csstream.Dispose();
}
catch (IOException ex) { throw ex; }
catch (CryptographicException ex) { throw ex; }
catch (ArgumentException ex) { throw ex; }
catch (Exception ex) { throw ex; }
finally { m_AESProvider.Clear(); }
return m_strEncrypt;
}
/// <summary>
/// AES 解密
/// </summary>
/// <param name="DecryptString">待解密密文</param>
/// <param name="DecryptKey">解密密钥</param>
/// <returns></returns>
public static string AESDecrypt(string DecryptString, string DecryptKey)
{
if (string.IsNullOrEmpty(DecryptString)) { throw (new Exception("密文不得为空")); }
if (string.IsNullOrEmpty(DecryptKey)) { throw (new Exception("密钥不得为空")); }
string m_strDecrypt = "";
byte[] m_btIV = Convert.FromBase64String("Rkb4jvUy/ye7Cd7k89QQgQ++");
Rijndael m_AESProvider = Rijndael.Create();
try
{
byte[] m_btDecryptString = Convert.FromBase64String(DecryptString);
MemoryStream m_stream = new MemoryStream();
CryptoStream m_csstream = new CryptoStream(m_stream, m_AESProvider.CreateDecryptor(Encoding.Default.GetBytes(DecryptKey), m_btIV), CryptoStreamMode.Write);
m_csstream.Write(m_btDecryptString, 0, m_btDecryptString.Length);
m_csstream.FlushFinalBlock();
m_strDecrypt = Encoding.Default.GetString(m_stream.ToArray());
m_stream.Close(); m_stream.Dispose();
m_csstream.Close(); m_csstream.Dispose();
}
catch (IOException ex) { throw ex; }
catch (CryptographicException ex) { throw ex; }
catch (ArgumentException ex) { throw ex; }
catch (Exception ex) { throw ex; }
finally { m_AESProvider.Clear(); }
return m_strDecrypt;
}
static void Main(string[] args)
{
Console.WriteLine("请输入需要加密的字符串:");
string str = Console.ReadLine();
string str1 = AESEncrypt(str,"123456");
Console.WriteLine("加密后的字符串为:");
Console.WriteLine(str1);
Console.WriteLine("下面为解密后的字符串:");
string str2=AESDecrypt(str1,"123456");
Console.WriteLine(str2);
Console.ReadKey();
}
}
}
分享一个C#下的加密解密的算法
最新推荐文章于 2023-03-22 10:11:18 发布