private static byte[] Keys = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
private static string miyue = "09@/*!^-+123";
#region DES加密字符串
public static string EncryptDES(string encryptString)
{
try
{
byte[] rgbKey = Encoding.UTF8.GetBytes(miyue);
byte[] rgbIV = Keys;
byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);
DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider();
MemoryStream mStream = new MemoryStream();
CryptoStream cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
cStream.Write(inputByteArray, 0, inputByteArray.Length);
cStream.FlushFinalBlock();
byte[] tembyte = mStream.ToArray();
StringBuilder sb = new StringBuilder();
foreach (byte item in tembyte)
{
sb.AppendFormat("{0:x2}", item);
}
return sb.ToString();
}
catch (Exception)
{
throw;
}
}
#endregion
#region DES解密字符串
public static string DecryptDES(string decryptString)
{
try
{
byte[] rgbKey = Encoding.UTF8.GetBytes(miyue);
byte[] rgbIV = Keys;
decryptString = decryptString.Replace(" ", "");
decryptString = decryptString.Replace("\r\n", "");
string txt16 = decryptString;
byte[] inputByteArray = new byte[txt16.Length / 2];
for (int i = 0; i < inputByteArray.Length; i++)
{
inputByteArray[i] = Convert.ToByte(txt16.Substring(i * 2, 2), 16);
}
DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider();
MemoryStream mStream = new MemoryStream();
CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
cStream.Write(inputByteArray, 0, inputByteArray.Length);
cStream.FlushFinalBlock();
return Encoding.UTF8.GetString(mStream.ToArray());
}
catch (Exception)
{
throw;
}
}
#endregion
#region AES加密
public static string AESEncrypt(string text, string key)
{
string iv = "789!@#$%^&*()253LKJHGATGKGK/asdfsdq#$werasdfhhh";
RijndaelManaged rijndaelCipher = new RijndaelManaged();
rijndaelCipher.Mode = CipherMode.CBC;
rijndaelCipher.Padding = PaddingMode.PKCS7;
rijndaelCipher.KeySize = 256;
rijndaelCipher.BlockSize = 256;
Byte[] bKey = new Byte[32];
Array.Copy(Encoding.UTF8.GetBytes(key.PadRight(bKey.Length)), bKey, bKey.Length);
Byte[] bVector = new Byte[32];
Array.Copy(Encoding.UTF8.GetBytes(iv.PadRight(bVector.Length)), bVector, bVector.Length);
rijndaelCipher.Key = bKey;
rijndaelCipher.IV = bVector;
ICryptoTransform transform = rijndaelCipher.CreateEncryptor();
byte[] encryptedData = Encoding.UTF8.GetBytes(text);
byte[] cipherBytes = transform.TransformFinalBlock(encryptedData, 0, encryptedData.Length);
return Convert.ToBase64String(cipherBytes);
}
public static string AESDecrypt(string text, string key)
{
string iv = "789!@#$%^&*()253LKJHGATGKGK/asdfsdq#$werasdfhhh";
RijndaelManaged rijndaelCipher = new RijndaelManaged();
rijndaelCipher.Mode = CipherMode.CBC;
rijndaelCipher.Padding = PaddingMode.PKCS7;
rijndaelCipher.KeySize = 256;
rijndaelCipher.BlockSize = 256;
byte[] pwdBytes = System.Text.Encoding.UTF8.GetBytes(key);
Byte[] bKey = new Byte[32];
Array.Copy(Encoding.UTF8.GetBytes(key.PadRight(bKey.Length)), bKey, bKey.Length);
Byte[] bVector = new Byte[32];
Array.Copy(Encoding.UTF8.GetBytes(iv.PadRight(bVector.Length)), bVector, bVector.Length);
rijndaelCipher.Key = bKey;
rijndaelCipher.IV = bVector;
ICryptoTransform transform = rijndaelCipher.CreateDecryptor();
byte[] decryData = Convert.FromBase64String(text);
byte[] plainText = transform.TransformFinalBlock(decryData, 0, decryData.Length);
return Encoding.UTF8.GetString(plainText);
}
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
namespace AES_WINFORM
{
public class AesHelpter
{
private readonly static string _iv = "1462#¥@/*op";
private readonly static string _key = "asldf#$%HJ@/g*jk";
#region AES加密
public static string AESEncrypt(string text)
{
RijndaelManaged rijndaelCipher = new RijndaelManaged();
rijndaelCipher.Mode = CipherMode.CBC;
rijndaelCipher.Padding = PaddingMode.PKCS7;
rijndaelCipher.KeySize = 256;
rijndaelCipher.BlockSize = 256;
Byte[] bKey = new Byte[32];
Array.Copy(Encoding.UTF8.GetBytes(_key.PadRight(bKey.Length)), bKey, bKey.Length);
Byte[] bVector = new Byte[32];
Array.Copy(Encoding.UTF8.GetBytes(_iv.PadRight(bVector.Length)), bVector, bVector.Length);
rijndaelCipher.Key = bKey;
rijndaelCipher.IV = bVector;
ICryptoTransform transform = rijndaelCipher.CreateEncryptor();
byte[] encryptedData = Encoding.UTF8.GetBytes(text);
byte[] cipherBytes = transform.TransformFinalBlock(encryptedData, 0, encryptedData.Length);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < cipherBytes.LongLength; i++)
{
sb.Append(cipherBytes[i].ToString("X2"));
}
return sb.ToString();
}
public static string AESDecrypt(string decryptString)
{
RijndaelManaged rijndaelCipher = new RijndaelManaged();
rijndaelCipher.Mode = CipherMode.CBC;
rijndaelCipher.Padding = PaddingMode.PKCS7;
rijndaelCipher.KeySize = 256;
rijndaelCipher.BlockSize = 256;
decryptString = decryptString.Replace(" ", "");
decryptString = decryptString.Replace(System.Environment.NewLine, "");
string txt16 = decryptString;
byte[] inputByteArray = new byte[txt16.Length / 2];
for (int i = 0; i < inputByteArray.Length; i++)
{
inputByteArray[i] = Convert.ToByte(txt16.Substring(i * 2, 2), 16);
}
byte[] pwdBytes = System.Text.Encoding.UTF8.GetBytes(_key);
Byte[] bKey = new Byte[32];
Array.Copy(Encoding.UTF8.GetBytes(_key.PadRight(bKey.Length)), bKey, bKey.Length);
Byte[] bVector = new Byte[32];
Array.Copy(Encoding.UTF8.GetBytes(_iv.PadRight(bVector.Length)), bVector, bVector.Length);
rijndaelCipher.Key = bKey;
rijndaelCipher.IV = bVector;
ICryptoTransform transform = rijndaelCipher.CreateDecryptor();
byte[] plainText = transform.TransformFinalBlock(inputByteArray, 0, inputByteArray.Length);
return Encoding.UTF8.GetString(plainText);
}
#endregion
}
}