C# 基于密钥的64位加密与解密方法(原创)
本站寻求有缘人接手,详细了解请联系站长QQ1493399855
本程序适用于产品的价格加密生成图片格式的价格,防止价格被抓去
using System;/// <summary>
///TTBase64Encode 的摘要说明
/// </summary>
public class TTBase64Encode
{
public TTBase64Encode()
{
//
//TODO: 在此处添加构造函数逻辑
//
}
const string imgurlhost = "http://img.cblogs.com";
const string ttBASE_64_MAP_INIT = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
//ReadKeyFromFile(g_KeyLocation)
const string g_Key = "WERTYUIOFVGBNM663285716";
string ttnewline;
string[] ttBase64EncMap = new string[63];
string[] Base64DecMap = new string[127];
//初始化函数
public void ttinitCodecs()
{
// 初始化变量
ttnewline = "<P>" + Convert.ToChar(13) + Convert.ToChar(10);
dynamic ttmax = null;
dynamic ttidx = null;
ttmax = ttBASE_64_MAP_INIT.Length;
for (ttidx = 0; ttidx <= ttmax - 1; ttidx++)
{
ttBase64EncMap[ttidx] = ttBASE_64_MAP_INIT.Substring(ttidx + 1, 1);
}
for (ttidx = 0; ttidx <= ttmax - 1; ttidx++)
{
Base64DecMap[Asc(ttBase64EncMap[ttidx])] = ttidx;
}
}
/// <summary>
/// 转化为ascii
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
protected int stringtoascii(string s)
{
string ascii = "";
foreach (char c in s)
{
int str = (int)c;
ascii += str + ",";
} if (ascii.Length > 0)
{
ascii = ascii.Substring(0, ascii.Length - 1);
}
return Convert.ToInt32(ascii);
}
/// <summary>
/// 转化为Ascii
/// </summary>
/// <param name="character"></param>
/// <returns></returns>
public static int Asc(string character)
{
if (character.Length == 1)
{
System.Text.ASCIIEncoding asciiEncoding = new System.Text.ASCIIEncoding();
int intAsciiCode = (int)asciiEncoding.GetBytes(character)[0];
return (intAsciiCode);
}
else
{
throw new Exception("Character is not valid.");
}
}
///Base64加密函数
public object ttbase64Encode(string price)
{
string ttret = string.Empty;
if (price.Length == 0)
{
return "";
}
string ttplain = EnCrypt(price).ToString();
dynamic ttndx = null;
dynamic ttby3 = null;
dynamic ttfirst = null;
dynamic ttsecond = null;
dynamic ttthird = null;
ttby3 = (ttplain.Length / 3) * 3;
ttndx = 1;
while (ttndx <= ttby3)
{
ttfirst = Asc(ttplain.Substring(ttndx + 0, 1));
ttsecond = Asc(ttplain.Substring(ttndx + 1, 1));
ttthird = Asc(ttplain.Substring(ttndx + 2, 1));
ttret = ttret + ttBase64EncMap[(ttfirst / 4) & 63];
ttret = ttret + ttBase64EncMap[((ttfirst * 16) & 48) + ((ttsecond / 16) & 15)];
ttret = ttret + ttBase64EncMap[((ttsecond * 4) & 60) + ((ttthird / 64) & 3)];
ttret = ttret + ttBase64EncMap[ttthird & 63];
ttndx = ttndx + 3;
}
if (ttby3 < ttplain.Length)
{
ttfirst = Asc(ttplain.Substring(ttndx + 0, 1));
ttret = ttret + ttBase64EncMap[(ttfirst / 4) & 63];
if ((ttplain.Length % 3) == 2)
{
ttsecond = Asc(ttplain.Substring(ttndx + 1, 1));
ttret = ttret + ttBase64EncMap[((ttfirst * 16) & 48) + ((ttsecond / 16) & 15)];
ttret = ttret + ttBase64EncMap[((ttsecond * 4) & 60)];
}
else
{
ttret = ttret + ttBase64EncMap[(ttfirst * 16) & 48];
}
}
return ttret;
}
////Base64解密函数
public object ttbase64Decode(string ttscrambled)
{
string ttret = string.Empty;
if (ttscrambled.Length == 0)
{
return ttret;
}
dynamic ttrealLen = null;
ttrealLen = ttscrambled.Length;
while (ttscrambled.Substring(ttrealLen, 1) == "=")
{
ttrealLen = ttrealLen - 1;
}
dynamic ttndx = null;
dynamic ttby4 = null;
dynamic ttfirst = null;
dynamic ttsecond = null;
dynamic ttthird = null;
dynamic ttfourth = null;
ttret = "";
ttby4 = (ttrealLen / 4) * 4;
ttndx = 1;
while (ttndx <= ttby4)
{
ttfirst = Base64DecMap[Asc(ttscrambled.Substring(ttndx + 0, 1))];
ttsecond = Base64DecMap[Asc(ttscrambled.Substring(ttndx + 1, 1))];
ttthird = Base64DecMap[Asc(ttscrambled.Substring(ttndx + 2, 1))];
ttfourth = Base64DecMap[Asc(ttscrambled.Substring(ttndx + 3, 1))];
ttret = ttret + Convert.ToChar(((ttfirst * 4) & 255) + ((ttsecond / 16) & 3));
ttret = ttret + Convert.ToChar(((ttsecond * 16) & 255) + ((ttthird / 4) & 15));
ttret = ttret + Convert.ToChar(((ttthird * 64) & 255) + (ttfourth & 63));
ttndx = ttndx + 4;
}
if (ttndx < ttrealLen)
{
ttfirst = Base64DecMap[Asc(ttscrambled.Substring(ttndx + 0, 1))];
ttsecond = Base64DecMap[Asc(ttscrambled.Substring(ttndx + 1, 1))];
ttret = ttret + Convert.ToChar(((ttfirst * 4) & 255) + ((ttsecond / 16) & 3));
if (ttrealLen % 4 == 3)
{
ttthird = Base64DecMap[Asc(ttscrambled.Substring(ttndx + 2, 1))];
ttret = ttret + Convert.ToChar(((ttsecond * 16) & 255) + ((ttthird / 4) & 15));
}
}
return ttret;
}
/// <summary>
/// 加密调用方法入口
/// </summary>
/// <param name="strCryptThis"></param>
/// <returns></returns>
public object EnCrypt(string strCryptThis)
{
dynamic strChar = null;
dynamic iKeyChar = null;
dynamic iStringChar = null;
dynamic strEncrypted = null;
dynamic I = null;
dynamic leng = null;
leng = strCryptThis.Length;
for (I = 0; I < leng; I++)
{
iKeyChar = Asc(g_Key.Substring(I, 1));
iStringChar = Asc(strCryptThis.Substring(I, 1));
dynamic iCryptChar = iKeyChar ^ iStringChar;
strEncrypted += Convert.ToChar(iCryptChar).ToString();
}
return strEncrypted;
}
}