其实很简单,拉出来跑一遍就知道了(笑)[C#] 纯文本查看 复制代码 using System;
using System.Security.Cryptography;
using System.Text;
using Debiao.WebUtils;
using System.Configuration;
namespace Debiao.WebUtils
{
class TriDESEncrypt
{
private static string sKey = "ENCRYPTBOO";
static void Main(string[] args)
{
Console.WriteLine(Decrypt("Ub0BrU4gGkrY/Pdz2N1PgPf0hlIO1r7Z87cfndguNSA="));
Console.ReadKey();
}
public static string Encrypt(string original)
{
return TriDESEncrypt.Encrypt(original, TriDESEncrypt.sKey);
}
public static string Decrypt(string original)
{
return TriDESEncrypt.Decrypt(original, TriDESEncrypt.sKey, System.Text.Encoding.Default);
}
public static string Decrypt(string original, string key)
{
return TriDESEncrypt.Decrypt(original, key, System.Text.Encoding.Default);
}
public static string Decrypt(string original, System.Text.Encoding encoding)
{
return TriDESEncrypt.Decrypt(original, TriDESEncrypt.sKey, encoding);
}
public static string Encrypt(string original, string key)
{
string result;
try
{
byte[] bytes = System.Text.Encoding.Default.GetBytes(original);
byte[] bytes2 = System.Text.Encoding.Default.GetBytes(key);
result = System.Convert.ToBase64String(TriDESEncrypt.Encrypt(bytes, bytes2));
}
catch
{
result = "";
}
return result;
}
public static string Decrypt(string encrypted, string key, System.Text.Encoding encoding)
{
string result;
try
{
byte[] encrypted2 = System.Convert.FromBase64String(encrypted);
byte[] bytes = System.Text.Encoding.Default.GetBytes(key);
result = encoding.GetString(TriDESEncrypt.Decrypt(encrypted2, bytes));
}
catch
{
result = "";
}
return result;
}
public static byte[] MakeMD5(byte[] original)
{
System.Security.Cryptography.MD5CryptoServiceProvider mD5CryptoServiceProvider = new System.Security.Cryptography.MD5CryptoServiceProvider();
return mD5CryptoServiceProvider.ComputeHash(original);
}
public static byte[] Encrypt(byte[] original, byte[] key)
{
return new System.Security.Cryptography.TripleDESCryptoServiceProvider
{
Key = TriDESEncrypt.MakeMD5(key),
Mode = System.Security.Cryptography.CipherMode.ECB
}.CreateEncryptor().TransformFinalBlock(original, 0, original.Length);
}
public static byte[] Decrypt(byte[] encrypted, byte[] key)
{
return new System.Security.Cryptography.TripleDESCryptoServiceProvider
{
Key = TriDESEncrypt.MakeMD5(key),
Mode = System.Security.Cryptography.CipherMode.ECB
}.CreateDecryptor().TransformFinalBlock(encrypted, 0, encrypted.Length);
}
public static byte[] Encrypt(byte[] original)
{
byte[] bytes = System.Text.Encoding.Default.GetBytes(TriDESEncrypt.sKey);
return TriDESEncrypt.Encrypt(original, bytes);
}
public static byte[] Decrypt(byte[] encrypted)
{
byte[] bytes = System.Text.Encoding.Default.GetBytes(TriDESEncrypt.sKey);
return TriDESEncrypt.Decrypt(encrypted, bytes);
}
}
}
运行结果:
版主可别说我违规,我只是解码了这个字符串
解密流程:
1.把原文base64解码转为bytes数组
2.取key的md5作为密钥
3.解密
加密方法也是一样的,倒着来一遍就行了
|