public class Encrypt
{
private static Encoding encoding = Encoding.Default;
public static string EncryptString(string originalStr)
{
return EncryptString(originalStr, "*&%$$FGad232f%^&&*()GTH$%3245gth", "@#%9286(^$jk$%#?", true);
}
private static string EncryptString(string originalStr, string key, string iv, bool returnBase64Code)
{
try
{
if (string.IsNullOrEmpty(originalStr) == false)
{
byte[] sourcesBytes;
byte[] originalBytes = encoding.GetBytes(originalStr);
byte[] keyBytes = encoding.GetBytes(key);
byte[] ivBytes = encoding.GetBytes(iv);
using (MemoryStream ms = new MemoryStream(originalBytes.Length))
{
using (RijndaelManaged rm = new RijndaelManaged())
{
using (ICryptoTransform ic = rm.CreateEncryptor(keyBytes, ivBytes))
{
using (CryptoStream cs = new CryptoStream(ms, ic, CryptoStreamMode.Write))
{
cs.Write(originalBytes, 0, originalBytes.Length);
cs.FlushFinalBlock();
sourcesBytes = ms.ToArray();
}
}
rm.Clear();
}
}
return (returnBase64Code ? Convert.ToBase64String(sourcesBytes) : encoding.GetString(sourcesBytes));
}
}
catch
{
}
return string.Empty;
}
public static string DecryptString(string targetStr)
{
return DecryptString(targetStr, "*&%$$FGad232f%^&&*()GTH$%3245gth", "@#%9286(^$jk$%#?", true);
}
private static string DecryptString(string targetstr, string key, string iv, bool returnBaseString)
{
try
{
byte[] targetBytes = (returnBaseString ? Convert.FromBase64String(targetstr) : encoding.GetBytes(targetstr));
byte[] sourcesBytes = new byte[targetBytes.Length];
byte[] keyBytes = encoding.GetBytes(key);
byte[] ivBytes = encoding.GetBytes(iv);
using (MemoryStream ms = new MemoryStream(targetBytes))
{
using (RijndaelManaged rg = new RijndaelManaged())
{
using (ICryptoTransform ic = rg.CreateDecryptor(keyBytes, ivBytes))
{
using (CryptoStream cs = new CryptoStream(ms, ic, CryptoStreamMode.Read))
{
cs.Read(sourcesBytes, 0, sourcesBytes.Length);
}
}
rg.Clear();
}
}
return encoding.GetString(sourcesBytes).Trim(new char[] { '\0' });
}
catch
{
}
return string.Empty;
}
public static string MD5Encrpyt(string originalStr, bool toUpper)
{
try
{
if (string.IsNullOrEmpty(originalStr) == false)
{
byte[] inputBytes = encoding.GetBytes(originalStr);
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
byte[] encryptBytes = md5.ComputeHash(inputBytes);
md5.Clear();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < encryptBytes.Length; i++)
{
sb.AppendFormat("{0:x2}", encryptBytes[i]);
}
return (toUpper ? sb.ToString().ToUpper() : sb.ToString());
}
}
catch
{
}
return string.Empty;
}
public static Encoding Encoding
{
get { return encoding; }
set { encoding = value; }
}
}
Encription
最新推荐文章于 2021-01-04 21:38:59 发布