using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Drawing; using System.Drawing.Text; using System.Drawing.Imaging; using System.Text; using System.Security.Cryptography; using System.IO; public partial class _Default : System.Web.UI.Page { public static string Key = "CaBertCp";//加密密钥必须为8位 protected void Page_Load(object sender, EventArgs e) { SetFontImage("微软雅黑", 20, MD5Decrypt("DBD0F5E5EE49E2F1B4BFCDF08A2E9F48 ")); //Response.Write(MD5Encrypt("139-1234-5678")); } //把这个页面当做图片来调用 /// <summary> /// 生成文字图片 /// </summary> /// <param name="familyName">字体</param> /// <param name="fontSize">字体大小</param> /// <param name="targetString">文字内容</param> public void SetFontImage(string familyName, int fontSize, string targetString) { //创建位图对象 Bitmap objBitmap = null; //创建绘图图面对象 Graphics g = null; //创建并初始化字体对象 Font stringFont = new Font(familyName, fontSize, FontStyle.Bold); //文本布局对象 StringFormat stringFormat = new StringFormat(); //设置文本格式 stringFormat.FormatFlags = StringFormatFlags.NoWrap; //绘图过程 try { //实例化位图对象 objBitmap = new Bitmap(1, 1); //实例化绘图图面对象,将位图对象放入图面中 g = Graphics.FromImage(objBitmap); //获取欲绘制文字图片的高宽 SizeF stringSize = g.MeasureString(targetString, stringFont); int nWidth = (int)stringSize.Width; int nHeight = (int)stringSize.Height; //获取高宽后释放无用的图面对象资源和位图对象资源 g.Dispose(); objBitmap.Dispose(); //根据已获得的高宽实例化新的位图对象 objBitmap = new Bitmap(nWidth, nHeight); //根据新的位图对象实例化新的绘图图面对象 g = Graphics.FromImage(objBitmap); //填充图片,可以指定背景颜色,开始坐标和图片高宽,指定文本呈现样式 g.FillRectangle(new SolidBrush(Color.White), new Rectangle(0, 0, nWidth, nHeight)); g.TextRenderingHint = TextRenderingHint.AntiAlias; //开始绘制 g.DrawString(targetString, stringFont, new SolidBrush(Color.Red), new PointF(0, 0), stringFormat); //生成图片 objBitmap.Save(Response.OutputStream, ImageFormat.Gif); } catch (Exception ee) { Response.Write(ee.Message.ToString()); } finally { if (null != g) g.Dispose(); if (null != objBitmap) objBitmap.Dispose(); Response.End(); } } /// <summary> /// 加密算法 /// </summary> /// <param name="pToEncrypt"></param> /// <returns></returns> public static string MD5Encrypt(string pToEncrypt) { DESCryptoServiceProvider des = new DESCryptoServiceProvider(); byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt); des.Key = ASCIIEncoding.ASCII.GetBytes(Key); des.IV = ASCIIEncoding.ASCII.GetBytes(Key); MemoryStream ms = new MemoryStream(); CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write); cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); StringBuilder ret = new StringBuilder(); foreach (byte b in ms.ToArray()) { ret.AppendFormat("{0:X2}", b); } ret.ToString(); return ret.ToString(); } /// <summary> /// 解密算法 /// </summary> /// <param name="pToDecrypt"></param> /// <returns></returns> public static string MD5Decrypt(string pToDecrypt) { try { DESCryptoServiceProvider des = new DESCryptoServiceProvider(); byte[] inputByteArray = new byte[pToDecrypt.Length / 2]; for (int x = 0; x < pToDecrypt.Length / 2; x++) { int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16)); inputByteArray[x] = (byte)i; } des.Key = ASCIIEncoding.ASCII.GetBytes(Key); des.IV = ASCIIEncoding.ASCII.GetBytes(Key); MemoryStream ms = new MemoryStream(); CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write); cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); StringBuilder ret = new StringBuilder(); return System.Text.Encoding.ASCII.GetString(ms.ToArray()); } catch (Exception) { return "www.cnblogs.com"; } } }