加密代码

本文提供了一系列实用的C#代码片段,包括了字符串的MD5和SHA1加密方法,判断HTTP请求是否为文件上传请求的方法,以及生成验证码图片和随机字符串的功能。这些代码简洁高效,适用于多种应用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

public static class Hash
 {
  public static string md5(string s)
  {
   return FormsAuthentication.HashPasswordForStoringInConfigFile(s, "MD5").ToLower();
  }
  public static string sha1(string s)
  {
   return FormsAuthentication.HashPasswordForStoringInConfigFile(s, "SHA1").ToLower();
  }
 }

 public static class HTTP
 {
  public static HttpWorkerRequest GetWokerRequest()
  {
   return (HttpContext.Current as IServiceProvider).GetService(typeof(HttpWorkerRequest)) as HttpWorkerRequest;
  }

  public static bool IsUploadRequest(HttpRequest r)
  {
   return r.ContentType.ToLower().StartsWith("multipart/form-data");
  }
 }

 public static class RandCodeBuilder
 {
  public static byte[] BuildImage(string s)
  {
   Bitmap bm = new Bitmap(76, 24, PixelFormat.Format32bppArgb);
   Graphics g = Graphics.FromImage(bm);
   Random r = new Random();
   
   g.Clear(Color.White);
   /*
   for (int i = 0; i < 50; i++)
   {
    int x1 = r.Next(bm.Width);
    int x2 = r.Next(bm.Width);
    int y1 = r.Next(bm.Height);
    int y2 = r.Next(bm.Height);
    g.DrawLine(new Pen(Color.FromArgb(r.Next())), x1, y1, x2, y2);
   }
   */
   for (int i = 0; i < 200; i++)
   {
    int x = r.Next(bm.Width);
    int y = r.Next(bm.Height);
    bm.SetPixel(x, y, Color.Silver);
   }
   
   int j = r.Next(1, 4);
   Font f = new Font("Courier New", 14, FontStyle.Bold);
   LinearGradientBrush lgb = new LinearGradientBrush(new Rectangle(0, 0, 76, 24), Color.DarkGreen, Color.DarkSeaGreen, 1.2f, true);
   g.DrawString(s, f, lgb, 0, 0);
   //g.DrawRectangle(new Pen(Color.Silver), 0, 0, 70, 22);
   
   MemoryStream ms = new MemoryStream();
   bm.Save(ms, ImageFormat.Gif);
   return ms.ToArray();
  }
 }
 
 public static class Text
 {
  private static char[] seeds = { '0','1','2','3','4','5','6','7','8','9', 'a','b','c','d','e','f','g','h','i','j', 'k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z' };

  public static string RandString(int len)
  {
   StringBuilder sb = new StringBuilder(36);
   Random r = new Random();
   for (int i = 0; i < len; i++)
   {
    sb.Append(seeds[r.Next(36)]);
   }
   return sb.ToString();
  }
 } 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值